检测元素是否溢出的最简单方法是什么?
我的用例是,我想限制某个内容框的高度为300px。如果内部内容比这高,我用溢出切断它。但如果它已满,我想显示一个'more'按钮,但如果没有,我不想显示该按钮。
是否有一种简单的方法来检测溢出,或者有更好的方法?
检测元素是否溢出的最简单方法是什么?
我的用例是,我想限制某个内容框的高度为300px。如果内部内容比这高,我用溢出切断它。但如果它已满,我想显示一个'more'按钮,但如果没有,我不想显示该按钮。
是否有一种简单的方法来检测溢出,或者有更好的方法?
当前回答
如果您只想显示更多内容的标识符,那么您可以使用纯CSS来实现这一点。我使用纯滚动阴影。诀窍在于使用background-attachment: local;。你的css看起来是这样的:
.scrollbox { overflow: auto; width: 200px; max-height: 200px; margin: 50px auto; background: /* Shadow covers */ linear-gradient(white 30%, rgba(255,255,255,0)), linear-gradient(rgba(255,255,255,0), white 70%) 0 100%, /* Shadows */ radial-gradient(50% 0, farthest-side, rgba(0,0,0,.2), rgba(0,0,0,0)), radial-gradient(50% 100%,farthest-side, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%; background: /* Shadow covers */ linear-gradient(white 30%, rgba(255,255,255,0)), linear-gradient(rgba(255,255,255,0), white 70%) 0 100%, /* Shadows */ radial-gradient(farthest-side at 50% 0, rgba(0,0,0,.2), rgba(0,0,0,0)), radial-gradient(farthest-side at 50% 100%, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%; background-repeat: no-repeat; background-color: white; background-size: 100% 40px, 100% 40px, 100% 14px, 100% 14px; /* Opera doesn't support this in the shorthand */ background-attachment: local, local, scroll, scroll; } <div class="scrollbox"> <ul> <li>Not enough content to scroll</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> <div class="scrollbox"> <ul> <li>Ah! Scroll below!</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>The end!</li> <li>No shadow there.</li> </ul> </div>
代码和示例可以在http://dabblet.com/gist/2462915上找到
你可以在这里找到解释:http://lea.verou.me/2012/04/background-attachment-local/。
其他回答
为条件可见性添加一个事件监听器:
document.getElementById('parent').addEventListener('scroll', (e) => {
const childEl = document.getElementById('child');
childEl.style.visibility = e.target.scrollTop > 0 ? 'visible' : 'hidden';
});
您可以检查相对于偏移父函数的边界。
// Position of left edge relative to frame left courtesy
// http://www.quirksmode.org/js/findpos.html
function absleft(el) {
var x = 0;
for (; el; el = el.offsetParent) {
x += el.offsetLeft;
}
return x;
}
// Position of top edge relative to top of frame.
function abstop(el) {
var y = 0;
for (; el; el = el.offsetParent) {
y += el.offsetTop;
}
return y;
}
// True iff el's bounding rectangle includes a non-zero area
// the container's bounding rectangle.
function overflows(el, opt_container) {
var cont = opt_container || el.offsetParent;
var left = absleft(el), right = left + el.offsetWidth,
top = abstop(el), bottom = top + el.offsetHeight;
var cleft = absleft(cont), cright = cleft + cont.offsetWidth,
ctop = abstop(cont), cbottom = ctop + cont.offsetHeight;
return left < cleft || top < ctop
|| right > cright || bottom > cbottom;
}
如果你传递给它一个元素,它会告诉你它的边界是否完全在容器中,如果没有显式提供容器,它将默认为元素的offset父元素。
如果你正在使用jQuery,你可能会尝试一个技巧:使外部div溢出:隐藏和内部div内容。然后使用.height()函数来检查内部div的高度是否大于外部div的高度。我不确定它是否会工作,但请尝试一下。
使用js检查孩子的offsetHeight是否大于父母..如果是,使父溢出滚动/隐藏/自动你想要的,并在更多div上显示:block ..
比较元素。scrollHeight到元素。clientHeight应该做这个任务。
下面是来自MDN解释Element的图片。scrollHeight和Element.clientHeight。