2011-07-23 59 views
3

我需要知道我的內容何時溢出我的分區。如果確實如此,我會放入一個鏈接,在所有內容的新窗口中打開頁面。檢查內容是否溢出

乾杯,

DalexL

+0

這可能有所幫助:http://stackoverflow.com/questions/835684/marquee-text-when-text-overflows –

回答

1

這是一個jQuery插件適合文本的寬度和高度:

(function($) { 
    $.fn.fitText = function(options) { 
     options = $.extend({ 
      width: 0, 
      height: 0 
     }, options); 

     $(this).each(function() { 
      var elem = $(this); 
      if (options.height > 0) { 
       while (elem.height() > options.height) { 
        elem.text(elem.text().substring(0, (elem.text().length - 4)) + 'YourLink'); 
       } 
      } 
      if (options.width > 0) { 
       while (elem.width() > options.width) { 
        elem.text(elem.text().substring(0, (elem.text().length - 4)) + 'YourLink'); 
       } 
      } 
     }); 
    } 
})(jQuery); 
2

如果創建這樣的結構:

<div id="outer" style="overflow: auto"> 
    <div id="inner"> 
    content 
    </div> 
</div> 

然後溢出發生時inner的寬度或高度超過的outer因爲外假定的尺寸視口和內部呈現顯示所有內容所需的最小寬度和高度。

您可以將outer標記爲visibility: hidden,以使其佈局但不顯示。

如果內容包含position: fixed內容,那麼該部分將不會被考慮(並且在CSS 2上甚至不會被裁剪)。

+0

問題被標記爲Javascript! – Gerben

+2

@Gerben,請您擴展一下嗎?這個問題明確地談到了DIV中的HTML內容。第一個標籤如何與JavaScript相關? –

9

使用jQuery和Marquee Text When Text Overflows

$('div').each(function(){ 
    var $this = $(this); 
    if ($this.get(0).scrollHeight > $this.height()) { 
     $this.after('<a href="#" target="new">Read More</a>'); 
    } 
}); 

http://jsfiddle.net/eF7jf/

+0

與接受的答案不同,這回答了問題。爲我開箱即用。 –

+0

優秀的,從來沒有想過檢查滾動高度。 – Ripside

+0

完美和乾淨的答案 –

6

沒有jQuery的答案:

if(elements.scrollHeight > element.clientHeight) 
    alert('content-overflow')//not to be confused with stackoverflow 
+1

似乎IE6和IE7有時會給scrollHeight提供不正確的值。你警告。 – Gerben