jquery - 仅在滚动时更改 div 的不透明度 - 静止时变回完全不透明度

标签 jquery css scroll opacity

我正在寻找一种方法来更改 div (.header) 的不透明度以仅在滚动时更改。

所以基本上,当您滚动时,它几乎变得不透明,但当您停止滚动时,它又变得完全可见。

我寻找了一些脚本并一直以这些类型的 jQuery 脚本结尾,但这只是我正在寻找的解决方案的一半:

$(window).scroll(function() {
    if ($(this).scrollTop() > 400) {
        $( ".header #background" ).fadeIn();
    } else {
        console.log('there');
        $( ".header #background" ).fadeOut();
    }
});

( http://jsfiddle.net/SEH5M/ )

有什么想法吗?

最佳答案

通过在每次滚动事件后使用超时检查(在本例中为 200 毫秒)以查看用户是否已停止滚动,您可以实现此效果。
fiddle :http://jsfiddle.net/SEH5M/276/

$( ".header #background" ).fadeIn(); //initial fadein
$(window).scroll(function() {
    if(!$( ".header #background" ).hasClass('transition')){ //avoid multiple fades
        $( ".header #background" ).stop().addClass('transition').fadeOut();
    }
    var position = $(window).scrollTop();
    setTimeout(function(){
        if(position - $(window).scrollTop() == 0){ //check if scroll position has changed

            $( ".header #background" ).stop().removeClass('transition').fadeIn();
        }
    }, 200);
});

关于jquery - 仅在滚动时更改 div 的不透明度 - 静止时变回完全不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159752/

相关文章:

javascript - 我如何更改 html 字符串?

php - 从ajax调用上传blueimp文件

css - 我可以将我的 CSS 背景定位到 "bottom minus x pixels"吗?

html - 使用百分比值创建 CSS 动画关键帧

javascript - html滚动: jump to end of page (horizontal) w/javascript?

jquery - 使用鼠标滚轮滚动不会触发 jQuery 悬停或鼠标悬停

jquery - 页脚动态增长时滚动页面

jquery - 使用ajax加载时div不调整高度

asp.net - 为什么元素在 jquery 中为 null,但在 document.getelementbyid() 中却存在

html - 将页面的一部分拉伸(stretch)到全高