gpt4 book ai didi

javascript - 单击时上下滑动 Div

转载 作者:行者123 更新时间:2023-11-28 02:26:40 25 4
gpt4 key购买 nike

单击标签时,div 相应地打开和关闭。我想要的是 div 应该缓慢地上下滑动,而不是立即显示。

<a href="" class="accordion">Click here</a>
<div class="panel" id="AccordionDiv">
<div class="store">
<div class="store-row">

<div class="cells store-logo text-center">
<img src="@strStaticWebsiteUrl@(objOfferPrice.StoreImage)" alt="" />
</div>

@if (objOfferPrice.Price < objOfferPrice.UrlPrice)
{
<div class="cells text-center">
<div class="product-price offer-price">Rs. @String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", objOfferPrice.Price)<sup>*</sup></div>
<p class="real-price">Price: @objOfferPrice.UrlPrice</p>
</div>
}
</div>
</div>

这是 html 代码,下面是脚本代码。

            <script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {

this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
</script>

最佳答案

使用 CSS

CSS 动画看起来也比 Javascript 动画好很多。 CSS 动画可以中途取消/反转。

Javascript:

<script>
var acc = document.getElementsByClassName("accordion");
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("slideActive");
});
}
</script>

CSS:

.accordion {
height: 0px;
transition: 0.3s;
}
.accordion .slideActive{
height: 100px;
}

(或者您喜欢的任何样式)


您可以使用以下方法检查它是否事件:

this.classList.contains('slideActive');

关于javascript - 单击时上下滑动 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47922905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com