gpt4 book ai didi

jquery - 如何在 jQuery 中正确滚动顶部到 div?

转载 作者:行者123 更新时间:2023-12-01 07:06:03 25 4
gpt4 key购买 nike

好吧,基本上我有:

<button class="contactButton">XYZ</button>

当用户按下此按钮时,我希望我的页面向下滚动到特定的 div。

我尝试过这个:

$(".contactButton").click(function () {
$("html,body").animate({
scrollTop: $(".specificDivClass").offset().top
}, 1100);
});

我还尝试了该网站的其他解决方案,但没有结果。我是 jQuery 新手,感谢您的建议。

最佳答案

这里有一个解决方案 https://jsfiddle.net/psfLbo1n/

$(".contactButton").click(function () {
$("html,body").animate({
scrollTop: $(".specificDivClass").offset().top
}, 1100);
});
.div1{
height: 700px;
width: 100%;
background: red;
}

.specificDivClass{
height: 1000px;
width: 100%;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="contactButton">XYZ</button>

<div class="div1">

</div>

<div class="specificDivClass">

</div>

关于jquery - 如何在 jQuery 中正确滚动顶部到 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332858/

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