用 jQuery 取得隱藏元素的真正長寬

一個讓你取得隱藏元素的實際長寬的 jQuery 插件

jQuery Actual plugin logo

說明

舊版的 jQuery 在擷取隱藏的DOM元素的長寬時常常會出問題. 當目標元素或是它的父元素的 css property ‘display’ 設成 ‘none’ 時. $('#someElement').width(); 傳回的往往是 0 而不是該元素的真正寬度. 這個時候試試看 $('#someElement').actual('width'); 吧!

Demo

  • 這裡 檢視這個 demo
  • 如果你有使用 css3pie 也許你你會想看看 這個 demo( demo/css3pie.html )
  • 原始碼裡也包含了這些 demo 頁面

下載


需求

  • jQuery 1.2.3+

支援瀏覽器


安裝

    • 首先, 確定你使用了一個有效的 DOCTYPE
    • 引用 JS 檔案
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="path-to-file/jquery.actual.js"></script>

使用方法

範例程式碼:
  // get hidden element actual width
  $( '.hidden' ).actual( 'width' );

  // get hidden element actual innerWidth
  $( '.hidden' ).actual( 'innerWidth' );

  // get hidden element actual outerWidth
  $( '.hidden' ).actual( 'outerWidth' );

  // get hidden element actual outerWidth and set the `includeMargin` argument
  $( '.hidden' ).actual( 'outerWidth', { includeMargin : true });

  // get hidden element actual height
  $( '.hidden' ).actual( 'height' );

  // get hidden element actual innerHeight
  $( '.hidden' ).actual( 'innerHeight' );

  // get hidden element actual outerHeight
  $( '.hidden' ).actual( 'outerHeight' );

  // get hidden element actual outerHeight and set the `includeMargin` argument
  $( '.hidden' ).actual( 'outerHeight', { includeMargin : true });

  // if the page jumps or blinks, pass a attribute '{ absolute : true }'
  // be very careful, you might get a wrong result depends on how you makrup your html and css
  $( '.hidden' ).actual( 'height', { absolute : true });

  // if you use css3pie with a float element 
  // for example a rounded corner navigation menu you can also try to pass a attribute '{ clone : true }'
  // please see demo/css3pie in action
  $( '.hidden' ).actual( 'width', { clone : true });

Related posts