スニペット/JavaScript
1 2 3 4 5 6 7 | var ua = navigator.userAgent.toLowerCase(), iph = ua.indexOf("iphone") >= 0, ipad = ua.indexOf("ipad") >= 0, ipod = ua.indexOf("ipod") >= 0, ad = ua.indexOf("android") >= 0, bb = ua.indexOf("blackberry") >= 0, wp = ua.indexOf("windowsphone") >= 0; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | /* * fixHeight - jQuery Plugin * http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html * * Author Koji Kimura @ STARRYWORKS inc. * http://www.starryworks.co.jp/ * * Licensed under the MIT License * */ (function($){ var isInitialized = false; var parents = []; var textHeight = 0; var $fontSizeDiv; $.fn.fixHeight = function() { this.each(function(){ var childrenGroups = getChildren( this ); $.each( childrenGroups, function(){ var $children = $(this); if ( !$children.filter(":visible").length ) return; var row = []; var top = 0; $children.each(function(){ if ( top != $(this).position().top ) { $(row).sameHeight(); row = []; top = $(this).position().top; } row.push(this); }); if ( row.length ) $(row).sameHeight(); }); }); init(); return this; } $.checkFixHeight = function( i_force ) { if ( $fontSizeDiv.height() == textHeight && i_force !== true ) return; textHeight = $fontSizeDiv.height(); $(parents).fixHeight(); } $.fn.sameHeight = function() { var maxHeight = 0; this.css("height","auto"); this.each(function(){ if ( $(this).height() > maxHeight ) maxHeight = $(this).height(); }); return this.height(maxHeight); } function getChildren( i_parent ) { var $parent = $( i_parent ); if ( $parent.data("fixHeightChildrenGroups") ) return $parent.data("fixHeightChildrenGroups"); var childrenGroups = []; var $children = $parent.find(".fixHeightChild"); if ( $children.length ) childrenGroups.push( $children ); var $groupedChildren = $parent.find("*[class*='fixHeightChild']:not(.fixHeightChild)"); if ( $groupedChildren.length ) { var classNames = {}; $groupedChildren.each(function(){ var a = $(this).attr("class").split(" "); var i; var l = a.length; var c; for ( i=0; i<l; i++ ) { c = a[i].match(/fixHeightChild[a-z0-9_-]+/i); if ( !c ) continue; c = c.toString(); if ( c ) classNames[c] = c; } }); for ( var c in classNames ) childrenGroups.push( $parent.find("."+c) ); } if ( !childrenGroups.length ) { $children = $parent.children(); if ( $children.length ) childrenGroups.push( $children ); } $parent.data("fixHeightChildrenGroups", childrenGroups ); parents.push( $parent ); return childrenGroups; } function init() { if ( isInitialized ) return; isInitialized = true; $fontSizeDiv = $(document).append('<div style="position:absolute;left:-9999px;top:-9999px;">s</div>'); setInterval($.checkFixHeight,1000); $(window).resize($.checkFixHeight); $.checkFixHeight(); $(window).load( function(){ $.checkFixHeight(true); } ); } })(jQuery); jQuery(document).ready(function(){ $(".fixHeight").fixHeight(); }); |
コメントを投稿する