在运行Jquery函数之前检查Widget是否可用 [英] Check if Widget is Available Before Running Jquery Function

查看:109
本文介绍了在运行Jquery函数之前检查Widget是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jquery插件同位素.根据屏幕分辨率,我需要销毁同位素小部件以防止其运行其功能.我正在使用以下代码:

$(window).smartresize(function(){
            if($(window).width() < 700) {
                container.isotope('destroy');

            }else {
                container.isotope({$options});
            }
        });

这在第一次调整大小时效果很好,同位素窗口小部件被破坏了.但是,如果我再次调整大小(低于700像素),则会引发以下异常:

cannot call methods on isotope prior to initialization; attempted to call method 'destroy'

在运行container.isotope('destroy');之前如何检查container.isotope是否存在?

完整的工作代码

      $(window).load(function(){
        var container = $('{$this->selector}')
        if($(window).width() > 701){
            container.isotope({$options});
        }else{
            container.isotope = false;
        }

        $(window).smartresize(function(){
          if($(window).width() < 700) {
            container.find('.item').removeAttr('style');
            if(container.isotope) {
              container.isotope('destroy')
              container.isotope = false
            }
          } else{
            container = $('{$this->selector}')
            container.isotope({$options})
          }
        });
      });

解决方案

I am using the Jquery plugin isotope. Depending on the screen resolution I need to destroy the isotope widget to prevent it from running its function. I am using the following code:

$(window).smartresize(function(){
            if($(window).width() < 700) {
                container.isotope('destroy');

            }else {
                container.isotope({$options});
            }
        });

This works fine on the first resize, the isotope widget is destroyed. However, if I resize again (below 700px) the following exception is thrown:

cannot call methods on isotope prior to initialization; attempted to call method 'destroy'

How can I check to see if container.isotope exists before running container.isotope('destroy');?

Complete Working Code

      $(window).load(function(){
        var container = $('{$this->selector}')
        if($(window).width() > 701){
            container.isotope({$options});
        }else{
            container.isotope = false;
        }

        $(window).smartresize(function(){
          if($(window).width() < 700) {
            container.find('.item').removeAttr('style');
            if(container.isotope) {
              container.isotope('destroy')
              container.isotope = false
            }
          } else{
            container = $('{$this->selector}')
            container.isotope({$options})
          }
        });
      });

解决方案

$(window).smartresize(function(){
  if($(window).width() < 700) {
    if(container.isotope) {
      container.isotope('destroy')
      container.isotope = false
    }
  } else if(container.isotope) {
    container.isotope({$options})
  }
})

这篇关于在运行Jquery函数之前检查Widget是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆