首页 > 解决方案 > 仅在特定宽度的窗口上调用 jQuery 函数

问题描述

我在我的网站上使用此代码。我只想在特定宽度的窗口中加载此代码。例如,在小于 768 像素的情况下,将不再调用此脚本。

jQuery(document).ready(function() {
var owl = jQuery('.owl-carousel');
owl.owlCarousel({
  rtl: true,
  margin: 10,
  nav: true,
  loop: true,
  responsive: {
    0: {
      items: 1
    },
    600: {
      items: 3
    },
    1000: {
      items: 5
    }
      }
    })
  })

标签: javascriptjquery

解决方案


您可以使用 jQuery 的宽度函数,然后执行以下操作:

jQuery(document).ready(function() {

   if (jQuery( window ).width() > 768) {
      /// your code here
   }
});

.width() 参考


推荐阅读