我有一個函式,它獲取段落高度,并通過每次加載影像時減去 100vh - 段落高度來計算影像高度。
它導致不令人滿意的效果,我想減去這個函式得到的最高值。
所以我認為最好的方法是將資料從多個負載傳遞到單個變數,然后選擇最高的一個。雖然我是 js 初學者并且不知道該怎么做。如果我錯了糾正我。
$(".module img").one("load", function () {
var z = $(this).next('p').height()
console.log(z)
$('.module img').css({'height': 'calc(100vh - ' z 'px)'})
}).each(function() {
if(this.complete) {
//$(this).load(); // For jQuery < 3.0
$(this).trigger('load'); // For jQuery >= 3.0
}
});
uj5u.com熱心網友回復:
像這樣的東西應該做的作業:
它只是將最高值存盤在變數中并使用它
let maxHeight = 0;
$(".module img").one("load", function () {
var z = $(this).next('p').height()
if (z > maxHeight){
maxHeight = z;
}
console.log(z)
$('.module img').css({'height': 'calc(100vh - ' maxHeight 'px)'})
}).each(function() {
if(this.complete) {
//$(this).load(); // For jQuery < 3.0
$(this).trigger('load'); // For jQuery >= 3.0
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435623.html
標籤:javascript jQuery 数组 功能 变量
上一篇:盡管coffee_machine是一個全域變數,為什么我得到“分配前參考的UnboundLocalError區域變數‘coffee_machine’”?[復制]
