This是一個非常好的答案,但這是您提到的視口插件的修改版本。
(function($) {
$.belowthefold = function(lookIn, elements, settings) {
var fold = $(lookIn).height() + $(lookIn).scrollTop();
return $(elements).filter(function(){
return fold <= $(this).offset().top - settings.threshold;
});
};
$.abovethetop = function(lookIn, elements, settings) {
var top = $(lookIn).scrollTop();
return $(elements).filter(function(){
return top >= $(this).offset().top + $(this).height() - settings.threshold;
});
};
$.rightofscreen = function(lookIn, elements, settings) {
var fold = $(lookIn).width() + $(lookIn).scrollLeft();
return $(elements).filter(function(){
return fold <= $(this).offset().left - settings.threshold;
});
};
$.leftofscreen = function(lookIn, elements, settings) {
var left = $(lookIn).scrollLeft();
return $(elements).filter(function(){
return left >= $(this).offset().left + $(this).width() - settings.threshold;
});
};
})(jQuery);
隨着HTML這樣的:
<div id="lookInMe">
<div class="peek"></div>
<div class="peek"></div>
[ ... ]
</div>
這樣稱呼它:
$.belowthefold("#lookInMe", ".peek", {threshold : 0}).text("Below");
$.abovethetop("#lookInMe", ".peek", {threshold : 0}).text("Above");
$.leftofscreen("#lookInMe", ".peek", {threshold : 0}).text("Left");
$.rightofscreen("#lookInMe", ".peek", {threshold : 0}).text("Right");
http://jsfiddle.net/daCrosby/vhF7x/1/
此代碼中有檢查,如果該元素在屏幕上visble,而不是DIV 。當您在div內滾動時,我的元素無法看到。編輯給定的代碼爲我工作也許是posibile? – Kimmax
@Kimmax *當你想檢查它們是否可見時? –
我在右邊有一個小信息框。所以當用戶滾動到下一個項目時,我想更新此信息框。因此,當仍然顯示3/4元素時觸發'事件'會很好,但在元素末尾觸發事件也會完成這項工作 – Kimmax