【问题标题】:How do I hide html elements for x time without them taking up space?如何在不占用空间的情况下隐藏 html 元素 x 时间?
【发布时间】:2019-08-14 12:32:07
【问题描述】:

我有以下页面:

<div class="background">
        <section class="statements">
            <article>
                <div class="article_background hidden" id="forge"></div>
                <p>
                    Greek Mythology states that Hephaistos was the god of blacksmiths, metalworking, carpenters,
                    craftsmen, artisans, sculptors, metallurgy, fire, and volcanoes.
                    During his lifetime he accomplished the creation of wonderous items; Achilles armor, Hermes winged
                    helmet and sandals, even going as far as to create automatons to work for him.
                </p>
            </article>
        </section>
</div>

我想让它不可见。我试过visibility,问题是它仍然占用空间。

我对@9​​87654323@ 和visibility 进行了很多尝试,但结果从来都不是我所希望的。

我正在尝试在纯 css 中完成此操作

【问题讨论】:

  • 您在寻找什么结果?你试过display: none吗?
  • 您已经尝试了很多display,但还没有偶然发现none?真的吗?
  • 如果您仔细阅读它指出的问题hide the elements for x time,这意味着我希望能够再次显示它们,这在设置为display: none; 后不起作用。我已经在 chrome、edge 和 firefox 上对其进行了测试。 @斯科特马库斯
  • @GetOffMyLawn 是的,我试过了,问题是如果你想再次显示它,它根本不起作用。我已经在实际的 Firefox/Chrome/Edge 上对其进行了测试,但它从未起作用,主要是因为与 display: none 没有对立面,就像 visibility: hidden 一样
  • 相反的是display: initial甚至display: blockdisplay: inlinedisplay: inline-block

标签: javascript html css load invisible


【解决方案1】:

通过这个简单的代码设置,无需任何 js 即可解决:

.background {
        overflow: hidden;
        visibility: hidden;
        max-height: 0px;
}

.background {
        animation: visibility 0ms 4.5s both;
        -webkit-animation: visibility 0ms 4.5s both;
}

@-webkit-keyframes visibility {
        100% {
                max-height: inherit;
                visibility: visible;
        }
}

@keyframes visibility {
        100% {
                max-height: inherit;
                visibility: visible;
        }
}

由于元素在开始时的高度为 0 且不可见,因此它们不占用滚动条中显示的空间,但仍会在访问时加载。

然后,在设置4.5s 之后,此功能会消除限制并显示所有内容,因为它是在 css 和 html 中预先配置的。希望这对某人有帮助!

【讨论】:

  • @keyframes 已经成为标准大约 5 年了。所有浏览器都支持它。没有理由再给它添加前缀了。
【解决方案2】:

您只需要display:none 隐藏该元素,然后display:blockdisplay:inline(取决于您使用的元素类型)再次显示它。或者,实际上您可以将display:none 应用于一个元素,然后删除该应用程序以恢复以前的样式。

至于计时部分,您可以使用setTimeout()将其隐藏一段时间。

let theDiv = document.querySelector("div");

document.querySelector("button").addEventListener("click", function(){
  theDiv.classList.add("hidden");    // Hide the element by applying the style
  
  // Then set up a function to run 5 seconds later
  setTimeout(function(){
    theDiv.textContent = "I'm back!";
    theDiv.classList.remove("hidden"); // Remove the class that hides the element
  }, 5000);
});
.hidden { display:none; } /* Will be applied for 5 seconds */
<button>Hide the text for 5 seconds:</button>
<div>Now you see me...</div>
<div>I'll be here the whole time, but I'll move up when the element above me gets hidden and I'll move down when it comes back.</div>
<div class="hidden">You'll never see me</div>
<div>&copy; 2019</div>

【讨论】:

  • 让我感到惊讶,这显然适用于 js。我正在用纯 css 尝试它,所以这可以解释为什么它不起作用。
  • display: none 被触发没有问题,但使用动画功能的延迟不再显示它。通过用可见性交换display 函数,它们可以工作,这意味着逻辑是正确的。整个display: none thingie 不喜欢之后通过 css 进行更改,显然
  • @Redimo CSS 过渡和动画只能使用具有可迭代值的属性。颜色值之所以起作用,是因为 CSS 渲染引擎知道如何从一个 Hex、命名颜色或 RGB 值到下一个。所有数字都有效(显然),visibility 有效,因为在幕后可见性是在数字尺度上管理的。 display 不能通过数字刻度工作,因此您不能使用过渡或动画。
  • 如果我没记错的话,这意味着它只能通过 js 工作。因为我为display:none display: initial 创建了一个只有css 的空模板,但它不起作用。
  • @Redimo 说它只适用于 JS 不太准确。由于上述原因,在动画的上下文中,display 根本不起作用(通过 CSS 或 JS)。我上面的例子不是动画display,它会在一段时间后立即改变它。动画试图在一段时间内改变一个值,因此渲染引擎必须能够知道如何从初始值转换到最终值。 noneblock 之间没有任何值,只能是其中之一,因此您无法为其设置动画。
猜你喜欢
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2015-07-25
  • 2014-06-11
  • 2022-11-27
  • 2016-02-01
  • 2022-01-09
  • 2011-02-25
相关资源
最近更新 更多