2014-11-01 54 views
0

我完全是jQuery的新手。這可能是我錯過了一些基本的東西,我的代碼看起來像一塊垃圾。如果你喜歡,隨意忽略這個。緩慢的jQuery - 很長的加載時間?

我寫了這段代碼,但有時瀏覽器並沒有迴應我的「clics」,也沒有做任何事情。然後它正常工作,一段時間後它再次卡住。 任何想法?非常感謝!

$(document).ready(function() { 

$("#text2").hide(); 
$("#text3").hide(); 

$('#ball1').click(function() { 
    $('#text2').fadeOut('slow'); 
    $('#text3').fadeOut('slow'); 
    $('#text1').fadeIn('slow'); 
    $("#arrow").animate({left:'40px'}); 

$('#ball2').click(function() { 
    $('#text1').fadeOut('slow'); 
    $('#text3').fadeOut('slow'); 
    $('#text2').fadeIn('slow'); 
    $("#arrow").animate({left:'400px'}); 

$('#ball3').click(function() { 
    $('#text2').fadeOut('slow'); 
    $('#text1').fadeOut('slow'); 
    $('#text3').fadeIn('slow'); 
    $("#arrow").animate({left:'770px'}); 
}); 
}); 
    }); 
    }); 
+0

你可以爲我們製作一個[jsFiddle](http://jsfiddle.com)來模擬這個問題嗎? – Ohgodwhy 2014-11-01 00:17:36

+2

您的其他點擊事件正在處理程序中綁定。 – 2014-11-01 00:18:05

+1

非常感謝您的回覆!不幸的是,我不知道你的意思,以及如何解決這個問題。我需要進一步的教育,是的。你能否向我解釋或重寫代碼?再次,如果你喜歡,請隨時忽略我... – 2014-11-01 00:22:18

回答

1

我認爲你的範圍是混亂的。您忘記關閉每個功能,而是將它們全部嵌套在第一個中。這可能很慢,因爲每次單擊#ball1時都會繼續分配點擊事件。用下面的代替它:

$(document).ready(function() { 

    $("#text2").hide(); 
    $("#text3").hide(); 

    $('#ball1').click(function() { 
    $('#text2').fadeOut('slow'); 
    $('#text3').fadeOut('slow'); 
    $('#text1').fadeIn('slow'); 
    $("#arrow").animate({left:'40px'}); 
    }); 

    $('#ball2').click(function() { 
    $('#text1').fadeOut('slow'); 
    $('#text3').fadeOut('slow'); 
    $('#text2').fadeIn('slow'); 
    $("#arrow").animate({left:'400px'}); 
    }); 

    $('#ball3').click(function() { 
    $('#text2').fadeOut('slow'); 
    $('#text1').fadeOut('slow'); 
    $('#text3').fadeIn('slow'); 
    $("#arrow").animate({left:'770px'}); 
    }); 

}); 
+1

OMG,你們真棒!我的意思是,當我開始時,對於我來說,獲得這種支持並不意味着什麼!謝謝,謝謝,謝謝 – 2014-11-01 00:23:35