2013-04-06 68 views
0

我想基於jquery創建動畫,下面是我的代碼;jquery根據選擇器切換(顯示/隱藏)

>items=document.querySelectorAll("#customers td span"); 
[<span alt=​" 02,Counter,11,2013-04-06 14:​59:​16">​ 02​</span>​, <span>​11​</span>​, <span alt=​" 02,Counter,11,2013-04-06 13:​22:​19">​ 02​</span>​, <span>​11​</span>​] 
>item=items[0]; // it has a parent tag <tr> i want the whole row to blink (both spans) 
<span alt=​" 02,Counter,11,2013-04-06 14:​59:​16">​ 02​</span>​ 
>tm=item.attributes.getNamedItem("alt"); 
alt=​" 02,Counter,11,2013-04-06 14:​59:​16" 
>dtm=tm.value.split(',')[3]; 
"2013-04-06 14:59:16" 

或在JQuery中:

$(document).ready(function() { 
    window.setInterval(function(){ 
     $("#customers, td, #span").each(function(){ 
      if($(this).children("span").attr("alt")!=null) 
       var dt=new Date($(this).children("span").attr("alt").split(",")[3]).getTime(); 
        if(dt>$.now()-10*1000){ //am i right here?? 
         console.log("animating"); 
         $(this).parent().fadeOut("slow"); 
         $(this).parent().fadeIn("slow"); 
        }   
     }); 
    },1000); 
}); 

每秒我想檢查在項的每個元素;如果dtm>當前時間 - 10秒,那麼它應該在500毫秒後隱藏,並應在500毫秒後顯示。

上面的代碼我只會眨眼一個跨度,我希望這兩個元素都可以眨眼..而且這個檢查應該每隔1秒繼續。

任何一個可以幫助我..

謝謝..

+0

如果你能夠使用jQuery,你爲什麼要搞亂'querySelectorAll'和'attributes.getNamedItem()'?你需要遍歷這些項目,所以'$(「#customers td span」)。each(...'應該照顧循環,然後'$(this).attr(「alt」)'獲得屬性與當前項目的日期。是問題如何將日期作爲一個字符串(在split()'後面)並將其轉換爲實際日期以與當前時間進行比較? – nnnnnn 2013-04-06 11:22:17

+0

我已經完成了它,現在問題是動畫的光滑度.. – 2013-04-07 07:44:12

+0

上面的代碼是在Chrome中運行,但不是在Firefox中可以有一些身體幫助?? – 2013-04-07 09:27:08

回答

0

下面是我最後的代碼;

<script> 
$(document).ready(function ($) { 
    window.setInterval(function(){ 
     $("#customers, td, span").each(function(){ 
      if($(this).children("span").attr("alt")!=null){ 
       var dt=new Date($(this).children("span").attr("alt").split(",")[3].replace(/-/g,"/")).getTime(); 
       if(dt>$.now()-20*1000){ 
        console.log("animating"); 
        $(this).parent().fadeOut(500,"linear"); 
        $(this).parent().fadeIn(500,"linear"); 
       } 
      } 
     }); 
    },1100); 
}); 
</script> 
+0

增加了'-webkit-transform:translateZ(0);'''客戶,td,span,tr'這改善了動畫的性能有點.. – 2013-04-09 08:11:38

+0

其他任何改進??我我正在開發這個爲Android .. – 2013-04-09 08:12:06