gpt4 book ai didi

javascript - 从页面底部滚动到顶部

转载 作者:行者123 更新时间:2023-11-29 17:31:20 26 4
gpt4 key购买 nike

我想知道是否在线有任何 jquery 插件可以帮助我在底部滚动页面。

我的意思是,当我将页面滚动到底部时,我想要一个出现的按钮并单击它我可以返回到页面顶部

有什么建议吗?

最佳答案

How to tell when you're at the bottom of the page :

if (  document.documentElement.clientHeight + 
$(document).scrollTop() >= document.body.offsetHeight ) {
...
}

滚动到页面顶部的方法如下:

$('html, body').animate({scrollTop:0}, 'slow');

以下是当您点击页面底部时如何将这些组合起来并淡入淡入以滚动到页面顶部的方法(链接只有在您单击它时才会重置,因为这对我来说更有意义) .

这利用了 .animate() 。在 4 个可能的参数中,我使用了 3 个:属性、持续时间和回调。

jsFiddle example


$(function() {
$(window).scroll(function() {
var totalHeight, currentScroll, visibleHeight;
// How far we've scrolled
currentScroll = $(document).scrollTop();
// Height of page
totalHeight = document.body.offsetHeight;
// Height visible
visibleHeight = document.documentElement.clientHeight;
// If visible + scrolled is greater or equal to total
// we're at the bottom
if (visibleHeight + currentScroll >= totalHeight) {
// Add to top link if it's not there
if ($("#toTop").length === 0) {
var toTop = $("<a>");
toTop.attr("id", "toTop");
toTop.attr("href", "#");
toTop.css("display", "none");
toTop.html("Scroll to Top of Page");
// Bind scroll to top of page functionality
toTop.click(function() {
// Use animate w properties, duration and a callback
$('html, body').animate({scrollTop:0}, 'slow', function() {
$("#toTop").remove();
});
return false;
});
$("body").append(toTop);
$("#toTop").fadeIn(3000);
}
}
});
});

关于javascript - 从页面底部滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com