2013-09-25 63 views
0

我有一個div 「#container1」,其中包含一組其他所有動畫,當點擊#container1。但是,爲了阻止用戶能夠這樣做兩次,我已經使用.off(「click」)去激活#container1不會啓動,但其他所有操作都會啓動?

還有一個名爲的div「#close1」,點擊後再次爲所有的div動畫,但是方向相反。然後我想使用.on(「點擊」)使「#container1」再次工作。

的問題是,一切都在功能close1其他()。對行爲以外( 「點擊」)。有人可以指出我做錯了什麼嗎?

function open1() { 
    $(this).children(".teamIconBG1").css('visibility', 'hidden'); 
    $(this).children(".teamIcon1").stop(true, true).animate({ 
     "left": "-=25", 
      "top": "-=25", 
      "width": "190px", 
      "height": "190px", 
      "border-radius": "110px" 
    }, 1000, 'easeOutElastic'); 
    $(this).children(".teamIconArrow1").stop(true, true).switchClass("teamIconArrow1", "teamIconArrow1_active", 500, "easeOutQuart"); 
    $(this).children(".teamIconTitle1").stop(true, true).switchClass("teamIconTitle1", "teamIconTitle1_active", 500, "easeOutQuart"); 
    $(this).children(".close1").stop(true, true).switchClass("close1", "close1_active", 500, "easeOutElastic"); 
    $(this).off("click"); 
} 

function close1() { 
    $(this).parent(".iconContainer1").on('click'); 
    $(this).parent(".iconContainer1").children(".teamIconBG1").css('visibility', 'visible'); 
    $(this).parent(".iconContainer1").children(".teamIcon1").stop(true, true).animate({ 
     "left": "+=25", 
      "top": "+=25", 
      "width": "140px", 
      "height": "140px", 
      "border-radius": "85px" 
    }, 1000, 'easeOutElastic'); 
    $(this).parent(".iconContainer1").children(".teamIconArrow1_active").stop(true, true).switchClass("teamIconArrow1_active", "teamIconArrow1", 500, "easeOutQuart"); 
    $(this).parent(".iconContainer1").children(".teamIconTitle1_active").stop(true, true).switchClass("teamIconTitle1_active", "teamIconTitle1", 500, "easeOutQuart"); 
    $(this).parent(".iconContainer1").children(".close1_active").stop(true, true).switchClass("close1_active", "close1", 500, "easeOutElastic"); 

} 

$("#container1").click(open1); 
$("#container1").click(function() { 
    $(".teamContent1").stop(true, true).switchClass("teamContent1", "teamContent1_active", 500, "easeOutQuart"); 
}); 

$("#close1").click(close1); 
$("#close1").click(function() { 
    $(".teamContent1_active").stop(true, false).switchClass("teamContent1_active", "teamContent1", 500, "easeOutQuart"); 
}); 
+1

你不告訴它點擊時該做什麼。你需要添加一個函數或函數調用。 – Archer

+0

換句話說,它不是一個翻轉開關,可以重新啓用使用'.off()'移除的舊事件處理程序。如果你使用'.off()'處理程序被永久丟棄。 – JJJ

+0

也許我使用了錯誤的功能。當我點擊**「#close1」時,我可以使用什麼來「關閉」功能** open1()**和「打開」** –

回答

1
  1. 使用。一是()一次只能處理程序時,你去註冊登記處理
  2. 使用命名空間的安全
  3. 只是打電話$(this).parent(".iconContainer1").on('click');不會註冊的處理程序,使用$(this).parent(".iconContainer1").off('click.open').one('click', open1);代替

嘗試

function open1() { 
    $(this).children(".teamIconBG1").css('visibility', 'hidden'); 
    $(this).children(".teamIcon1").stop(true, true).animate({ 
     "left": "-=25", 
      "top": "-=25", 
      "width": "190px", 
      "height": "190px", 
      "border-radius": "110px" 
    }, 1000, 'easeOutElastic'); 
    $(this).children(".teamIconArrow1").stop(true, true).switchClass("teamIconArrow1", "teamIconArrow1_active", 500, "easeOutQuart"); 
    $(this).children(".teamIconTitle1").stop(true, true).switchClass("teamIconTitle1", "teamIconTitle1_active", 500, "easeOutQuart"); 
    $(this).children(".close1").stop(true, true).switchClass("close1", "close1_active", 500, "easeOutElastic"); 
} 

function close1() { 
    $(this).parent(".iconContainer1").off('click.open').one('click', open1); 
    $(this).parent(".iconContainer1").children(".teamIconBG1").css('visibility', 'visible'); 
    $(this).parent(".iconContainer1").children(".teamIcon1").stop(true, true).animate({ 
     "left": "+=25", 
      "top": "+=25", 
      "width": "140px", 
      "height": "140px", 
      "border-radius": "85px" 
    }, 1000, 'easeOutElastic'); 
    $(this).parent(".iconContainer1").children(".teamIconArrow1_active").stop(true, true).switchClass("teamIconArrow1_active", "teamIconArrow1", 500, "easeOutQuart"); 
    $(this).parent(".iconContainer1").children(".teamIconTitle1_active").stop(true, true).switchClass("teamIconTitle1_active", "teamIconTitle1", 500, "easeOutQuart"); 
    $(this).parent(".iconContainer1").children(".close1_active").stop(true, true).switchClass("close1_active", "close1", 500, "easeOutElastic"); 

} 

$("#container1").one('click.open', open1); 
$("#container1").click(function() { 
    $(".teamContent1").stop(true, true).switchClass("teamContent1", "teamContent1_active", 500, "easeOutQuart"); 
}); 

$("#close1").click(close1); 
$("#close1").click(function() { 
    $(".teamContent1_active").stop(true, false).switchClass("teamContent1_active", "teamContent1", 500, "easeOutQuart"); 
}); 
0

它應該是這樣的......

$(this).parent(".iconContainer1").on('click', functionName); 

$(this).parent(".iconContainer1").on('click', function() { 
    // do something here when clicked 
}); 

見jQuery的機制的文檔爲on() ......

http://api.jquery.com/on/

1

你不必爲click事件處理程序,.on()應該像使用:

$(this).parent(".iconContainer1").on('click', function() { 
    //do something 
});