开发者

How to rewrite this javascript code using only jQuery instead

I am a beginner at jQuery, so I had a friend help me write some script for an effect I was having trouble with. Problem, is he is pretty much only familiar with Javascript and doesn't know how to translate this same script into jQuery. Is there a way to write this same code into 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 abo开发者_JS百科ve)
    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);
}


Here you go:

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);    
    });
});

Btw, you can use a regular for loop instead of $.each:

for (var i = 0; i < ids.length; i++) {
    setTimeout(function() { animateText(ids[i], timing); }, delay * i);
}

A regular loop preforms slightly faster, but is also slightly more ugly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜