Jquery检查元素在视口中是否可见
问题描述:
检查div类media是否在浏览器可视视口内,而不管窗口滚动位置如何。
Function to check if the div class "media" is within the browsers visual viewport regardless of the window scroll position.
<HTML>
<HEAD>
<TITLE>My first HTML document</TITLE>
</HEAD>
<BODY>
<div class="main">
<div class="media"></div>
</div>
</BODY>
</HTML>
尝试使用此插件 https://github.com/customd/jquery-visible 使用此功能,但
我不知道如何使其工作。
Trying to use this plugin https://github.com/customd/jquery-visible with this function but I don't know how to make it work.
$('#element').visible( true );
答
嗯,你是怎么试图让它起作用的?根据该插件的文档, .visible()
返回一个布尔值,指示元素是否可见。所以你会这样使用它:
Well, how did you try to make it work? According to the documentation for that plugin, .visible()
returns a boolean indicating if the element is visible. So you'd use it like this:
if ($('#element').visible(true)) {
// The element is visible, do something
} else {
// The element is NOT visible, do something else
}