如何确定元素是否溢出(使用 jQuery)?

发布于 2024-11-27 00:39:11 字数 164 浏览 1 评论 0 原文

布局如下所示:

在此处输入图像描述

基本上我想要的是找出 ELEMENT 是否超出了 PAGE : )

我所知道的是页面宽度,它是固定的@ 900 px......

The layout looks like this:

enter image description here

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难愈 2024-12-04 00:39:11

计算元素的 width,然后获取其 left,最后将其减去页面的 width,即可得到溢出。

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>

示例如下: http://jsfiddle.net/jackJoe/Q5FdG/

Calculate 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/

温柔女人霸气范 2024-12-04 00:39:11

假设您的页面上有一些

,您可以判断它是否在视口之外,如下所示:

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');
}

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');
}
月亮邮递员 2024-12-04 00:39:11

您是否尝试过使用 CSS 来防止这种情况发生?

pre { word-wrap:break-word; }

遍历页面上的每个元素似乎有点过多,并且在繁忙的页面上会花费一些时间。这是可能的,但并不完全实用。

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.

诗酒趁年少 2024-12-04 00:39:11

这里的所有答案都没有检查元素是否位于top:负值left:负值

这就是为什么我在这里给出一个更正确或更精确的答案。

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>

检查小提琴

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文