2013-07-07 121 views
0

有沒有人可以解釋我這段jQuery代碼?我堅持使用變量currentnext。他們究竟在檢索什麼?jquery代碼說明

function rotate() { 

//Get the first image 

var current = ($('#images .image.show')? $('#images .image.show') : $('#images .image:first')); 
if (current.length == 0) current = $('#images .image:first'); 
//Get next image, when it reaches the end, rotate it back to the first image 

var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#images .image:first') :current.next()) : $('#images .image:first')); 
    //Un-comment the 3 lines below to get the images in random order 
    //var sibs = current.siblings(); 
    //var rndNum = Math.floor(Math.random() * sibs.length); 
    //var next = $(sibs[ rndNum ]); 
    //Set the fade in effect for the next image, the show class has higher z-index 

next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000); 
//Hide the current image 
current.animate({opacity: 0.0}, 1000).removeClass('show'); 

}; 

回答

1

那些被ternary or conditional operators

這些都是等價的:

if($('#images .image.show')){ 
    var current = $('#images .image.show'); 
} 
else{ 
    var current = $('#images .image:first'); 
} 

if(current.next().length){ 
    if(current.next().hasClass('show')){ 
     var next = $('#images .image:first'); 
    } 
    else{ 
     var next = current.next(); 
    } 
else{ 
    var next = $('#images .image:first'); 
} 
+0

我知道這些爲三元運營商。但不能嘗試那些包裹像這樣編碼。 :)非常感謝 – mmaumio

+0

順便說一句,爲什麼你認爲image.show沒有間隔?如果他們是.image .show怎麼辦?如果它們在我測試過的間隔中播放,它不適用於幻燈片。 – mmaumio

+0

'.image.show'將偵聽器附加到同時具有'image'和'show'類的節點,'.image .show'將偵聽器添加到'.image'的後代節點'show'中,所以空間很重要 – karthikr