javascript - jquery实现鼠标滚动时元素消失,不滚动的时候元素回来?
ringa_lee
ringa_lee 2017-04-10 17:26:10
[JavaScript讨论组]

我想实现的功能:

对于透明的那一块,我想在鼠标滚动的时候消失,但是鼠标不滚动的时候不消失。

但是如果利用scroll事件来监听滚动,如果我滚动的时候可以让它消失,但我不滚动的时候它也回不来了

$(function(){
    $(window).scroll(function(e) {
        $("#header").hide();
    });
});

html代码

<html>
<head>
<script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.2.1/jquery.min.js"></script>

<script>
$(function(){
    $(window).scroll(function(e) {
        $("#header").hide();
    });
});
</script>

<style>
.main
{
    width: 700px;
    margin: 200px auto;
}
.header
{
position:fixed;
width:100%;
height:150px;
top:0px;
left:0px;
background: rgb(255, 255, 255);
background: rgba(255, 255, 255, 0.6);
background: transparent\9;
border: 1px solid #ccc;
}
</style>
</head>

<body>
<p class="header" id="header">
<p id="main" class="main">
Hello World
</p>
</body>
</html>

类似于知乎专栏,知乎专栏是鼠标向下滚动的时候不显示作者的信息,鼠标向上滚动的时候显示作者信息

ringa_lee
ringa_lee

ringa_lee

全部回复(2)
怪我咯

scroll() 方法的扩展

/*  
    对scroll()方法进行扩展
    在jQuery 中scroll()只可以监听滚动的时候的一个事件
    这段js可以监听scrollStart和scrollStop
*/
(function(){

    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);

    special.scrollStart = {
        setup: function() {
            var timer,
                handler =  function(evt) {
                    var _self = this,
                        _args = arguments;

                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = 'scrollStart';
                        jQuery.event.handle.apply(_self, _args);
                    }

                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollStop.latency);

                };

            jQuery(this).on('scroll', handler).data(uid1, handler);

        },
        teardown: function(){
            jQuery(this).off( 'scroll', jQuery(this).data(uid1) );
        }
    };

    special.scrollStop = {
        latency: 300,
        setup: function() {

            var timer,
                    handler = function(evt) {

                    var _self = this,
                        _args = arguments;

                    if (timer) {
                        clearTimeout(timer);
                    }

                    timer = setTimeout( function(){

                        timer = null;
                        evt.type = 'scrollStop';
                        jQuery.event.handle.apply(_self, _args);

                    }, special.scrollStop.latency);

                };

            jQuery(this).on('scroll', handler).data(uid2, handler);

        },
        teardown: function() {
            jQuery(this).off( 'scroll', jQuery(this).data(uid2) );
        }
    };

})();

调用方法:

(function(){
    jQuery(window).on('scrollStart', function(){
        console.log("start");
    });

    jQuery(window).on('scrollStop', function(e){
        console.log("end");
    });
})();
PHP中文网

在函数内部添加判断,你只设定了隐藏,但是隐藏后他不会再出现,你要在最外部的函数添加判断,else{$("#header")。show()}

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号