我有一個網站,該網站針對移動設備和桌面設備採取不同的行爲。我可以用這個選擇器選擇羯羊火災功能:針對不同窗口尺寸觸發不同功能的最佳方式
if($(window).width < 768) {
// fire functions for mobile
} else {
// fire functions for desktop
}
請問這個片段的工作,如果我寫了很多東西到佔位符?它是否跳過整個if
或else
代碼(以減少加載時間)?
應創建變量函數像這樣,以保持它的清潔,或者我應該引發的一切我if {} else {}
功能?
// Is this one better?
if ($(window).width() < 768) {
// $('something').something();
// var another = $('another');
// another.something();
// lets guess lots of stuff to do here
// like 200 lines of code
} else {
// $('something').something();
// var another = $('another');
// another.something();
// lets guess lots of stuff to do here
// like 200 lines of code
}
// Or this one?
var mobileFunctions = function() {
// $('something').something();
// var another = $('another');
// another.something();
// lets guess lots of stuff to do here
// like 200 lines of code
}
var desktopFunctions = function() {
// $('something').something();
// var another = $('another');
// another.something();
// lets guess lots of stuff to do here
// like 200 lines of code
}
if ($(window).width() < 768) {
mobileFunctions();
} else {
desktopFunctions();
}
感謝您的回答。我可能認爲,JavaScript比實際更重。 –