【问题标题】:Javascript add class on scroll with timeoutJavascript在超时滚动时添加类
【发布时间】:2018-10-31 16:33:14
【问题描述】:

当指定的类滚动到视图中时,我在添加类时遇到了一些问题,并且在添加类之间存在延迟。

我有 3 个并排的 div,都带有在 javascript 代码中看到的类 fadeInDownScroll。

onScrollReach 函数有效,并且当滚动到达时添加了类 fadeInDown,但是所有三个 div 都同时添加了这个。

下面是我一直在尝试工作的 javascript 代码:

function onScrollReach(selector, classToBeAdded, offset, delayTime, callback) {
    var didScroll = false;
    var this_top;
    var height;
    var top;

    //If no offset, set one as 0 so that its initialised
    if(!offset) { var offset = 0; } 
    $(window).scroll(function() {
        didScroll = true;
    });
    //Set interval of a tenth of a second, so this will trigger 10 times a second
    setInterval(function() {
        //If they've scrolled within the last 1/10th of a second
        if (didScroll) {

        //Prevent retrigger by setting false
        didScroll = false;
        //Get scroll height
        top = $(this).scrollTop();
        //For each of the selector element (class you're looking for)
        $(selector).each(function(i){
            //Set position of where on page you want it to trigger the event
            this_top = $(this).offset().top - offset;
            height   = $(this).height();

            // Scrolled within current section & doesn't already have the class
            if (top >= this_top && !$(this).hasClass(classToBeAdded)) {
            //=$(this).addClass(classToBeAdded);

            setTimeout(function(){
                console.log(delayTime * (i / 2));
                console.log('class added');
                $(this).addClass(classToBeAdded);
            }, 100);
            //You can call it with a function so tha tyou can do something else straight after
            //This only applies if thats the case
            if (typeof callback == "function") callback(selector);
            }
        });
        }
    }, 100);
}

//Target Class, Class to be added, Offset for scroll, Delay Time
onScrollReach(".fadeInDownScroll", "fadeInDown", 600, 3000, '');

我知道这是一个有很多类似帖子的主题,但是在阅读了很多帖子后,我找不到解决这个确切问题的帖子

【问题讨论】:

  • 这里有一些代码味道。首先,有一个时间间隔来判断用户滚动是否看起来很奇怪,因为您有一个滚动事件处理程序。其次,每个循环中的this_topheight 不限于闭包。因此,对于您迭代的每个选择器,它们都是相同的。
  • @Taplar 感谢您的评论,我是 Javascript 新手,所以我可能有很多可以改进的地方。读完这篇文章后,我在滚动上选择了 setInterval:benmarshall.me/attaching-javascript-handlers-to-scroll-events。你能给我关于关闭评论的进一步解释吗?如果它们在每个引用“this”的函数中,我很难理解为什么它们会是相同的
  • 顶一下 - 有人知道这方面的更多信息吗?

标签: javascript jquery scroll settimeout onscroll


【解决方案1】:

试试这个更新的代码

function onScrollReach(selector, classToBeAdded, offset, delayTime, callback) {
    var didScroll = false;
    var this_top;
    var height;
    var top;

    //If no offset, set one as 0 so that its initialised
    if(!offset) { var offset = 0; } 
    $(window).scroll(function() {
        didScroll = true;
    });
    //Set interval of a tenth of a second, so this will trigger 10 times a second
    setInterval(function() {
        //If they've scrolled within the last 1/10th of a second
        if (didScroll) {

        //Prevent retrigger by setting false
        didScroll = false;
        //Get scroll height
        top = $(this).scrollTop();
        //For each of the selector element (class you're looking for)
            //Set position of where on page you want it to trigger the event
            this_top = $(this).offset().top - offset;
            height   = $(this).height();

            // Scrolled within current section & doesn't already have the class
            if (top >= this_top && !$(this).hasClass(classToBeAdded)) {
            //=$(this).addClass(classToBeAdded);

            setTimeout(function(){
                console.log('class added');
                $(selector).addClass(classToBeAdded);
            }, 100);
            //You can call it with a function so tha tyou can do something else straight after
            //This only applies if thats the case
            if (typeof callback == "function") callback(selector);
            }
        }
    }, 100);
}

//Target Class, Class to be added, Offset for scroll, Delay Time
onScrollReach(".fadeInDownScroll", "fadeInDown", 600, 3000, '');

【讨论】:

  • 感谢您的回复。所有三个 div 都同时添加了新类。拜托,你能解释一下为什么你把这个都拿出来了吗?我之所以包含 each 是为了让我可以在同一页面上的其他 div 上使用这个类,而不会在第一次滚动类时触发将类添加到每个 div
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
相关资源
最近更新 更多