找到我正在滚动的当前元素的索引 - Jquery

时间:2013-01-25 15:08:16

标签: javascript jquery indexing scroll

假设我有一些固定高度的自动滚动启用的div,类名为“class”。我想找到我正在滚动的当前'class'的div的索引,如下所示:

var type = -1;
$(window).scroll(function(type_request){
    type = $('.class').index($(this));console.log(type);
});

但滚动后我没有输出。

1 个答案:

答案 0 :(得分:1)

看起来您的方法调用已被撤消。试试这个:

$(window).scroll(function(){
    type = $(this).index();
    console.log(type);
});

编辑:如果你只想用类.class滚动div,那么你需要特别绑定:

$('.class').scroll(function(){
    type = $(this).index();
    console.log(type);
});

此外,如果你使用的是jQuery 1.7+,那么你应该使用on而不是scroll(这是bind('scroll')的简写):

$('.class').on('scroll', function(){
    type = $(this).index();
    console.log(type);
});