开发者

Detect if div is visible in browser window

开发者 https://www.devze.com 2023-04-08 19:05 出处:网络
I have multiple collapsible divs like so +--------------+ Div 1| +--------------+ Div 2| +--------------+

I have multiple collapsible divs like so

+--------------+
| Div 1        |
+--------------+
| Div 2        |
+--------------+
| Div 3        |
+--------------+

Now when I click on one of the divs it expands

Question: How do I dete开发者_如何学编程ct that expanded div was expanded over browser window (bottom border)?


You could try this, it's an untested idea off the top of my head so some modification may be needed.

var div1Height = $("#div1").height();
var div2Height = $("#div2").height();
var div3Height = $("#div3").height();
var windowSize = $(window).height();


//assign function for on click (you'll want to change this)
$("#div1, #div2, #div3").click(function(e){
if(div1Height > windowSize){
//assuming div1 is at the top
console.log("div 1 passing extents");
}
if((div2Height + parseInt($("#div2").position().top) > windowSize){
console.log("div 2 passing extents");
}
if((div3Height + parseInt($("#div3").position().top) > windowSize){
console.log("div 3passing extents");
}

});
0

精彩评论

暂无评论...
验证码 换一张
取 消