jQuery滚动功能从特定高度开始

标签 jquery css scroll css-transitions

这其实是两个问题:

  1. 为什么从 display:none;display:block; 的 1s 转换不起作用?
  2. 如何使此滚动功能仅在从顶部滚动 200px 后发生,即在滚动过“红色框”之后发生?

非常感谢任何帮助,非常感谢!

$(window).scroll(function() {
    $('#menu').css('display', 'block');
    clearTimeout($.data(this, 'scrollTimer'));
    $.data(this, 'scrollTimer', setTimeout(function() {
       $('#menu').css('display', 'none');
    }, 1500));
});
html {
   padding:0;margin:0;
}
body {
    height:2000px
}
#redBox{ 
    height:200px;
    width:100%;
    background:red;
    float:left;
    color:white;
    text-align:center;
    line-height:8em;
    font-size:1.2em;
}
#menu {
    position:fixed;
    top:0;
    width: 100%;
    height: 200px;
    background: navy;
    opacity: .5;
    color:white;
    text-align:center;
    line-height:8em;
    font-size:1.2em;	
    display: none;
    -webkit-transition:all 1s;
    transition:all 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="redBox">scroll function should start below this box (height: 200px)</div>
<div id="menu">scroll menu</div>

最佳答案

1) 您可以使用 jQuery fadeInfadeOut

2) 或者

$(window).scroll(function () {
    var scrollTop = $(window).scrollTop();
    if (scrollTop > 200) {
        $('#menu').fadeIn('slow');
        clearTimeout($.data(this, 'scrollTimer'));
        $.data(this, 'scrollTimer', setTimeout(function () {
            $('#menu').fadeOut('slow');
        }, 1500));
    }else{
        clearTimeout($.data(this, 'scrollTimer'));
        $('#menu').fadeOut('slow');
    }
});

https://jsfiddle.net/uqpamt4z/

关于jQuery滚动功能从特定高度开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515370/

相关文章:

java - 为什么spring不使用ajax上传呢?

javascript - 如何创建带有复选框的 "check/uncheck all"?

javascript - 如何在不更改 textContent 值的情况下将字符附加到 SVG 文本元素?

javascript - 如何在select2中添加图标?

html - 使用 Html/CSS 滚动表格

javascript - 数据表 - 更新属性数据

javascript - 通过选项的属性值设置选定的选项

javascript - 你如何改变javascript slider 的大小

c++ - 滚动时Qt List Widget动态内容加载

jquery - 在使用 JQuery 加载时滚动到新页面上的片段标识符?