当您初始化 panelExample 变量时,您可以执行任何您想要的逻辑。
jQuery(document).ready(function($){
var panelExample = (screen.width >= 768) ?
$('#sidecart').scotchPanel({
containerSelector: 'body',
direction: 'left',
duration: 300,
transition: 'ease',
clickSelector: '.toggle-sidecart',
distanceX: '100%', // this sets the width
enableEscapeKey: true
}) :
$('#sidecart').scotchPanel({
containerSelector: 'body',
direction: 'left',
duration: 300,
transition: 'ease',
clickSelector: '.toggle-sidecart',
distanceX: '70%', // this sets the width
enableEscapeKey: true
});
});
如果您要设置许多不同的对象选项,那么上面的示例是有意义的。在您的问题中,唯一改变的是距离X,因此使变量声明更严格可能更具可读性:
jQuery(document).ready(function($){
var panelWidth = (screen.width >= 768) ? "100%" : "70%",
panelExample = $('#sidecart').scotchPanel({
containerSelector: 'body',
direction: 'left',
duration: 300,
transition: 'ease',
clickSelector: '.toggle-sidecart',
distanceX: panelWidth, // this sets the width
enableEscapeKey: true
});
});