使用javascript、移动设备上的jquery平滑滚动到页面顶部或底部
我有一个一页的网站,在水平移动视图上分为两部分。所以我尝试创建一个滚动/滑动效果到页面的顶部或底部。我试过这个检测代码使用javascript、移动设备上的jquery平滑滚动到页面顶部或底部,javascript,mobile,scroll,smoothing,Javascript,Mobile,Scroll,Smoothing,我有一个一页的网站,在水平移动视图上分为两部分。所以我尝试创建一个滚动/滑动效果到页面的顶部或底部。我试过这个检测代码 var bottomofDoc = $(".main_container").height(); $(window).scroll(function(){ var cur_top = $(window).scrollTop(); if(top < cur_top){ direction = 'down';
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;
});
var bottomofDoc=$(“.main_容器”).height();
$(窗口)。滚动(函数(){
var cur_top=$(窗口).scrollTop();
如果(顶部<当前顶部){
方向=‘向下’;
$('html,body').animate({scrollTop:$(document.height()},600);
控制台日志(方向);
}否则{
方向=‘向上’;
$(“html,body”).animate({scrollTop:0},600);
控制台日志(方向);
}
顶部=当前顶部;
});
但它的工作原理是有车的,闪烁不定,当我滚动一次时,它会继续做很多次动作。
如何实现平滑滚动到顶部或底部的任何想法我没有单击@Brachu191。2.您可能希望引入一个标志和/或计时器,以便在一个仍在运行时不进行更多动画调用。我尝试了,但它没有帮助@CBroeWell如果您只提供了这些,我们将无法帮助您。向我们展示您的尝试以及结果。
$(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;
}
}
});
});