2017-08-10 19 views
-1

我有一個函數可以捕獲一個div的最大高度(maxHeight),並將兩個相鄰的div設置爲等於maxHeight。我試圖調用這個函數,並在每次窗口大小調整時更新maxHeight。感謝您的幫助。window.onresize不調用函數

Codepen


function setEqualHeight() { 
 
    var maxHeight = 0; 
 
    var height1 = $("#shopify-section-1502392377646").height(); 
 
    var height2 = $("#shopify-section-1502392409157").height(); 
 
    var height3 = $("#shopify-section-1502392513501").height(); 
 

 
    if (height1 > maxHeight) { 
 
    maxHeight = height1; 
 
    } 
 
    if (height2 > maxHeight) { 
 
    maxHeight = height2; 
 
    } 
 
    if (height3 > maxHeight) { 
 
    maxHeight = height3; 
 
    } 
 

 
    $("#shopify-section-1502392377646").height(maxHeight); 
 
    $("#shopify-section-1502392409157").height(maxHeight); 
 
    $("#shopify-section-1502392513501").height(maxHeight); 
 
}; 
 

 
setEqualHeight(); 
 

 
window.onresize = setEqualHeight();
#shopify-section-1502392377646, 
 
#shopify-section-1502392409157, 
 
#shopify-section-1502392513501 { 
 
    width: 33.333333333333333%; 
 
    display: inline-block; 
 
    float: left; 
 
    color: #fff; 
 
} 
 
#shopify-section-1502392377646 { 
 
    background: #023861; 
 
} 
 
#shopify-section-1502392409157 { 
 
    background: #055c9e; 
 
} 
 
#shopify-section-1502392513501 { 
 
    background: #3087ca; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="shopify-section-1502392377646"> 
 
    Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini. 
 
</div> 
 
<div id="shopify-section-1502392409157"> 
 
    Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus winter purslane kale. Celery potato scallion desert raisin horseradish spinach carrot soko. Lotus root water spinach 
 
    fennel kombu maize bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea. 
 
</div> 
 
<div id="shopify-section-1502392513501"> 
 
    Nori grape silver beet broccoli kombu beet greens fava bean potato quandong celery. Bunya nuts black-eyed pea prairie turnip leek lentil turnip greens parsnip. 
 
</div>

+1

查看我的jsFiddle:https://jsfiddle.net/k5puy79u/1/...使用段落標籤來獲取文本的實際高度並相應調整大小。 –

回答

1

你應該使用:

window.onresize = setEqualHeight 

您當前的代碼的函數的返回值設置爲onresize,這不什麼你正在尋找。