2012-04-20 53 views
0
j = {largeSign: function(a) { 
     var b = $(#identity .scorecard"), c = 43, d = 105 - c, e = 800; 
     this.animation(b, d, c, e, a) 
    },animation: function (a, b, c, d, e) { 
     var f = this, g = 1e3, h, i = function() { 
      $(".sign", a).each(function(a, f) { 
        h = parseInt(e + $(this).text()), a > 2 && (d += 30), a === 0 || a === 3 ? $(this).animate({backgroundPosition: "0px " + (b * h + c) + "px"}, d * 1.6) : a === 1 || a === 4 ? $(this).animate({backgroundPosition: "0px " + (b * h + c) + "px"}, d * 1.8) : (a === 2 || a === 5) && $(this).animate({backgroundPosition: "0px " + (b * h - b + c) + "px"}, d * 2, function() { 
         $(this).delay(200).animate({backgroundPosition: "0px " + (b * parseInt(e + $(this).text()) + c) + "px"}, 1e3) 
        }) 
      }) 
     }; 
     setTimeout(i, g) 
    }} 

基本上我試圖動畫的背景位置,並讓它停止在div .sign內解析的值。如何在文檔加載後加載此javascript函數?

$(document).ready(function(){ 

    // the call goes here 

}) 

回答

1

使用。就緒()

$(document).ready(function(){ 
    j.largeSign(); 
}); 

http://api.jquery.com/ready/

+1

我最喜歡這個選項。如果您使用setInterval或其他上下文來調用您的函數,您將無法訪問它,如果它已在文檔範圍內定義的話。更好地事先定義它,然後等待文檔準備就緒後再運行它。 – 2012-04-20 21:12:49

+0

我們已經回答了一個簡單的問題5次。這...... – undefined 2012-04-20 21:14:24

+0

@ElliotBonneville它確實有點不同,我稱之爲用戶提供的功能。 – 2012-04-20 21:14:45

0

由包裝代碼可以用.ready()包裝:

jQuery(document).ready(function($) { 
    // Code using $ as usual goes here. 
}); 
1

看起來你正在使用jQuery的,所以這裏的jQuery的解決方案:

$(function() { 
    // your code here 
}); 

這僅僅是用於

$(document).ready(function() { 
    // your code here 
}); 
+0

+1提供簡便的解決方案。 – undefined 2012-04-20 21:22:09

0

您速記

+0

爲什麼你將jquery傳遞給匿名函數? – 2012-04-21 09:01:25

+0

這只是一個沒有衝突的做法。請參閱提供的鏈接中的jQuery文檔。摘錄來自它。 – 2012-04-21 15:39:24

0

如果你想要這個文件加載後運行,試試這個:

$(function(){ 
//put your code here 
j = {largeSign: function(a) {... 
}); 

我發現使用「$(函數(){'比'$(document).ready'更好,因爲如果'$(document).ready'被棄用,如果你已經使用'$功能(){..'。

+0

根據[.ready()'](http://api.jquery.com/ready/)的[jQuery doc],您的語法等價於'$(document).ready()'。 – jfriend00 2012-04-20 21:14:56

0
​​

其中..

context將是什麼this將參考實際函數內當它運行

arguments可以是單個值或值的數組/函數可以接收的對象作爲參數(如果有的話)。

0

嘗試

$(document).ready(function() { 
    // put all your jQuery goodness in here. 
});