在移动设备上更改画布面板上的jquery宽度

时间:2017-04-09 16:30:22

标签: javascript jquery off-canvas-menu

我使用scotch panels制作非画布菜单。我想在移动设备上将面板的宽度从70%更改为100%。这是我正在使用的代码:

jQuery(document).ready(function($){

var panelExample = $('#sidecart').scotchPanel({
    containerSelector: 'body',
    direction: 'left',
    duration: 300,
    transition: 'ease',
    clickSelector: '.toggle-sidecart',
    distanceX: '70%', // this sets the width
    enableEscapeKey: true
});

});

我想我可以使用下面的代码,但我不确定如何

if (screen.width >= 768) {
    // make width of panel 100%
}

任何人都可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

初始化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
  });
});

如果您设置了许多不同的对象选项,则上述示例是有意义的。在您的问题中,唯一改变的是distanceX,因此使变量声明更紧密可能更具可读性:

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
  });

});