简体   繁体   English

jQuery媒体查询切换按钮-根据屏幕宽度的不同动作

[英]jQuery media queries for toggle button - different action depending on screen width

I have a toggle button which basically shows/hides a list of filters on a Product Index page. 我有一个切换按钮,基本上可以显示/隐藏“产品索引”页面上的过滤器列表。 Desktop is simple, when clicking the button the panel is shown or hidden with the following script: 桌面很简单,单击按钮时,面板将显示或隐藏以下脚本:

$(".filter-toggle").click(function(){
    $(".grid").toggleClass("hide-filters");
});

When the browser is below 1024px I use the mmenu plugin to duplicate the filters content and move it into an off-canvas panel. 当浏览器低于1024px时,我使用mmenu插件复制滤镜内容并将其移到画布之外的面板中。 The code for that looks like this: 该代码如下所示:

$(document).ready(function() {
    $(".filters").mmenu({
        // Options
        navbar: {
            title: "Filters"
        }
    }, {
        // Configuration
        clone: true,
        offCanvas: {
            pageSelector: ".page"
        },
        classNames: {
            selected: "active"
        }
    });

    var API = $(".filters").data("mmenu");
    var $icon = $(".filter-toggle");

    $icon.on("click", function() {
        API.open();
    });
});

Obviously (at the minute) when the toggle button is clicked it will show/hide the content on the page but also trigger the slide-out menu at the same time. 显然(按分钟)单击切换按钮时,它将显示/隐藏页面上的内容,但同时会触发滑出菜单。

My question is how can I only run the relevant script when it's needed. 我的问题是如何只在需要时运行相关脚本。 Is it also possible to do this on resize on not just on pageLoad/refresh? 是否有可能不仅在pageLoad / refresh上在调整大小时执行此操作?

I tried using matchMedia . 我尝试使用matchMedia Here's a quick CodePen showing where I'm at... 这是一个快速的CodePen,显示了我的位置...

https://codepen.io/moy/pen/wymvjN https://codepen.io/moy/pen/wymvjN

It looked like it worked to begin with. 看起来一开始就是可行的。 Above 1024px the toggle worked. 高于1024像素时,切换有效。 Shrinking the browser down meant the button triggered the slide-out panel - great! 缩小浏览器意味着该按钮触发了滑出面板-太好了! But when scaling back up to desktop the slide-out menu is triggered whenever the toggle is clicked. 但是,当还原到桌面时,只要单击切换,就会触发滑出菜单。 On refresh it works again ...until you shrink the browser down and back up again. 刷新后,它将再次起作用...直到您缩小浏览器并再次备份。

Any ideas? 有任何想法吗?

You can listen for window resize events, and fire functions accordingly. 您可以侦听窗口调整大小事件,并相应地触发功能。 For example, in jQuery: 例如,在jQuery中:

$(window).on('resize', function(e) {
  var windowWidth = $(window).width();

  if (windowWidth < 1024) {
    // initialize small viewport functionality
  } else {
    // initialized large viewport functionality
  }
});

But, you'll need a way to disable that mmenu plugin when switching back to desktop, and include it in whatever script you run during that condition. 但是,您需要一种方法,以在切换回桌面时禁用该mmenu插件,并将其包含在该条件下运行的任何脚本中。

Alternatively, the mmenu "Responsive Layout" documentation provides an example for using CSS media queries to hide the cloned menu at certain breakpoints. 另外, mmenu的“响应式布局”文档提供了一个示例,该示例使用CSS媒体查询在某些断点处隐藏克隆的菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM