2013-10-21 84 views
3

我有一個用於創建圖像輪播的jQuery代碼。我知道這不是最優雅的一段代碼。event.stopImmediatePropagation();適用於除Firefox以外的所有瀏覽器

jQuery(function(){ 
$(".project").css('max-height','180px'); //180px 
var expanded = 0; 
var position = 0; 
x = 0; 


$(".project").click(function(){ 
    if (expanded == 0){ 
     $(this).css('max-height','320px'); 
     expanded = 1; 
     $(this).find('.projectcarousel').find('.control').fadeIn(300); 
     $(this).find('.projectcarousel').find('.control').css('display','block'); 
     $(this).find('.projectdescription').find('.tags').fadeIn(500); 
     $(this).css('opacity','1'); 
    } 
    else if (expanded == 1){ 
     $(this).css('max-height','180px'); 
     $(this).find('.projectcarousel').find('.control').fadeOut(300); 
     $(this).find('.projectdescription').find('.tags').fadeOut(500); 
     $(this).find('.viewscreen').find('.carousel').css('-moz-transform','translate(0,0)'); 
     $(this).find('.viewscreen').find('.carousel').css('-webkit-transform','translate(0,0)'); 
     $(this).find('.viewscreen').find('.carousel').css('-o-transform','translate(0,0)'); 
     $(this).find('.viewscreen').find('.carousel').css('transform','translate(0,0)'); 
     expanded = 0; 
     position = 0; 
     x = 0; 
    } 
}); 

$('.prev').click(function(){ 
    event.stopImmediatePropagation(); 

    switch(position){ 
     case 0: 
      x = "-420px" 
      position = 1; 
      break; 

     case 1: 
      x = "-840px" 
      position = 2; 
      break; 

     case 2: 
      x = "-1260px" 
      position = 3; 
      break; 

     case 3: 
      x = "-1680px" 
      position = 4; 
      break; 

     default: 
      x = 0; 
      position = 0; 
    } 

    $(this).parent().parent().next().find('.carousel').css('-moz-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('-webkit-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('-o-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('transform','translate('+x+',0)'); 
}); 

$('.next').click(function(){ 
    event.stopImmediatePropagation(); 

    switch(position){ 

     case 0: 
      x = "-1680px" 
      position = 4; 
      break; 

     case 1: 
      x = "0px" 
      position = 0; 
      break; 

     case 2: 
      x = "-420px" 
      position = 1; 
      break; 

     case 3: 
      x = "-840px" 
      position = 2; 
      break; 

     case 4: 
      x = "-1260px" 
      position = 3; 
      break; 

     default: 
      x = 0; 
      position = 0; 
    } 

    $(this).parent().parent().next().find('.carousel').css('-moz-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('-webkit-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('-o-transform','translate('+x+',0)'); 
    $(this).parent().parent().next().find('.carousel').css('transform','translate('+x+',0)'); 
}); 
}); 

event.stopImmediatePropagation();工作在Chrome,Opera和Safari,但不能在Firefox工作。我試過使用event.stopPropagation()event.preventDefault();,但兩個代碼都不起作用。

我從Firefox得到的錯誤是「使用getPreventDefault的()已過時。使用defaultPrevented而不是'ReferenceError:event is not defined'。

我使用的代碼以錯誤的方式或者是有在Firefox中的錯誤,我應該知道的?

+2

你所有的活動功能都缺少'event'(或類似)作爲參數。 – Zeta

+1

讓我看看我有一個錯誤消息,說:*''ReferenceError:事件沒有定義'。「*哦,哪裏是事件定義,哪裏哦,它可以在哪裏? – epascarello

回答

6

使用jQuery的規範化事件對象,這是作爲第一個參數傳遞給你的回調函數,你所有的jQuery的事件處理程序:

$('.next').click(function(e){ // added the 'e' parameter 
    e.stopImmediatePropagation(); // uses jQuery's event object, referenced by e 

請注意,我把它命名爲e但你可以叫它幾乎任何你喜歡的,只要它是一個有效的JavaScript標識符。我選擇e,因爲它很短(只有一個字符),相對明確(我知道它指的是表示事件的對象),並且我不太可能隱藏任何其他具有相同名稱的變量。

+1

N.B.對於那些可能被這裏的特殊性困惑的人來說:函數參數的命名並不重要:'e','evt','event',甚至'foo'都會引用事件對象。如果你喜歡短一些,「e」就短一些。 – Robusto

0

使用event

1)

$('.prev').click(function(event){ 
event.stopImmediatePropagation(); 

2)

$('.next').click(function(event){ 
event.stopImmediatePropagation(); 
相關問題