This là một câu trả lời rất hay, nhưng đây là phiên bản sửa đổi của plugin Viewport mà bạn đã đề cập.
(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);
Với HTML như thế này:
<div id="lookInMe">
<div class="peek"></div>
<div class="peek"></div>
[ ... ]
</div>
Gọi nó như thế này:
$.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/
mã này có kiểm tra nếu phần tử là visble trên màn hình, không phải trên div . Không thể thấy phần tử của tôi khi bạn cuộn bên trong div. Có lẽ nó là posibile để chỉnh sửa mã cho sẵn để làm việc cho tôi? – Kimmax
@Kimmax * khi * bạn có muốn kiểm tra xem chúng có hiển thị không? –
Tôi có một hộp thông tin nhỏ ở bên phải. Vì vậy, khi người dùng cuộn xuống mục tiếp theo, tôi muốn cập nhật hộp thông tin này. Vì vậy, nó sẽ là tốt để có được một 'sự kiện' kích hoạt khi 3/4 của phần tử vẫn được hiển thị, nhưng kích hoạt một sự kiện ở phần cuối của phần tử sẽ làm công việc quá – Kimmax