[英]Smooth scroll to top or bottom of page with javascript,jquery on mobile
我有一个单页网站,在水平移动视图上分为两个部分。 因此,我尝试在页面顶部或底部创建滚动/滑动效果。 我尝试了此检测代码
var bottomofDoc = $(".main_container").height();
$(window).scroll(function(){
var cur_top = $(window).scrollTop();
if(top < cur_top){
direction = 'down';
$('html,body').animate({scrollTop: $(document).height()}, 600);
console.log(direction);
}else{
direction = 'up';
$("html, body").animate({ scrollTop: 0 }, 600);
console.log(direction);
}
top = cur_top;
});
但是它的工作原理是越野车,带有闪烁,当我滚动一次时,它将继续执行很多次操作。 任何想法如何实现平滑滚动到顶部或底部
$(function(){
$('a[href*="#"]:not([href="#"])').click(function(){
if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname){
var target = $(this.hash);
target = target.length ? target:$('[name=' + this.hash.slice(1) + ']');
if(target.length){
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.