隐藏div直到滚动

时间:2013-01-04 13:52:21

标签: javascript jquery html

我正在尝试这样做,以便当我在页面的顶部时<DIV>是不可见的,但每当我滚动到它时,它会弹出一个链接,直接返回到顶部。页。使用JavaScript会更容易吗?还是有办法只使用HTML?

1 个答案:

答案 0 :(得分:8)

没有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”,因为没有可见性,用户无法看到或点击它。

相关问题