2017-04-09 47 views
0

我寫了一個腳本,從窗口的寬度基於文件加載一些HTML。jQuery的窗口寬度不工作在某些斷點

的問題是,在某些點它無法工作

var winWidth = $(window).width(); 
//var winWidth = window.outerWidth; 
//var winWidth = document.body.offsetWidth; 


    if((winWidth > 0) && (winWidth <= 767)){ 
     console.log('Mobile'); 
     $.ajax({ 
      url : "home-banner-parts/mobile.html", 
      dataType: "html", 
      success : function (data) { 
       $("#homeslider").html(data); 
      } 
     }); 
    } 
    if((winWidth >= 768) && (winWidth <= 1279)){ 
     console.log('Tab'); 
     $.ajax({ 
      url : "home-banner-parts/tab.html", 
      dataType: "html", 
      success : function (data) { 
       $("#homeslider").html(data); 
      } 
     }); 
    } 
    if((winWidth >= 1280)){ 
     console.log('Desktop'); 
     $.ajax({ 
      url : "home-banner-parts/desktop.html", 
      dataType: "html", 
      success : function (data) { 
       $("#homeslider").html(data); 
      } 
     }); 
    } 


//the above code is wrapped in function 
$(window).resize(function() { 
    console.log('Resizing'); 
    homeCarousel(); 
}); 

於是問題就來了周圍的寬度

  • 1281px到1295px - 負載tab.html但應該sektop.html加載
  • 770px 785px - 加載mobile.html,但應加載tab.html

請他lp

+0

你可以添加的console.log(winWidth);在IF語句上方跟蹤當前寬度? – cosmoonot

+0

我做了,它沒有顯示正確的顯示在調整大小鉻 - 有一個不匹配。 –

+0

哦,你正在調整大小。你需要使用$(window).on('load resize',function(){// code here。}); – cosmoonot

回答

1

的像素範圍,您的代碼失敗,指向滾動條的寬度。

事實上,你需要使用window.innerWidth得到實際使用的視口。

所以var winWidth = window.innerWidth;

最後,你也應該打電話給你的代碼時,DOM是準備好了,所以

function handleWindowSize(){ 
    // your code here 
} 
$(window).resize(function() { 
    console.log('Resizing'); 
    handleWindowSize(); 
    homeCarousel(); 
}); 
$(document).ready(function(){ 
    $(window).trigger('resize'); 
}) 
1

您需要將所有內容都包含在$(window).on('load resize', function() { ... });事件處理函數中,以便在頁面loadresize事件中運行此代碼。

我還要某處跟蹤狀態,使用戶免於不必要的射擊$.load()如果要加載的是已經在頁面上。

var $body = $('body'); 
 

 
    $(window).on('load resize', function() { 
 
     
 
     var winWidth = $(this).width(), 
 
      state = $body.data('state'); 
 

 
     console.log(winWidth); 
 

 
     if ((winWidth > 0) && (winWidth <= 767) && (state != 'mobile')) { 
 
     $body.data('state','mobile'); 
 
     console.log('Mobile'); 
 
     $.ajax({ 
 
      url: "home-banner-parts/mobile.html", 
 
      dataType: "html", 
 
      success: function(data) { 
 
      $("#homeslider").html(data); 
 
      } 
 
     }); 
 
     } 
 
     if ((winWidth >= 768) && (winWidth <= 1279) && (state != 'tab')) { 
 
     $body.data('state','tab'); 
 
     console.log('Tab'); 
 
     $.ajax({ 
 
      url: "home-banner-parts/tab.html", 
 
      dataType: "html", 
 
      success: function(data) { 
 
      $("#homeslider").html(data); 
 
      } 
 
     }); 
 
     } 
 
     if ((winWidth >= 1280) && (state != 'desktop')) { 
 
     $body.data('state','desktop'); 
 
     console.log('Desktop'); 
 
     $('bo') 
 
     $.ajax({ 
 
      url: "home-banner-parts/desktop.html", 
 
      dataType: "html", 
 
      success: function(data) { 
 
      $("#homeslider").html(data); 
 
      } 
 
     }); 
 
     } 
 
    })
body { 
 
overflow-y: scroll; 
 
min-height: 200vh; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

上面的代碼被封裝在一個功能。我呼籲負荷的功能和resize..Updated代碼 –

+0

@StacyJ更新的代碼是什麼?它是不是在你的OP。我的演示似乎工作。它是否適合你? –

+0

@StacyJ你只檢查調整的時刻,你想負荷和調整。 – cosmoonot