[英]Determine the id of the div that has been scrolled to bottom
简单的问题,简单的观点,无法在SO中找到答案。 当用户滚动到div底部时,下面的javascript函数会发出警报。 我有很多div,但是我只希望在div id = imageloc滚动时运行警报。
$(window).scroll( function() {
if ( document.documentElement.clientHeight + $( document ).scrollTop() >= document.body.offsetHeight ) {
if (this.id == "imageloc" ) {
alert("bottom of the page.");
}
}
});
当用户滚动到div底部时,下面的javascript函数会发出警报。 我有很多div,但是我只希望在div id = imageloc滚动时运行警报。
也许我误会了,但它的用语就像您有许多可滚动的div,并且如果其中之一滚动到底部,则想做一些事情。 然后,您应该在此特定的div上监听scroll
事件:
$("#imageloc").scroll( function(e) {
if ( this.scrollTop >= this.scrollHeight - this.offsetHeight ) {
console.log( 'bottom' )
}
});
如果您想在文档滚动到特定div的底部之外时执行某些操作,请尝试以下操作: http : //jsfiddle.net/aheh5/1/ (但是,每次用户滚动窗口并# imageloc在视口上方)。
这样使用
$(window).scroll(function() {
var winTop = $(this).scrollTop();
var $divs = $('div');
var top = $.grep($divs, function(item) {
return $(item).position().top <= winTop;
});
alert($(top).attr('id'));
});
你可以尝试这样
var imgLocTop = $('#imageloc').offset().top ;
var $window = $(window);
var limit = Math.abs($window.height() - imgLocTop);
$window.scroll( function() {
if($window.scrollTop() >= limit) {
alert('Reached the required div');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.