admin管理员组文章数量:1194125
The layout looks like this:
Basically all I want is to find out if the ELEMENT went outside the PAGE :)
All I know is the page width, which is fixed @ 900 px...
The layout looks like this:
Basically all I want is to find out if the ELEMENT went outside the PAGE :)
All I know is the page width, which is fixed @ 900 px...
Share Improve this question edited Jul 28, 2017 at 10:43 Titouan de Bailleul 12.9k11 gold badges69 silver badges122 bronze badges asked Jul 28, 2011 at 21:58 AlexAlex 68.4k185 gold badges459 silver badges650 bronze badges 2 |4 Answers
Reset to default 13Calculate the element's width
, then get its left
, finally subtract it to the page's width
and you'll get the overflow.
var pageWidth = $(".page").width();
var elementWidth = $(".element").width();
var elementLeft = $(".element").position().left;
if (pageWidth - (elementWidth + elementLeft) < 0) {
alert("overflow!");
} else {
alert("doesn't overflow");
}
.page {
overflow: hidden;
width: 200px;
height: 200px;
position: relative;
background: black;
}
.element {
width: 100px;
height: 100px;
position: absolute;
background: blue;
top: 10px;
left: 150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="page">
<div class="element">
</div>
</div>
Example here: http://jsfiddle.net/jackJoe/Q5FdG/
Assuming you have some <div id="elem"></div>
on your page, you could tell if it is outside of the viewport like this:
var $elem = $('#elem'),
top = $elem.offset().top,
left = $elem.offset().left,
width = $elem.width(),
height = $elem.height();
if (left + width > $(window).width()) {
alert('Off page to the right');
}
if (top + height > $(window).height()) {
alert('Off page to the bottom');
}
Have you tried using CSS to prevent it from happening?
pre { word-wrap:break-word; }
To iterate through every element on the page seems excessive, and it will take some time on a busy page. It is possible but not entirely practical.
All the answers here have not checked if element is at top: negative value
or left: negative value
.
So that's why, I am giving a more correct or precise answer here.
var pageWidth = $(".page").width();
var pageHeight = $(".page").height();
var pageTop = $(".page").offset().top;
var pageLeft = $(".page").offset().left;
var elementWidth = $(".element").width();
var elementHeight = $(".element").height();
var elementTop = $(".element").offset().top;
var elementLeft = $(".element").offset().left;
if ((elementLeft >= pageLeft) && (elementTop >= pageTop) && (elementLeft + elementWidth <= pageLeft + pageWidth) && (elementTop + elementHeight <= pageTop + pageHeight)) {
// Its inside
alert('Inside');
} else {
//
alert('Outside');
}
.page {
overflow: hidden;
width: 200px;
height: 200px;
position: relative;
background: black;
}
.element {
width: 100px;
height: 100px;
position: absolute;
background: blue;
top: -10px;
left: -10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="page">
<div class="element">
</div>
</div>
Check Fiddle
本文标签: javascriptHow to find out if an element overflows (with jQuery)Stack Overflow
版权声明:本文标题:javascript - How to find out if an element overflows (with jQuery)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738472309a2088659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<pre>
element which sometimes can have longer lines, and they go beyond the page. There can be multiple elements on the page – Alex Commented Jul 28, 2011 at 22:02