好的,我使用以下代码来检查元素是否显示在屏幕上。
(function($) {
/**
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://teamdf.com/jquery-plugins/license/
*
* @author Sam Sehnert
* @desc A small plugin that checks whether elements are within
* the user visible viewport of a web browser.
* only accounts for vertical position, not horizontal.
*/
$.fn.visible = function(partial) {
var $t = $(this),
$w = $(window),
viewTop = $w.scrollTop(),
viewBottom = viewTop + $w.height(),
_top = $t.offset().top,
_bottom = _top + $t.height(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom;
return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
};
})(jQuery);
然而,我想使用这段代码来检查它是否在可滚动的元素中可见。具体来说,我用于主要内容的main标签。我该如何修改代码,使其适用于我的可滚动元素?我不太确定该怎么做。我已经尝试将$w变量更改为$('main'),但似乎会出现异常行为。