jQuery 检查元素在 scrollable 中是否可见 div
jQuery check if element is visible inside scrollable div
好的,所以我使用以下代码来检查元素是否在屏幕上可见。
(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);
但是,我想使用这段代码来检查它是否在可滚动元素内可见。特别是我用于主要内容的主要标签。我将如何更改此代码以使其适用于我的可滚动元素?
我不太确定该怎么做。我已经尝试将 $w 变量更改为 $('main') 但这似乎很奇怪。
However, I would like to use this piece of code so that it checks whether it is visible inside a scrollable element.
该插件仅限于检测 body 的直接子项。这几乎使插件无法检测到任何嵌套元素。看到这个 explanation。
好的,所以我使用以下代码来检查元素是否在屏幕上可见。
(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);
但是,我想使用这段代码来检查它是否在可滚动元素内可见。特别是我用于主要内容的主要标签。我将如何更改此代码以使其适用于我的可滚动元素? 我不太确定该怎么做。我已经尝试将 $w 变量更改为 $('main') 但这似乎很奇怪。
However, I would like to use this piece of code so that it checks whether it is visible inside a scrollable element.
该插件仅限于检测 body 的直接子项。这几乎使插件无法检测到任何嵌套元素。看到这个 explanation。