【问题标题】:How to disable jQuery plugin on window resize如何在窗口调整大小时禁用 jQuery 插件
【发布时间】:2014-07-02 00:58:43
【问题描述】:

我正在尝试实现窗口调整大小事件侦听器,以便我使用的 jScroll jQuery 插件不会在某些项目的滚动导致问题的较小屏幕上触发。

通过将我在这里读到的各种内容放在一起,这就是我想出的:

<script>    
    $(window).resize(function() {
        windowWidth = $(this).width();
        if (.windowWidth() > 602) {
            $(".sectionTitle").jScroll({speed : "slow",top : 50});
});

</script>

How to disable JavaScript in media query 是我的主要帮助来源,但仍然无法使用。任何帮助将不胜感激。

RE 的 cmets: 这是我在控制台中得到的:

Uncaught SyntaxError: Unexpected token 。 (指数):113 2 未捕获的 SyntaxError:输入意外结束(索引):1 GET http://ghost-mirror.org/notify_act.php net::ERR_NAME_NOT_RESOLVED jquery.js:5

当我不将 jscroll 代码包装在 resize 函数中时,.sectionTitle 会跟随用户在窗口中滚动,就像一个固定元素一样,直到它到达其容器的末尾。

我更新了 entiendo 所说的代码:

<script>    
$(window).resize(function() {
    var windowWidth = $(this).width();
    if(windowWidth > 602) {
        $(".sectionTitle").jScroll({speed : "slow",top : 50});
        }
  });
</script>

如果我可以让插件在大于 602 像素的窗口宽度上默认运行,并在小于 602 的窗口上默认禁用,我将非常感激。 (目前它只在我调整窗口大小时运行,所以如果我加载页面但不更改大小,则脚本不会运行)。

【问题讨论】:

  • 究竟是什么“不起作用”?您的控制台中是否收到任何 JS 错误?例如,我可以在您的代码中清楚地看到一个语法错误 - 您需要在继续之前修复它...
  • 您更新的代码仍然有两个语法错误。两者都包含在if (.windowWidth() &gt; 602) {
  • “不工作”不是对问题的描述...请添加所有相关信息 - 是否还有更多错误报告?
  • 首先,弄清楚如何禁用 jScroll。在禁用 jScroll 之前不要担心 resize 事件。
  • @KevinB - 但在此之前,OP 需要修复所有语法问题...

标签: javascript jquery media


【解决方案1】:

所以,这里有一个建议:

HTML:

 <div id="container">
     <div class="scroll">
     <h3>Page 1</h3>
     <p>Page 1 of jScroll Example - jQuery Infinite Scrolling Plugin
     This is the content of page 1 in the jScroll example. Scroll to the bottom of this box to load the next set of content.

     This is example text for the jScroll demonstration. jScroll is a jQuery plugin for infinite scrolling, endless scrolling, lazy loading, auto-paging, or whatever you may call it.

     With jScroll, you can initialize the scroller on an element with a fixed height and overflow setting of "auto" or "scroll," or it can be set on a standard block-level element within the document and the scrolling will be initialized based on the brower window's scroll position.

     jScroll is open-source and can be downloaded from my GitHub repository at github.com/pklauzinski/jscroll. </p>
     <a href="example-page2.html">next page</a>
     </div>
 </div>

jQuery:

$(document).ready(function(){
var origElement = $('.scroll').clone(); //we're cloning the original element and will use that to replace the content of the div "container" whenever the window size is less than 602px

if($(window).width() > 602){
    $('.scroll').jscroll();
    }

$(window).resize(function(){
    if($(window).width() < 602){
        alert($(window).width());
       $('.scroll').remove();
       $('#container').html(origElement);
       }
    else{
         $('.scroll').jscroll();
        }
    });
});

这是一个小提琴:http://jsfiddle.net/6YXFu/3/

【讨论】:

  • 因为修复语法错误将解决他所问的问题。
  • 太棒了!这行得通,非常感谢。现在唯一的问题是 jScroll 只会在我调整窗口大小时触发,即。如果我只是在不调整大小的情况下加载页面,则代码将无法运行。有没有一种方法可以让它默认运行,然后仅在检测到窗口小于 602px 时才禁用?干杯老兄
  • 显然没有。他拥有的代码(和您的代码)不会根据窗口大小启用/禁用 jscroll。
  • 你是对的.. 我想我有点误读了这个问题。我更新了我的答案。
  • OP,如果可行的话。请将其标记为正确答案,以便其他有相同或相似问题的人可以以此作为参考来解决他们的问题。
猜你喜欢
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 2011-03-17
  • 2016-09-23
相关资源
最近更新 更多