jquery监听div或其它元素改变 jquery resize 事件 出错解决方案

        网上拷贝能人的下列代码:

  1. (function($, h, c) {  
  2.     var a = $([]), e = $.resize = $.extend($.resize, {}), i, k = "setTimeout", j = "resize", d = j  
  3.             + "-special-event", b = "delay", f = "throttleWindow";  
  4.     e[b] = 350;  
  5.     e[f] = true;  
  6.     $.event.special[j] = {  
  7.         setup : function() {  
  8.             if (!e[f] && this[k]) {  
  9.                 return false  
  10.             }  
  11.             var l = $(this);  
  12.             a = a.add(l);  
  13.             $.data(this, d, {  
  14.                 w : l.width(),  
  15.                 h : l.height()  
  16.             });  
  17.             if (a.length === 1) {  
  18.                 g()  
  19.             }  
  20.         },  
  21.         teardown : function() {  
  22.             if (!e[f] && this[k]) {  
  23.                 return false  
  24.             }  
  25.             var l = $(this);  
  26.             a = a.not(l);  
  27.             l.removeData(d);  
  28.             if (!a.length) {  
  29.                 clearTimeout(i)  
  30.             }  
  31.         },  
  32.         add : function(l) {  
  33.             if (!e[f] && this[k]) {  
  34.                 return false  
  35.             }  
  36.             var n;  
  37.             function m(s, o, p) {  
  38.                 var q = $(this), r = $.data(this, d);  
  39.                 r.w = o !== c ? o : q.width();  
  40.                 r.h = p !== c ? p : q.height();  
  41.                 n.apply(this, arguments)  
  42.             }  
  43.             if ($.isFunction(l)) {  
  44.                 n = l;  
  45.                 return m  
  46.             } else {  
  47.                 n = l.handler;  
  48.                 l.handler = m  
  49.             }  
  50.         }  
  51.     };  
  52.     function g() {  
  53.         i = h[k](function() {  
  54.             a.each(function() {  
  55.                 var n = $(this), m = n.width(), l = n.height(), o = $  
  56.                         .data(this, d);  
  57.                 if (m !== o.w || l !== o.h) {  
  58.                     n.trigger(j, [ o.w = m, o.h = l ])  
  59.                 }  
  60.             });  
  61.             g()  
  62.         }, e[b])  
  63.     }  
  64. })(jQuery, this);  

1 . 放入,声明了jq的地方

2 . 可以放入方法装好(在1的条件下也可以直接随意放),更好的体现出逻辑:

var function名字 = function(){

上面拷贝的代码:

}

监听调用,以及出错时的处理:

  1.                 $("选择器id,css...等等").on("resize ",function(event){ 
  2.                         console.log("阻止冒泡");  
  3.                         event.stopPropagation();
  4.                         console.log($(this).hight());//等等。。。
  5.                     });
  6.     

js分号有调节格式的作用,-_-(ctrl+k+d)也可以调节格式

猜你喜欢

转载自blog.csdn.net/hwc_dd/article/details/80488780
今日推荐