我有几个div
s .posts
其中有一个attr data-id
这对应于mysql DB id
.
<div class="posts" data-id="1"></div>
<div class="posts" data-id="2"></div>
现在如果我想滚动到特定的div
只有我知道data-id
。
我将如何滚动到它?
我的JSFiddle 在这里。
谁能给出一个例子以及 JSFiddle 吗?
您使用链接锚点和 JQuery。
只需为您的链接提供“scroll”类,并在头部使用以下代码:
$(function() {
// Listen for a click event on the anchor
$('.scroll').click(function(event) {
// Prevent the jump to target that is default browser behavior
event.preventDefault();
// Animate the scrollTop property of the scrollParent to the top offset
// of the target element. In this case, we have an animation duration of 1000ms(1 second).
$('html').animate({
scrollTop: $(this.hash).offset().top
}, 1000);
});
});
/* Just for demo purposes */
.post {
margin: 100vh 0;
background: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#anchor" class="scroll">Go To Div 8</a>
<div class="post" id="anchor">Scroll to me</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)