[英]How can I scroll to the div automatically when I click the button?
我在页面顶部有一个按钮,在中间有4个表,在页面底部有一个div(id =“ MasterDiv”)。 单击按钮时如何自动滚动到div? 谢谢
您可以使用此函数(使用jQuery的.animate
)
function scrollToElement(selector, callback){
var animation = {scrollTop: $(selector).offset().top};
$('html,body').animate(animation, 'slow', 'swing', function() {
if (typeof callback == 'function') {
callback();
}
callback = null;
});
}
您这样称呼它:
scrollToElement('#MasterDiv');
或者,如果您要包含回调:
scrollToElement('#MasterDiv', function(){
alert('Page scrolled');
});
Example
A named anchor inside an HTML document:
<a name="tips">Useful Tips Section</a>
Create a link to the "Useful Tips Section" inside the same document:
<a href="#tips">Visit the Useful Tips Section</a>
Or, create a link to the "Useful Tips Section" from another page:
<a href="http://www.w3schools.com/html_links.htm#tips">
Visit the Useful Tips Section</a>
您可以尝试以下库: http : //demos.flesler.com/jquery/scrollTo/
没有动画基本的Javascript示例(使用ID代替name属性)
window.location = "#MasterDiv";
jQuery动画示例
$.scrollTo("#MasterDiv");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.