文档加载后如何加载此javascript函数?
基本上,我正在尝试设置背景位置的动画,并让它在div.符号中解析的值上停止 通过以下方式包装代码:文档加载后如何加载此javascript函数?,javascript,jquery,animation,background,Javascript,Jquery,Animation,Background,基本上,我正在尝试设置背景位置的动画,并让它在div.符号中解析的值上停止 通过以下方式包装代码: 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 =
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)
}}
看起来您正在使用jQuery,下面是jQuery解决方案:
$(document).ready(function(){
// the call goes here
})
这只是
$(function() {
// your code here
});
您可以将其包装为:
Use.ready()
如果要在加载文档后运行此操作,请尝试以下操作:
$(document).ready(function(){
j.largeSign();
});
我发现使用“$(function(){”比使用“$(document).ready”更好,因为如果“$(document).ready”曾经被弃用,那么如果您已经在使用“$(function(){..”,则不必更改任何代码
在哪里
上下文
将是实际函数运行时该所指的内容
及
参数
可以是单个值,也可以是函数可以作为参数接收的值/对象数组(如果有)。试试看
$(function(){
j.largeSign.apply(context, arguments);
});
我最喜欢这个选项。如果您使用setInterval或其他上下文来调用函数,如果它是在document ready(文档准备就绪)的范围内定义的,您将无法访问它。最好先定义它,然后等到文档准备就绪后再运行它。@ElliotBonneville在调用函数方面有点不同用户提供了。OP调用函数并不难。:p“this”在函数中指的是什么,“a”是什么意思?如果需要正确提供,请参阅我的答案。根据,您的语法相当于
$(document).ready()
。为什么要将jquery传递到匿名函数中?这只是一种没有冲突的方式。请参阅提供的链接中的jquery文档。代码片段来自它。
$(document).ready(function(){
j.largeSign();
});
$(function(){
//put your code here
j = {largeSign: function(a) {...
});
$(function(){
j.largeSign.apply(context, arguments);
});
$(document).ready(function() {
// put all your jQuery goodness in here.
});