简体   繁体   English

如何知道我的元素是否溢出

[英]How to know if my element is overflowed

I have a slider that contains N elements. 我有一个包含N个元素的滑块。 Each element will by translated by N pixels when the user click on the next button. 当用户点击下一个按钮时,每个元素将按N个像素平移。 When the element is out of the wrapper div, it disappears because it is overflowed by another element. 当元素超出包装div时,它会因为被另一个元素溢出而消失。

My plugin does not use any margins, just the transform property. 我的插件不使用任何边距,只使用转换属性。

I would like to know if there is a way to know if my element is out of the div. 我想知道是否有办法知道我的元素是否超出了div。 :visible does not work for my problem because the element is already visible but overflowed. :visible对我的问题不起作用,因为该元素已经可见但溢出。

By determine parent width and get child width then use if condition 通过确定parent width并获得child width然后使用if condition

if($('span').width() > divWidth){
alert('Overflowed!');
// do something!
}

jsFiddle Demo jsFiddle演示

if you update your question with your html then I can update with your codes. 如果你用你的html更新你的问题,那么我可以用你的代码更新。

If I understand correctly, one way to do it would be to compare the position of this element to the size (width/height or both) of his parent. 如果我理解正确,一种方法是将这个元素的位置与他父母的大小(宽度/高度或两者)进行比较。

With Jquery you could do it this way: 使用Jquery,你可以这样做:

<script>
  //This is the position of the right side of the element 
  //relative to his parent
  var rightPos = $("#element").position().left + $("#element").width();
  //And bottom side
  var botPos = $("#element").position().top + $("#element").height();
  if (rightPos > $("#element").parent().width()) {
    //The element is outside the right limit of the the parent block
  } else if (botPos > $("#element").parent.height()) {
    //It's outside the bottom limit of the parent block
  }
</script>

If it's not the parent you could then adapt this code to compare the position to the width of the correct div, preferably by using the jquery offset() method instead of position(). 如果它不是父级,则可以调整此代码以将位置与正确div的宽度进行比较,最好使用jquery offset()方法而不是position()。

You could give the wrapper div the CSS property of overflow: hidden 您可以为包装器div提供overflow:hidden的CSS属性

This would mean that any elements inside of it are not visible when they leave the bounds of the wrapper. 这意味着当它们离开包装器的边界时,它内部的任何元素都是不可见的。

Otherwise you could check whether your element is outside of the wrapper div using jQuery to compare the position to that of the parent. 否则,您可以使用jQuery检查您的元素是否在包装div之外,以将位置与父级的位置进行比较。

There is a nice tool for testing if an element is visible on the screen. 有一个很好的工具可以测试元素在屏幕上是否可见。

Detect if a DOM Element is Truly Visible 检测DOM元素是否真正可见

It looks at an object and checks each of its parents to see if it's still visible to the user. 它查看一个对象并检查其每个父对象以查看它是否仍然对用户可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM