我以前有過以下 function,它是相當分解重複冗餘變量。所以我想改進它並將圖像存儲到數組中。但是,即使使用上一個腳本的相同上下文標記,它也不會檢測陣列中的圖像值,並將其與item
相匹配。我不知道爲什麼。我設置每個項目以檢查不同圖像大小的實例,然後執行if
所述的操作。爲什麼函數不能匹配變化數組的寬度和高度?
Working fiddle (non-working script):
Working fiddle: (working script):
我不是表達陣列的正確值。我覺得我正在過度思考,忽略一些小事。但我真的很難過。
var top_horizontalvert_mobile = ['320 x 50', '300 x 100', '300 x 50', '250 x 250', '120 x 60', '240 x 400', '180 x 150', '125 x 125'];
var top_horizontal_desktop = ['729 x 90', '468 x 60'];
var bottom_horizontal_desktop = ['930 x 180', '336 x 280'];
var bottom_vert_mobile = ['234 x 60'];
var middle_vert_mobile = ['300 x 250'];
var right_vert_desktop = ['120 x 600', '160 x 600'];
$("#carousel-container-mobile, #carousel-container-desktop").on('slid.bs.carousel', function() {
$('.item').each(function() {
var imgs = $('.item');
var w = $(img).width();
var h = $(img).height();
var img = $('img', this);
if (w == top_horizontalvert_mobile && h == top_horizontalvert_mobile) {
img.addClass('top, smaller-img');
}
if (w == top_horizontal_desktop && h == top_horizontal_desktop) {
img.addClass('top, larger-img');
}
if (w == bottom_vert_mobile && h == bottom_vert_mobile) {2
img.addClass('bottom, smaller-img');
}
if (w == bottom_horizontal_desktop && h == bottom_horizontal_desktop) {
img.addClass('bottom, larger-img');
}
if (w == middle_vert_mobile && h == middle_vert_mobile) {
img.addClass('middle, smaller-img');
}
if (w == right_vert_desktop && h == right_vert_desktop) {
img.addClass('right, smaller-img');
}
if (w == middle_vert_mobile && h == middle_vert_mobile) {
img.addClass('right, smaller-img');
}
});
});
如果元素隱藏,隱藏元素的高度和寬度將爲垃圾值。垃圾在某種意義上不是一個正確的價值。 –