javascript - 隐藏 div 直到滚动

标签 javascript jquery html

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

相关文章:

javascript - 使用循环打印javascript的时间

html - 如何让行内 block 占据整行?

css - html页面中的自定义标签(如xml)?

javascript - 不允许 <> 特殊字符的正则表达式

javascript - HTML5本地存储支持的数据

javascript - 制作带有延迟的循环 slider

jquery - 将 html 附加到元素的更简洁的方法

php - 为什么我的 "Page [0]"不在我的网页中央?

javascript - Bootstrap 、模态对话框、shown.bs.modal 事件不会触发

javascript - ionic : Header overlaps content when subheader is hidden in tabs-based app in iOS