UNPKG

jquery.actual

Version:

Older version of jQuery has trouble finding the width/height of invisible DOM elements. With element or its parent element has css property 'display' set to 'none'. `$('.hidden').width();` will return 0 instead of the actual width; This plugin simply fix

165 lines (145 loc) 5.31 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery Actual Plugin Demo - Normal Usage</title> <style type="text/css"> label{ display: block; } #wrap{ width: 500px; margin: 0 auto 50px; } #info{ text-align: center; margin: 50px 0 0; } #operation{ width: 500px; height: 150px; position: relative; } #outer, #inner{ padding: 20px; margin: 10px auto; position: relative; } #outer span, #inner span{ position: absolute; top: 5px; left: 10px; color: white; } #outer{ width: 100px; background: #000; } #inner{ width: 30px; background: #555; } </style> </head> <body> <div id="wrap"> <h1> jQuery Actual Plugin Demo - Normal Usage </h1> <p> Try to switch between position, visibility, display, opacity to see how the result is. </p> <form id="options"> <label>#outer</label> <input class="outer" type="checkbox" name="position" value="absolute" /> position: absolute<br /> <input class="outer" type="checkbox" name="visibility" value="hidden" /> visibility: hidden<br /> <input class="outer" type="checkbox" name="display" value="none" /> display: none<br /> <input class="outer" type="checkbox" name="opacity" value="0" /> opacity: 0<br /> <br /> <label>#inner</label> <input class="inner" type="checkbox" name="position" value="absolute" /> position: absolute<br /> <input class="inner" type="checkbox" name="visibility" value="hidden" /> visibility: hidden<br /> <input class="inner" type="checkbox" name="display" value="none" /> display: none<br /> <input class="inner" type="checkbox" name="opacity" value="0" /> opacity: 0<br /> </form> <div id="operation"> <div id="outer"> <span>#outer</span> <div id="inner"> <span>#inner</span> </div> </div> </div> <div id="display"></div> <h3 id="info"> Demo provides by <a href="http://dreamerslab.com/">DreamersLab</a> </h3> </div> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <script type="text/javascript" src="../jquery.actual.min.js"></script> <script type="text/javascript" charset="utf-8"> // wrap everything in a anonymous function to prevent global ( function( $ ){ // the testing result with default jquery width() and this plugin actual('width') var result = function(){ // THE ACTUAL DEMO CODE // empty display area before append new result $( '#display' ). empty(). append( '<p>$( "#inner" ).width(); => ' + $( '#inner' ).width() + 'px</p>' ). append( '<p>$( "#inner" ).actual( "width" ); => ' + $( '#inner' ).actual( 'width' ) + 'px</p>' ); }, switchCss = function( callback ){ // cache dom element $checkbox = $( ':checkbox' ); // change result on clicke event $checkbox.bind( 'click', function(){ // vars to store selected css for #outer and #inner var outerCss = {}, innerCss = {}; // get each selected css props for #outer element $checkbox.filter( '.outer:checked' ).each( function(){ var $this = $( this ); outerCss[ $this.attr( 'name' )] = $this.val(); }); // get each selected css props for #inner element $checkbox.filter( '.inner:checked' ).each( function(){ var $this = $( this ); innerCss[ $this.attr( 'name' )] = $this.val(); }); // clear all style before apply new styles $( '#outer' ).removeAttr( 'style' ).css( outerCss ); $( '#inner' ).removeAttr( 'style' ).css( innerCss ); // fire callback function if any if( callback ) callback.call( this ); }); }; // when document is ready show the initial result // when user switchs the css props shows the new result $( function(){ result(); switchCss( result ); }); })( jQuery ); </script> <script type="text/javascript" charset="utf-8"> // google analytics var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20960410-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html>