0

当浏览器大小小于 768 像素时,如何仅执行此脚本的一部分?

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();
4

1 回答 1

1

我在针对小型设备时使用它:

if(window.screen.availWidth < 768)
于 2013-10-13T05:34:18.413 回答