我正在使用一个平台,可以检测设备是否在移动设备上,并且我正在使用js脚本,我希望在移动设备上查看时有不同的设置。我只是问我是否可以做任何更简单的方法?这是我的示例代码:
function loadKwicks(){
function desk(){
setTimeout(function(){
$('.kwicks').kwicks({
minSize: 20,
behavior: 'menu',
duration: 500,
autoResize:true
});
},500)
}
function mob(){
setTimeout(function(){
$('.kwicks').kwicks({
minSize: 80,
behavior: 'menu',
duration: 500,
autoResize:true,
isVertical:true
});
},500)
}
if (data.device === "mobile") {
mob();
} else {
desk();
}}
答案 0 :(得分:1)
这是相同的....但写的不同。
function loadKwicks(){
var mobSettings = {
minSize: 80,
behavior: 'menu',
duration: 500,
autoResize:true,
isVertical:true
};
var deskSettings = {
minSize: 20,
behavior: 'menu',
duration: 500,
autoResize:true
};
var settings = (data.device === "mobile") ? mobSettings : deskSettings;
setTimeout(function(){
$('.kwicks').kwicks(settings);
},500);
}
它可以缩短更多!
function loadKwicks(){
var settings = [
{
minSize: 20, // Desktop settings
behavior: 'menu',
duration: 500,
autoResize:true
},
{
minSize: 80, // Mobile settings
behavior: 'menu',
duration: 500,
autoResize:true,
isVertical:true
}
];
setTimeout(function(){
$('.kwicks').kwicks(settings[(data.device === "mobile")?1:0]); // True will evaluate as 1
},500);
}