我認爲你必須使用事件偵聽器:
$('someselector').on('appear', function(event, $all_appeared_elements) {
// this element is now inside browser viewport
});
$('someselector').on('disappear', function(event, $all_disappeared_elements) {
// this element is now outside browser viewport
});
和jQuery通配符選擇,如「[ID^=的Lightroom]」選擇任何ID開始 「的Lightroom」。 然後,你必須設置的時間間隔與javascript函數的setInterval()重複代碼每n秒,清除它,當元素消失使用clearInterval()
檢查這些代碼可以幫助您:
var intervals = [];
$("[id^=LightRoom]").on('appear', function(event, $all_appeared_elements) {
// create an interval to repeat action every 1 second
// and store it inside interval array using id as key
var appeared = $(this).attr("id");
intervals[appeared] = setInterval(function(){
$.get('https://localhost/" + appeared + ".php', function(data) {
$(appeared).prop('src', data);
});
}, 1000);
});
$("[id^=LightRoom]").on('disappear', function(event, $all_disappeared_elements) {
// delete intervals for non-visible elements
var disappeared = $(this).attr("id");
clearInterval(intervals[disappeared]);
});
文檔http://morr.github.io/appear.html – madalinivascu
是的,不幸的是我無法改變它以適應我的需求。 – davinci