2013-10-13 35 views
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(); 
+1

您使用'> ='代替'<='或是一個錯字? –

+0

你是對的這是一個錯字 – no0ne

回答

1

我使用此定位時小型設備:

if(window.screen.availWidth < 768) 
相關問題