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

說明
舊版的 jQuery 在擷取隱藏的DOM元素的長寬時常常會出問題. 當目標元素或是它的父元素的 css property ‘display’ 設成 ‘none’ 時. $('#someElement').width();
傳回的往往是 0 而不是該元素的真正寬度. 這個時候試試看 $('#someElement').actual('width');
吧!
Demo
下載
- 從 Github 下載原始碼
需求
- jQuery 1.2.3+
支援瀏覽器
- Firefox 2.0+
- Internet Explorer 6+
- Safari 3+
- Opera 10.6+
- Chrome 8+
安裝
- 首先, 確定你使用了一個有效的 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 });