css 如何在单击按钮时显示div

polhcujo  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(149)

我想在按下按钮时使屏幕变暗,但我不知道怎么做。我试着在商店开门时显示图像,在商店关门时关闭图像。我用下面的类做了一个div来使屏幕变暗:
我该怎么解决这个问题,怎么才能让它变得更好?
我希望d-show在shopOpen为真时启用,在为假时消失。

var shopOpen = false;
var d = document.getElementById("darken")
d.classList.add('darken');
d.classList.remove('d-show')

function toggleShop() {
  if (shopOpen = false) {
    d.classList.toggle('d-show');
    d.classList.toggle('darken');
    shopOpen = true;
  }
  if (shopOpen = true) {
    d.classList.toggle('darken');
    d.classList.toggle('d-show');
    shopOpen = false;
  } else {
    console.log("an error has occured while displaying 'darken'")
  }
}
.darken {
  display: none;
  opacity: 0%;
  position: absolute;
  top: 0%;
  left: 0%;
  background: rgba(0, 0, 0, 0.5);
  height: 100vh;
  width: 100vw;
  z-index: 90;
}

.d-show {
  display: block;
  opacity: 100%;
}
<div class="darken" id="darken"></div>
<div class="header">
  <h1 id='scrdisp' class='score'></h1>
</div>
<div class="cookieholder">
  <img src="assets/cookie.png" class="cookie" id="cookie" onclick="
            score = score + 1; 
            document.getElementById('scrdisp').innerHTML = score;
            var r = document.querySelector(':root');
            var ck = document.getElementById('cookie')
            ck.classList.remove('cookie-wtAni')
            var rV = parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--🍪hxw').replace('px', ''));
            var aT = parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--🍪ani').replace('px', ''));
            /* r.style.setProperty('--🍪hxw', rV + 15 + 'px');
            setTimeout(() => { r.style.setProperty('--🍪hxw', '250px'); }, 50); */
            ck.classList.add('cookie-wtAni');
            setTimeout(() => { ck.classList.remove('cookie-wtAni'); }, aT);
         " />
</div>
<img src="assets/shop.png" id="shopBtn" class="shopBtn" onclick="toggleShop()" />
bfhwhh0e

bfhwhh0e1#

{display: none;}
表示元素不占空间,因此提供的高度、宽度、不透明度和背景属性无关紧要

6l7fqoea

6l7fqoea2#

您可以切换元素上的hidden属性来执行此操作。

<button onclick="document.querySelector('div').toggleAttribute('hidden')">click me
</button>
<div style="height:32px; width:32px; background-color:black"> </div>

相关问题