0
當瀏覽器大小小於768px時,如何才能執行此腳本的一部分?根據屏幕大小僅執行jQuery的一部分
var $container = $('div.recomendField'),
divs = $("div.onethird").get().sort(function() {
return Math.round(Math.random()) - 0.5;
}).slice(0, 3),
$clonedDivs = $(divs).clone();
$container.html('');
$clonedDivs.each(function (index) {
$container.append(this);
if (index % 3 == 0) { // execute only
$(this).css("margin-left", "0%"); // if
} else { // less
$(this).css("margin-left", "2%"); // than
} // 768px
});
$clonedDivs.show();
I have trided this
var $container = $('div.recomendField'),
divs = $("div.onethird").get().sort(function() {
return Math.round(Math.random()) - 0.5;
}).slice(0, 3),
$clonedDivs = $(divs).clone();
$container.html('');
$clonedDivs.each(function (index) {
$container.append(this);
if ($(window).width() >= 768) { // <-this
if (index % 3 == 0) {
$(this).css("margin-left", "0%");
} else {
$(this).css("margin-left", "2%");
}
}
});
$clonedDivs.show();
您使用'> ='代替'<='或是一個錯字? –
你是對的這是一個錯字 – no0ne