我是jQuery的初学者,所以我有一个朋友帮我写一些剧本,因为我遇到了麻烦。问题是,他几乎只熟悉Javascript,并且不知道如何将这个相同的脚本翻译成jQuery。有没有办法将相同的代码写入jQuery?
$(document).ready(function() {
//milliseconds - change this value to modify the delay between
//one text animation and the other
var delay = 1000;
//milliseconds - change this value to modify the single text
//animation speed
var timing = 2000;
animateText('creative_div', timing);
//setTimeout allow to call the function animateText after a
//certain delay (declared above)
setTimeout("animateText('motivated_div'," + timing + ");", delay);
setTimeout("animateText('skilled_div'," + timing + ");", delay * 2);
});
function animateText(divElement, timing) {
//$(divElement).style="visibility: visible";
document.getElementById(divElement).style.visibility = "visible";
$('#'+divElement).effect('drop',
{
direction:"up",
mode:"show",
distance:"400"
},
timing);
}
答案 0 :(得分:1)
你走了:
function animateText(id, t) {
$('#' + id)
.css('visibility', 'visible')
.effect('drop', {direction: 'up', mode: 'show', distance: '400'}, t);
}
$(function() {
var delay = 1000,
timing = 2000,
ids = ['creative_div', 'motivated_div', 'skilled_div'];
$.each(ids, function(i, id) {
setTimeout(function() { animateText(id, timing); }, delay * i);
});
});
顺便说一句,您可以使用常规for循环而不是$ .each:
for (var i = 0; i < ids.length; i++) {
setTimeout(function() { animateText(ids[i], timing); }, delay * i);
}
常规循环预制稍微快一些,但也稍微丑陋。