我正在努力做到这一点,这样当我在页面顶部<DIV>
时,它是不可见的,但是每当我滚动到它时,它就会弹出一个链接,回到页面顶部。使用 JavaScript 会更容易,还是有办法只使用 HTML?
问问题
7946 次
1 回答
9
那么没有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
});
您仍然可以在此处使用“位置:固定”,因为没有可见性,用户无法看到或单击它。
于 2013-01-04T14:06:49.833 回答