2014-12-20 10 views
1

다음 스크립트는 특정 컨테이너의 div 및 창 크기를 동일하게 유지합니다. http://stephenakins.blogspot.gr/2011/01/uniform-div-heights-for-liquid-css-p.html스크립트가 jQuery 1.9.0과 작동하지 않음

로드시 작동하지만 jQuery 1.9.0에서는 윈도우 크기 조정 기능이 작동하지 않습니다. jQuery 버전을 1.5.0으로 변경하면 크기가 조정됩니다. jQuery 마이그레이션 플러그인을 사용했지만 경고 또는 오류가 발생하지 않습니다. 1.9.0 구문과의 비 호환성이 있습니까? 어떤 아이디어?

// these are (ruh-roh) globals. You could wrap in an 
// immediately-Invoked Function Expression (IIFE) if you wanted to... 
var currentTallest = 0, 
    currentRowStart = 0, 
    rowDivs = new Array(); 

function setConformingHeight(el, newHeight) { 
    // set the height to something new, but remember the original height in case things change 
    el.data("originalHeight", (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight"))); 
    el.height(newHeight); 
} 

function getOriginalHeight(el) { 
    // if the height has changed, send the originalHeight 
    return (el.data("originalHeight") == undefined) ? (el.height()) : (el.data("originalHeight")); 
} 

function columnConform() { 

    // find the tallest DIV in the row, and set the heights of all of the DIVs to match it. 
    $('.equalize > .col').each(function() { 

     // "caching" 
     var $el = $(this); 

     var topPosition = $el.position().top; 

     if (currentRowStart != topPosition) { 

      // we just came to a new row. Set all the heights on the completed row 
      for(currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest); 

      // set the variables for the new row 
      rowDivs.length = 0; // empty the array 
      currentRowStart = topPosition; 
      currentTallest = getOriginalHeight($el); 
      rowDivs.push($el); 

     } else { 

      // another div on the current row. Add it to the list and check if it's taller 
      rowDivs.push($el); 
      currentTallest = (currentTallest < getOriginalHeight($el)) ? (getOriginalHeight($el)) : (currentTallest); 

     } 
     // do the last row 
     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) setConformingHeight(rowDivs[currentDiv], currentTallest); 

    }); 

} 


// Dom Ready 
// You might also want to wait until window.onload if images are the things that 
// are unequalizing the blocks 
$(function() { 
    columnConform(); 
}); 


// Window resize functions 
$(window).resize(function() { 
columnConform(); 
}); 
+0

콘솔에 오류가 표시됩니까? –

+0

jsfiddle을 만들었지 만 jQuery 1.9.1에서 크기 조정에 문제가 없지만 콘솔에서 오류가 표시되지 않는 동안 내 코드에서는 작동하지 않습니다. http://jsfiddle.net/drivebass/fax0v36a/1/ – drivebass

답변

2

나는 여기 귀하의 문제가 data()을 사용한다고 생각합니다. jQuery 1.9.0에서 요소의 data-* 특성 내에 포함 된 값은 data()을 사용하여 요소가 DOM에서 처음 탐색 될 때만 읽혀집니다.

대신 data('originalHeight)을 사용하는 대신 attr('data-originalHeight')을 사용하십시오.이 값은 호출 할 때마다 값을 다시 읽습니다.

+0

스크립트는 데이터를 사용하여 html5 속성이 아닌 요소의 높이를 저장합니다. 귀하의 제안에 따라 요소에 데이터 속성이 추가되고 불행하게도 문제가 해결되지 않습니다. jsfiddle을 만들었고 1.9.1에서 작동하지만 여전히 코드에 문제가 있습니다. 그것을 알아낼 수 없습니다. http://jsfiddle.net/drivebass/fax0v36a/1/ – drivebass

+0

필자는 이것이 스크립트의 올바른 방향을 지적했기 때문에 이것을 대답으로 받아 들였습니다. 요소의 높이를 업데이트했습니다. 더 잘 작동하는 수정 된 스크립트 http://codepen.io/micahgodbolt/pen/FgqLc를 발견했습니다! 감사 – drivebass