我有一个网站,该网站针对移动设备和桌面设备采取不同的行为。我可以用这个选择器选择羯羊火灾功能:针对不同窗口尺寸触发不同功能的最佳方式
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比实际更重。 –