2017-04-21 95 views
0

只要用户的滚动位置“击中”特殊 - 其他 - 元素,我想要为该元素添加一个类。如果y位置大于另一个元素,则将类添加到元素

我尝试使用代码因此

var hands = $(".sw_3--breit"); 
    $(window).scroll(function() { 
    var scroll = $(window).scrollTop(); 
// The next line is the one I am asking for help 
     if (scroll >= window.innerHeight) 
     { 
      hands.addClass("fixed"); 
     } else { 
      hands.removeClass("fixed"); 
     } 
}); 

其中加入后级滚动越大,则用户显示高度我想工作良好。但是,当用户“击中”另一个元素时,我想添加 - 然后删除 - 一个类。

我问什么是什么 - 非常粗略的和愚蠢的我知道 - 这样的:

var other_elements_position = $(".other_element"().position; 
if (scroll >= other_elements_position) 

我怎样才能做到这一点?我已经使用jQuery来做其他事情了,所以使用jquery会让我觉得有意义。

谢谢!

+0

'$(“.. SW_3 - 半夏”)的位置;'我的选择和方法的怀疑。应用于它?你对'.position'有什么期望? – Jai

+0

当然是错的。我只想演示一下我想要的结果作为输出:$(“。other_element”)。y_position_of_that_object; – Daiaiai

回答

0

对于有像我这样有同样的问题的人,这个工作对我来说:

var hands = $(".sw_3--breit"); 
var hands_original = $(".sw_8").position(); 
var hands_off = $("#target_agentur").position(); 
var hands_corrected = (hands_original.top + 680) // here I add a small delay to the trigger of the "animation" 
$(window).scroll(function() { 
    var scroll = $(window).scrollTop(); 
    if (scroll >= hands_corrected && scroll <= hands_off.top) // I doublecheck against 2 heights 
{ 
     hands.addClass("fixed"); 
    } else { 
     hands.removeClass("fixed"); 
    } 
}); 
相关问题