我寫了一個腳本,從窗口的寬度基於文件加載一些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
你可以添加的console.log(winWidth);在IF語句上方跟蹤當前寬度? – cosmoonot
我做了,它沒有顯示正確的顯示在調整大小鉻 - 有一個不匹配。 –
哦,你正在調整大小。你需要使用$(window).on('load resize',function(){// code here。}); – cosmoonot