【问题标题】:Is there a way to hide absolute div with JS?有没有办法用JS隐藏绝对div?
【发布时间】:2021-11-02 15:45:04
【问题描述】:

我有这个.endGame div,我想隐藏它,然后在需要时再次显示。所以我创建了.endGameDisplay 来隐藏它,但它不起作用,我不知道为什么。 仅当display: none;.endGame 中时才会隐藏

在 java 脚本中,我希望它在页面打开时隐藏,所以我有这样的东西:

const endGame = document.querySelector('.endGame');

endGameOff();

//end game on func
function endGameOn() {
    endGame.classList.remove('.endGameDisplay');
}

//end game off func
function endGameOff() {
    endGame.classList.add('.endGameDisplay');
}

我还尝试将classlist.add 更改为.remove 以检查我是否没有记错但仍然没有。

我的 HTML:

<div class="endGame">
    <h1>You</h1>
    <h2 class="wonORlost">text</h2>
    <h3>Do you want to restar game or go back to the menu?</h3>
    <div class="btns">
         <div class="restart">Restart</div>
         <div class="exit">Exit</div>
    </div>
</div>

我的 CSS:

.endGame {
        position: absolute;
        z-index: 1;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        background: $primary-color;
        color: $white-color;
        border-radius: 25px;
        text-align: center;
        box-shadow: 0 0 15px #333;
        

        .h1 {
            font-weight: $font-bold;
        }

        .h2 {
            font-weight: $font-bold;
            color: lighten($bot-color, 15%);
        }

        .h3 {
            font-weight: $font-normal;
        }

        .btns {
            display: grid;
            justify-content: center;
            justify-items: center;

            .restart {
                background: $green-color;
                cursor: pointer;
                @include transition-ease;
    
                &:hover {
                    background: darken($green-color, 10%);
                    @include transition-ease;
                }
            }
    
            .exit {
                background: $red-color;
                cursor: pointer;
                @include transition-ease;
    
                &:hover {
                    background: darken($red-color, 10%);
                    @include transition-ease;
                }
            }
        }

        &.endGameDisplay {
            display: none;
        }
    }

【问题讨论】:

    标签: javascript html css hide absolute


    【解决方案1】:

    添加类时删除句点 (.)。 .endGameDisplay 应该是endGameDisplay

    const endGame = document.querySelector('.endGame');
    
    endGameOff();
    
    //end game on func
    function endGameOn() {
      endGame.classList.remove('endGameDisplay');
    }
    
    //end game off func
    function endGameOff() {
      endGame.classList.add('endGameDisplay');
    }
    .endGameDisplay {
      display: none;
    }
    <div class="endGame">
      <h1>You</h1>
      <h2 class="wonORlost">text</h2>
      <h3>Do you want to restar game or go back to the menu?</h3>
      <div class="btns">
        <div class="restart">Restart</div>
        <div class="exit">Exit</div>
      </div>
    </div>

    【讨论】:

    • 非常感谢! Ohhh .. 这很容易解决,但相信我,我在问这个问题之前 1.5 小时搜索了问题的答案,但没有看到。再次感谢;)
    猜你喜欢
    • 2021-09-02
    • 2015-12-11
    • 2021-04-28
    • 2019-01-23
    • 2019-06-26
    • 2016-01-18
    • 2016-05-29
    • 2019-02-19
    • 2019-08-14
    相关资源
    最近更新 更多