zepto和jquery的區別,zepto的不同使用6條小結

1. Zepto 對象 不能自定義事件

  例如執行: $({}).bind('cust', function(){});
 結果:  TypeError: Object has no method 'addEventListener'
  解決辦法是創建一個脫離文檔流的節點作爲事件對象:

  例如: $('').bind('cust', function(){});

2. Zepto 的選擇器表達式: [name=value]  中value 必須用 雙引號 "  or 單引號 ' 括起來

  例如執行:$('[data-userid=123123123]')
         結果:Error: SyntaxError: DOM Exception 12

  解決辦法: $('[data-userid="123123123]"') or $("[data-userid='123123123']")

2-1.zepto的選擇器沒有辦法選出 $("div[name!='abc']") 的元素

2-2.zepto獲取select元素的選中option不能用類似jq的方法$('option[selected]'),因爲selected屬性不是css的標準屬性

    應該使用$('option').not(function(){ return !this.selected })
    比如:jq:$this.find('option[selected]').attr('data-v') * 1
 zepto:$this.find('option').not(function() {return !this.selected}).attr('data-v') * 1
   但是獲取有select中含有disabled屬性的元素可以用 $this.find("option:not(:disabled)") 因爲disabled是標準屬性

   參考網址:https://github.com/madrobby/zepto/issues/503

2-3、zepto在操作dom的selected和checked屬性時儘量使用prop方法,以下是官方說明:

3.Zepto 是根據標準瀏覽器寫的,所以對於節點尺寸的方法只提供 width() 和 height(),省去了 innerWidth(), innerHeight(),outerWidth(),outerHeight()

Zepto.js: 由盒模型( box-sizing )決定
jQery: 忽略盒模型,始終返回內容區域的寬/高(不包含 padding 、 border )解決方式就是使用 .css('width') 而不是 .width() 。

3-1.邊框三角形寬高的獲取

假設用下面的 HTML 和 CSS 畫了一個小三角形:

1
2
3
4
5
6
7
8
<div class="caret">div>  
.caret { 
  width: 0; 
  height: 0; 
  border-width: 0 20px 20px; 
  border-color: transparent transparent blue;  
  border-style: none dotted solid; 

  jQuery 使用 .width() 和 .css('width') 都返回 ,高度也一樣;

Zepto 使用 .width() 返回 ,使用 .css('width') 返回 0px 。
所以,這種場景,jQuery 使用 .outerWidth() / .outerHeight() ;Zepto 使用 .width() / .height() 。

3-2.offset()

Zepto.js: 返回 top 、 left 、 width 、 height
jQuery: 返回 width 、 height

3-3.隱藏元素

Zepto.js: 無法獲取寬高;
jQuery: 可以獲取。

4.Zepto 的each 方法只能遍歷 數組,不能遍歷JSON對象

5.Zepto 的animate 方法參數說明 :

1
2
3
4
5
$("#some_element").animate({ 
        opacity: 0.25, left: '50px', 
        color: '#abcdef', 
        rotateZ: '45deg', translate3d: '0,10px,0' 
        }, 500, 'ease-out')<pre name="code" class="html"> 

  

1
2
3
4
$this_bd.animate({height:iTthis_bd_h+'px',complete:siblingsUp()},700); 
        function siblingsUp(){ 
            $this_bd.siblings('.bd').animate({height:'0px'},50); 
        };

 

會發現zepto的complete會先執行,在執行animate動畫,所以 回調函數應該寫在後面,如

$('.warter').animate({margin:'150px 0 0 -80px',opacity:'1',rotate:'-45deg'},800,'ease-in-out',function(){alert(1);});

來源:愛創課堂

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章