1/* 2 * fixHeight - jQuery Plugin 3 * http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html 4 * 5 * Author Koji Kimura @ STARRYWORKS inc. 6 * http://www.starryworks.co.jp/ 7 * 8 * Licensed under the MIT License 9 * 10 */ 11(function($){ 12 13 var parents = []; // 親è¦ç´ 14 var isInitialized = false; // åæåæ¸ã¿ãã©ã° 15 var textHeight = 0; // æåã®ãµã¤ãº 16 var $fontSizeDiv; // æåã®ãµã¤ãºåå¾ç¨div 17 18 //------------------------------------------------------------ 19 // jQuery ãã©ã°ã¤ã³ 20 //------------------------------------------------------------ 21 $.fn.fixHeight = function( i_force ) { 22 init(); 23 24 this.each(function(){ 25 var childrenGroups = getChildren( this ); 26 var children = childrenGroups.shift() || []; 27 28 //$childrenã®Y座æ¨ãåããã®ã¯åãé«ãã« 29 var rows = [[]]; 30 var top = 0; 31 var c=0; 32 var $c; 33 34 for ( c=0; c<children.length; c++ ) { 35 $c = $(children[c]); 36 var i; 37 if ( top != $c.offset().top ) { 38 for ( i=0; i<rows.length; i++ ) if ( !$(rows[i]).data("fixedHeight") || i_force ) $(rows[i]).sameHeight().data("fixedHeight",true); 39 rows = [[]]; 40 top = $c.offset().top; 41 for ( i=0; i<childrenGroups.length; i++ ) rows.push([]); 42 } 43 rows[0].push(children[c]); 44 for ( i=0; i<childrenGroups.length; i++ ) { 45 rows[i+1].push(childrenGroups[i][c]); 46 } 47 } 48 if ( rows[0] && rows[0].length ) for ( i=0; i<rows.length; i++ ) $(rows[i]).sameHeight(); 49 50 parents.push( $(this) ); 51 }); 52 53 return this; 54 }; 55 56 //------------------------------------------------------------ 57 // åãé«ãã«æããjQueryãã©ã°ã¤ã³ 58 //------------------------------------------------------------ 59 $.fn.sameHeight = function() { 60 var maxHeight = 0; 61 this.css("height","auto"); 62 this.each(function(){ 63 if ( $(this).height() > maxHeight ) maxHeight = $(this).height(); 64 }); 65 return this.height(maxHeight); 66 }; 67 68 //------------------------------------------------------------ 69 // ãã©ã³ããµã¤ãºã®å¤æ´æãã¦ã£ã³ãã¦ã®ãªãµã¤ãºæã«é«ããæããå¦çãå®è¡ 70 //------------------------------------------------------------ 71 $.checkFixHeight = function( i_force ) { 72 if ( $fontSizeDiv.height() == textHeight && i_force !== true ) return; 73 textHeight = $fontSizeDiv.height(); 74 if ( parents.length ) $(parents).fixHeight( i_force ); 75 } 76 77 //------------------------------------------------------------ 78 // [private] ãã©ã³ããµã¤ãºç¢ºèªç¨ã®DIV追å ã¨ã¿ã¤ãã¼ã»ãã 79 //------------------------------------------------------------ 80 function init() { 81 if ( isInitialized ) return; 82 isInitialized = true; 83 $fontSizeDiv = $('body').append('<div style="position:absolute;left:-9999px;top:-9999px;">s</div>'); 84 setInterval($.checkFixHeight,1000); 85 $.checkFixHeight(); 86 $(window).on("resize",$.checkFixHeight); 87 $(window).on("load",$.checkFixHeight); 88 } 89 90 //------------------------------------------------------------ 91 // [private] å¦çãã¹ãåè¦ç´ ã®ã°ã«ã¼ããåå¾ 92 //------------------------------------------------------------ 93 function getChildren( i_parent ) { 94 var $parent = $( i_parent ); 95 96 //æ¢ã«ãã¼ã¿ãåå¨ããã°ãããè¿ã 97 //if ( $parent.data("fixHeightChildrenGroups") ) return $parent.data("fixHeightChildrenGroups"); 98 99 //åã°ã«ã¼ããæ ¼ç´ããé å 100 var childrenGroups = []; 101 102 //fixHeightChildã¯ã©ã¹ãæã¤åè¦ç´ ãåå¾ 103 var $children = $parent.find(".fixHeightChild"); 104 if ( $children.length ) childrenGroups.push( $children ); 105 106 //fixHeightChildXXXã¯ã©ã¹ãæã¤åè¦ç´ ãåå¾ 107 var $groupedChildren = $parent.find("*[class*='fixHeightChild']:not(.fixHeightChild)"); 108 if ( $groupedChildren.length ) { 109 var classNames = {}; 110 $groupedChildren.each(function(){ 111 var a = $(this).attr("class").split(" "); 112 var i; 113 var l = a.length; 114 var c; 115 for ( i=0; i<l; i++ ) { 116 c = a[i].match(/fixHeightChild[a-z0-9_-]+/i); 117 if ( !c ) continue; 118 c = c.toString(); 119 if ( c ) classNames[c] = c; 120 } 121 }); 122 for ( var c in classNames ) childrenGroups.push( $parent.find("."+c) ); 123 } 124 125 //åè¦ç´ ã®æå®ããªãå ´åã¯ç´å±ã®åãåå¾ 126 if ( !childrenGroups.length ) { 127 $children = $parent.children();
128 if ( $children.length ) childrenGroups.push( $children ); 129 } 130 131 return childrenGroups; 132 } 133 134})(jQuery); 135 136jQuery(document).ready(function(){ 137$('.fixHeight').fixHeight(); 138});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.