2014-04-17 5 views
0

아래 함수를 사용하여 표 행에서 등 높이 열을 설정합니다. 초기 페이지로드시이 함수는 높이를 'div.gallery-item'에 '0'으로 설정합니다. 리프레시 할 때이 함수는 적절하게 시작하고 함수에서 설정 한 높이를 지정합니다.jQuery 첫 페이지로드시 트리거링하지 않지만 새로 고침시 정상적으로 트리거링하지 않음

/* Responsive equal height columns for gallery (http://css-tricks.com/examples/EqualHeightsInRows/) */ 

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. 
$('div.gallery-item').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); 

}); 

} 


$(window).resize(function() { 
columnConform(); 
}); 

$(function() { 
columnConform(); 
}); 

나는 이것이 내가 기능을로드하고있어 어떻게 함께 할 수있는 뭔가 알고 :

여기 스크립트입니다. 이 스크립트를 가져온 기사에 대한 조언은 이미지가 열의 높이를 다양하게 설정하는 경우 window.onload을 사용하는 것이었지만 아래에 설명 된대로 스크립트의 마지막 부분에 추가하려고 시도했지만 기능에 ' 전혀 방아쇠를 당기지 마라.

$(window).onload = (function() { 
columnConform(); 
}); 

어떤 조언이 필요합니까?

+0

$ (문서) .ready()를 시도하십시오. – Ruben

답변

1

당신은 사용해야

window.onload = columnConform;

대신 :

$(function() { 
    columnConform(); 
}); 

이렇게하면 이미지 크기와 부모 컨테이너가 적합합니다.

+0

나는 이것을 풀어 봤지만 효과가 있지만로드시 등 높이 열이없는 내용의 플래시가 있습니다. 스크립트가 완료 한 후에 만 ​​내용을 표시하는 방법이 있습니까? '$ (function() { \t window.onload = columnConform; });' – jasonbradberry

+1

예, 기본적으로 숨기고, onload 핸들러가 호출되면 표시합니다. 편집 :하지만 준비된 처리기 안에 중첩하지 마십시오, 이것은 쓸모가 없습니다 ('$ (function() {...});은 준비된 처리기의 줄임말입니다!). 그래서,'window.onload = columnConform; '을 사용하십시오. 참고로 플래시를 피하기 위해 보통 웹 사이트에서는 애니메이션을로드하는 방법을 사용합니다. 자체 논리를 따라 구현해야합니다. –

+0

감사. 기본적으로 각 div에 대해'display : none;'을 설정하고'columnConform;'함수에서 높이와 함께'display : block'을 추가함으로써 당신이 그것에 대해 간다고 가정합니까? – jasonbradberry

0

코드를 $(document).ready(handler) 세그먼트에로드하는 대신로드하지 않으셨습니까?

링크 ->HERE

관련 문제