替代(window).scroll [英] alternative to (window).scroll

查看:152
本文介绍了替代(window).scroll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
ScrollTop在Chrome中确实很生涩

Possible Duplicate:
ScrollTop really jerky in Chrome

我正在使用以下代码获取返回顶部的按钮,并在用户滚动后淡出导航.问题是它在每次滚动时都会触发,因此导致滚动真的很生涩.是否有另一种方法可以执行该功能一次?

I'm using the following code to get a back to top button and a navigation to fade in once the user scrolls. The problem is its triggering every time you scroll, therefore causing the scrolling to be really jerky. Is there an alternate way to do this, which would maybe trigger the function only once?

    $(function () {
    $(window).scroll(function () {
        if ($(this).scrollTop() > 600) {
            $('#backToTop, #navigation').fadeIn();
        } else {
            $('#backToTop, #navigation').fadeOut();
        }
    });
    }); 

   });

推荐答案

也许设置超时时间...

Maybe set a timeout...

function scrollit(){  
    if ($(this).scrollTop() > 600) {
    $('#backToTop').fadeIn();
    } else {
    $('#backToTop').fadeOut();
    }
     }

var timer;

$(window).scroll(function () {

window.clearTimeout(timer);
timer = window.setTimeout(function(){ scrollit(); }, 2000);

    }); 

这篇关于替代(window).scroll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆