简体   繁体   English

当 window 宽度等于 600px 时如何禁用 OverlayScrollbars?

[英]How to disable OverlayScrollbars when window width equal 600px?

I use overlayScrollbars to create a <nav></nav> .我使用overlayScrollbars创建一个<nav></nav>

When window width less than 600px, then navbar go left like sidebar.当 window 宽度小于 600px 时,导航栏 go 像侧边栏一样离开。

    <ul>
      <li>test 1</li>
      <li>test 2</li>
      <li>test 3</li>
    </ul>

and create a js file and add this line:并创建一个 js 文件并添加以下行:

$('ul').overlayScrollbars({ });

now when my widnow width less than 600px, overlayScrollbars worke fine but when my window width bigger than 600px, overlayScrollbars still work and element width is like sidebar.现在当我的窗口宽度小于 600px 时,overlayScrollbars 工作正常,但是当我的 window 宽度大于 600px 时,overlayScrollbars 仍然有效并且元素宽度就像侧边栏。

How can I disable overlayScrollbars() when my window width bigger than 600px?当我的 window 宽度大于 600px 时,如何禁用 overlayScrollbars()?

HTML HTML

<ul id="test">
    <li>test 1</li>
    <li>test 2</li>
    <li>test 3</li>
</ul>

JQuery JQuery

So you get it onload
--------------------

if($(window).width() < 600) {
    var instance = OverlayScrollbars(document.getElementById("test"), {});
}

$(window).resize(function() {
  var instance = OverlayScrollbars(document.getElementById("test"), {});
  if ($(window).width() > 600) {
    instance.destroy();
  }
});

You can use the $(window).height(); $(window).width();您可以使用$(window).height(); $(window).width(); $(window).height(); $(window).width(); to check the width and height of page and then enable or disable stuffs with a basic if else function.检查页面的宽度和高度,然后使用基本的if else function 启用或禁用东西。

You need to:你需要:

  1. Store the overlay instances as variables, so you can .destroy() them.将覆盖实例存储为变量,以便您可以.destroy()它们。
  2. Check the window size when the page is opened, and create it depending on size.打开页面时检查window大小,根据大小创建。
  3. Attach .destroy() and recreation of new instances to an onresize event, with the same check as when the page loads..destroy()和新实例的重新创建附加到onresize事件,与页面加载时进行相同的检查。

Here's an example of how to store and destroy with jQuery.这是一个如何使用 jQuery 进行存储和销毁的示例。

//initializes plugin and stores the instance into a variable
var instance = $('ul').overlayScrollbars({ }).overlayScrollbars();
instance.destroy();

Use Media Query to hide elements from any screen!使用媒体查询从任何屏幕隐藏元素!

Try it:试试看:

    <style>
      @media (min-width:600px){
        .yourOverlayElement{ display:none; }
      }
    </style>

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

相关问题 当窗口宽度小于 600px 时,如何禁用 MUI Grid 项目堆叠 - How to disable MUI Grid items from stacking when window width is smaller than 600px 如何禁用滚动条是身体的宽度和高度大于1000px和600px? - How to disable the scrollbars is width and height of body bigger than 1000px and 600px? 类星体 - 垂直滚动条的宽度不超过600px - Quasar - Vertical scrollbar not working on more than 600px width 当显示低于 600px 时,如何将 html 图像更改为 slider? - How can I change html images into a slider when the display with is under 600px? <a>屏幕分辨率低于600像素时</a>无法隐藏<a>标签</a> - cant hide an <a> tag when the screen resolution is below 600px 使用JavaScript获取相当于“max-width:600px”的css - Get css equivalent of “max-width: 600px” using JavaScript 如何在小于600px的屏幕上“打开”页面加载的Bootstrap下拉菜单 - How to 'open' Bootstrap Dropdown menus on pageload on screens smaller than 600px 仅当屏幕宽度小于 600px 时,使 div 从底部消失 850px - make div disappear 850px from bottom only when screen is less than 600px wide Intersection Observer 在 600px 视口下无法工作 (+GSAP) - Intersection Observer not working below 600px viewport (+GSAP) 如果窗口小于 600px,则使用纯 JavaScript 有条件地加载 jquery - Load jquery conditionally with pure JavaScript if windows with < than 600px
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM