jquery onscroll hasClass propery不起作用

时间:2015-06-03 05:37:29

标签: jquery html

在我的模板中,我跟踪了request.out.print "Content-Type: text/plain\n\nHello from #{__FILE__}" up scroll

在向上滚动时,我向down scroll添加了一些class names。在h2 Downscroll如果h2有类名,我就完成了一些任务。

Downscroll我使用了hasClass属性。但它没有动态添加class names

这是我用过的脚本

    $(document).ready( function() {

var lastScrollTop = 0;

$(window).on('scroll', function(e){
    var st = $(this).scrollTop();

        if (st > lastScrollTop){ //down scroll

                $(".rightclassname h2").each(function(){
                    var moveright = $(this).offset().top 
                    if($(window).scrollTop() > moveright && !$(this).hasClass('reached')) {
                        $(this).addClass('reached');
                        console.log(moveright);
                    }
                })

                $(".leftclassname h2").each(function(){
                    var moveleft = $(this).offset().top ;
                    if($(window).scrollTop() > moveleft && !$(this).hasClass('reached'))
                    { 
                        console.log('Moveleft');
                        $(this).addClass('reached');
                    }
                })
        }
        else { //Up scroll

                $(".rightclassname h2").each(function(){
                    var moveright = $(this).offset().top   ;

                    if($(window).scrollTop() < moveright && !$(this).hasClass('reached')) {
                        $(this).addClass('reached');
                        console.log('right up ');
                    }
                })


                $(".leftclassname h2").each(function(){
                    var moveleft = $(this).offset().top  ;
                    if($(window).scrollTop() < moveleft && !$(this).hasClass('reached'))
                    { 
                        $(this).addClass('reached');
                        console.log('leftup');
                    }
                })
        }
        lastScrollTop = st; 
    })
})

此代码在down scroll中正常运行。但是在up scroll中,hasclass属性未跟踪动态添加的类reached。所以它运作不正常。

请在此处帮助我跟踪hasClass属性。

****请参考这个小提琴Refer the fiddle here **

downscroll时,通过正确显示的相应标题交叉显示。到upscroll它不起作用。**

1 个答案:

答案 0 :(得分:0)

你已将'each'格式化为'map',我偶然会这样做......但我认为它应该是这样的:

var obj = {
  "flammable": "inflammable",
  "duh": "no duh"
};
$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>