gpt4 book ai didi

javascript - 隐藏 div 直到滚动

转载 作者:行者123 更新时间:2023-11-28 11:37:55 25 4
gpt4 key购买 nike

我试图做到这一点,以便当我位于页面顶部时 <DIV>是不可见的,但每当我滚动到它时,它就会弹出一个链接,返回页面顶部。使用 JavaScript 会更容易吗?还是有办法只用 HTML 来实现?

最佳答案

好吧,(据我所知)没有 Javascript 就没有办法做到这一点。

假设 HTML 像这样:

<button id="top">Top</button>

您可以使用以下代码:

$(window).scroll(function() { // when the page is scrolled run this
if($(this).scrollTop() != 0) { // if you're NOT at the top
$('#top').fadeIn("fast"); // fade in
} else { // else
$('#top').fadeOut("fast"); // fade out
}
});

$('#top').click(function() { // when the button is clicked
$('body,html').animate({scrollTop:0},500); // return to the top with a nice animation
});

您仍然可以在此处使用“position:fixed”,因为在不可见的情况下,用户无法看到或单击它。

关于javascript - 隐藏 div 直到滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14158459/

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