Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery检查元素在可滚动div中是否可见_Jquery_Scroll_Visible - Fatal编程技术网

jQuery检查元素在可滚动div中是否可见

jQuery检查元素在可滚动div中是否可见,jquery,scroll,visible,Jquery,Scroll,Visible,好的,我使用下面的代码来检查元素在屏幕上是否可见 (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 ele

好的,我使用下面的代码来检查元素在屏幕上是否可见

(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);
(函数($){
/**
*版权所有2012,数字融合
*根据麻省理工学院许可证授权。
* http://teamdf.com/jquery-plugins/license/
*
*@作者Sam Sehnert
*@desc一个检查元素是否在
*web浏览器的用户可见视口。
*只考虑垂直位置,不考虑水平位置。
*/
$.fn.visible=函数(部分){
var$t=$(此),
$w=$(窗口),
viewTop=$w.scrollTop(),
viewBottom=viewTop+$w.height(),
_top=$t.offset().top,
_底部=_顶部+$t.高度(),
compareTop=partial==true?\u底部:\u顶部,
compareBottom=部分===真?\u顶部:\u底部;
返回((compareBottom=viewTop));
};
})(jQuery);
但是,我想使用这段代码,以便它检查它在可滚动元素中是否可见。特别是我用于主要内容的主标签。我将如何更改此代码以使其适用于我的可滚动元素? 我不太清楚该怎么办。我已经尝试将$w变量更改为$('main'),但这似乎有点奇怪

但是,我想使用这段代码,以便它检查它在可滚动元素中是否可见


该插件仅限于检测身体的直接子对象。这几乎使插件无法检测到任何嵌套元素。看这个

我明白了,这是一个还不可能的限制?有没有其他方法可以实现我想实现的目标,或者我被卡住了?也许有一个不同的插件支持这个功能?我不知道有哪一个插件支持这个功能,但我记得有一个插件是基于这个插件的。也许读这篇文章会让你更好地理解并更接近你的目标。同时,我将尝试挖掘一个基于可见性和滚动的旧项目。