我想在元素被点击时使页面向下滚动150像素,即使当前位置已经滚动了一半。例如,您大约已经滚动了一半的页面,您单击该链接,它将再向下滑动150个像素。
使用jQuery可以实现吗?
我一直在研究scrollTop和scrollTo插件,但似乎无法理清思路。
我想在元素被点击时使页面向下滚动150像素,即使当前位置已经滚动了一半。例如,您大约已经滚动了一半的页面,您单击该链接,它将再向下滑动150个像素。
使用jQuery可以实现吗?
我一直在研究scrollTop和scrollTo插件,但似乎无法理清思路。
var y = $(window).scrollTop(); //your current y position on the page
$(window).scrollTop(y+150);
只需要检查这个:
$(document).ready(function() {
$(".scroll").click(function(event){
$('html, body').animate({scrollTop: '+=150px'}, 800);
});
});
当您点击元素时,它将使滚动条从当前位置滚动。
而150px用于向下滚动150px。
+=
对我解决了一个类似的问题非常有帮助。 - NetOperator Wibby'+=150px'
真的很棒,它既简洁又清晰。太喜欢了! - Joshua Pinter.animate()
之前,调用.stop()
可能有助于防止两个滚动动画同时运行(这将导致闪烁效果)。 - Thomas Kekeisenanimate
来实现此功能,具体操作请参考以下链接:http://blog.freelancer-id.com/index.php/2009/03/26/scroll-window-smoothly-in-jquery
如果您想使用 scrollTo
插件来完成,请参考以下链接:How to scroll the window using JQuery $.scrollTo() function
纯JS解决方案,适合新手或其他人使用。
var scrollAmount = 150;
var element = document.getElementById("elem");
element.addEventListener("click", scrollPage);
function scrollPage() {
var currentPositionOfPage = window.scrollY;
window.scrollTo(0, currentPositionOfPage + scrollAmount);
}
避免冲突的HCD解决方案的更新版本:
var y = $j(window).scrollTop();
$j("html, body").animate({ scrollTop: y + $j(window).height() }, 600);