的底部,我想在如何確定是否隱藏/溢出文本是在頂部或元素
How to determine from javascript if an html element has overflowing content
上Shog9的回答擴張,我想知道這是否是隱藏的文本位於包含元素的頂部或底部(或者兩者都沒有)。
什麼是最好的方式去呢?
的底部,我想在如何確定是否隱藏/溢出文本是在頂部或元素
How to determine from javascript if an html element has overflowing content
上Shog9的回答擴張,我想知道這是否是隱藏的文本位於包含元素的頂部或底部(或者兩者都沒有)。
什麼是最好的方式去呢?
我無法看透樹木,不見森林。 Joel的代碼片段var isScrolledDown = el.scrollTop > 0;
讓我意識到如何去做。我使用了兩個函數:
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;
}
還沒有測試過它是否可以在IE6 +上工作,但FF工作。
如果有任何錯誤,請讓我知道。
您可以結合scrollLeft
和scrollTop
與Shog的回答。
具體做法是:
// 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;
}
您是否有內容超過父級頂部的溢出元素示例?我能想到的唯一場景是一個溢出的元素:auto用戶滾動內容的地方... – Shog9 2010-01-07 22:00:34
是的,overflow:auto是我正在使用的。所以當滾動時,內容可以隱藏在上面和下面,兩者都可以或不包含包含的元素。請參閱下面的答案。 – slolife 2010-01-07 22:24:57
@ Shog9,我有一個場景,我們滾動的內容,但我們不想使用滾動條,而是正常(和漂亮的)箭頭。 – trysis 2015-05-04 20:43:57