我试图做到这一点,以便当我位于页面顶部时 <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/