0

我正在尝试访问下面闭包内的 jQuery 对象的选择器,这样我就不必指定它和/或缓存它。如果我用 $this 替换 $(".the_lead"),它将不会执行它的操作。

调用插件

$(".the_lead").scroll_lead({speedup: 400});

堵塞

var $this = $(this);

$(window).scroll(function() {

    var window_height = $(window).height();
    var document_height = $(document).height();
    var hide_lead;
    var scrollTop = $(window).scrollTop();

    console.log($this);

    if(!hide_lead){

        if(scrollTop>(document_height/2)){
            $(".the_lead").slideDown(options.speedup);
            }else{
            $(".the_lead").slideUp(500,function(){
                $(".the_lead").hide();
          });}
         }


        $('#hide_lead').click(function(e){
            //$(".the_lead").parent().parents('div').hide();
            hide_lead = true;           
            e.preventDefault();
        });     

    }); 

$(this) 的控制台输出:

[selector: ".the_lead", context: document, constructor: function, init: function, selector: ""…]
context: #document
length: 0
selector: ".the_lead"
__proto__: Object[0]
4

1 回答 1

1

注意length: 0控制台的输出。窗口滚动事件或多或少地触发用户移动的每个像素。如果没有看到更多插件代码,我无法确定,但我敢打赌 $this 仅在第一次触发滚动事件时是正确的。

此外,缓存似乎您只想缓存 .the_lead 元素之一,不是吗?这就是您的代码的读取方式。但是在函数中,$this = $(this);您似乎希望所有元素都带有 class .the_lead。尝试像这样直接缓存它。$this = $('.the_lead');

于 2013-02-08T16:15:38.183 回答