简体   繁体   English

如何确定隐藏/溢出文本是在元素的顶部还是底部

[英]How to determine if hidden/overflow text is at top or bottom of element

I'd like to expand on Shog9's answer in 我想进一步扩展Shog9的回答

How to determine from javascript if an html element has overflowing content 如何从JavaScript确定HTML元素是否包含溢出内容

And I'd like to know if the text that is hidden is at the top or at the bottom (or both or none) of the containing element. 而且我想知道隐藏的文本是在包含元素的顶部还是底部(或者两者都没有)。

What's the best way to go about that? 最好的方法是什么?

You can combine scrollLeft and scrollTop with Shog's answer. 您可以将scrollLeftscrollTop与Shog的答案结合使用。

Specifically: 特别:

// Author: Shog9
// Determines if the passed element is overflowing its bounds,
// either vertically or horizontally.
// Will temporarily modify the "overflow" style to detect this
// if necessary.
// Modified to check if the user has scrolled right or down.
function checkOverflow(el)
{
   var curOverflow = el.style.overflow;
   if ( !curOverflow || curOverflow === "visible" )
      el.style.overflow = "hidden";

   var isOverflowing = el.clientWidth < el.scrollWidth 
      || el.clientHeight < el.scrollHeight;

   // check scroll location
   var isScrolledRight = el.scrollLeft > 0;
   var isScrolledDown = el.scrollTop > 0;

   el.style.overflow = curOverflow;

   return isOverflowing;
}

I could not see the forest through the trees. 我看不到穿过树林的森林。 Joel's code snippet var isScrolledDown = el.scrollTop > 0; Joel的代码段var isScrolledDown = el.scrollTop > 0; made me realize how to do it. 让我意识到该怎么做。 I used two functions: 我使用了两个功能:

function HasTopOverflow(el) {
   return el.scrollTop;
}

function HasBottomOverflow(el) {
   var scrollTop = el.scrollTop,
       clientHeight = el.clientHeight,
       scrollHeight = Math.max(el.scrollHeight, clientHeight);

   return (scrollTop + clientHeight) < scrollHeight;
}

Haven't tested if it'll work on IE6+ yet, but FF works. 尚未测试它是否可以在IE6 +上运行,但FF可以运行。

If there are any bugs, please let me know. 如果有任何错误,请告诉我。

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

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