0

我正在使用下面的代码制作一些动画。但是我需要用跨度包装第一个单词,这样我就可以更改第一个单词的颜色。我将如何添加跨度?

jQuery 一点也不强,所以任何帮助都将不胜感激。谢谢你。

jQuery:

$(function() {
    var num = 0;
    var words = [
        {left: "Slam Your Face", right: "Watch Out"},
        {left: "Death", right: "Death"}
     ];    

    var fadel = $('#fadeL');
    var fader = $('#fadeR');

    animate();

    function animate() {
        var leftword = words[num].left;
        var rightword = words[num].right;

    fadel
        .stop()
        .css('top', '18px')
        .css('left', '0')
        .css('opacity', 0)
        .text(leftword).show();

    fader
        .stop()
        .css('top', '68px')
        .css('left', '400px')
        .css('opacity', 0)
        .text(rightword).show();

    fadel.animate({
        opacity: 1,
        left: (350 - fadel.width()) + 'px'
        }, 3000, function() {
            fadel.fadeOut("fast", function() {
            queueNext();
        });
    });

    fader.animate({
        opacity: 1,
        left: '250px'
    }, 3000, function() {  
            fader.fadeOut("fast");
        });
    }

    function queueNext() {
        if (++num == words.length)
        num = 0;

        animate();
    }
});
4

2 回答 2

0

见小提琴:http: //jsfiddle.net/YxMHx/6/

$(function() {
    var num = 0;
    var someConditionIsTrue = true;
    var words = [
        {left: "Slam Your Face", right: "Watch Out"},
        {left: "Death", right: "Death"}
     ];    

    var fadel = $('#fadeL');
    var fader = $('#fadeR');

    animate();

    function animate() {
        var leftword = words[num].left;
        var rightword = words[num].right;

        var first = leftword.slice(0, leftword.indexOf(" "));

        first = "<span class=\"span_class\">" + first + "</span>";
        var lastWords = leftword.substring(leftword.indexOf(" ") + 1);

        var finalString = first + lastWords;


    fadel
        .stop()
        .css('top', '18px')
        .css('left', '0')
        .css('opacity', 0)
        .html(finalString).show().delay(2000);

    fader
        .stop()
        .css('top', '68px')
        .css('left', '400px')
        .css('opacity', 0)
        .text(rightword).show();

    fadel.animate({
        opacity: 1,
        left: (350 - fadel.width()) + 'px'
        }, 3000, function() {
            fadel.fadeOut("fast", function() {
            queueNext();
        });
    });

    fader.animate({
        opacity: 1,
        left: '250px'
    }, 3000, function() {  
            fader.fadeOut("fast");
        });
    }

    function queueNext() {

        if (++num == words.length)
        num = 0;

        animate();
    }

    queueNext();
});
于 2013-04-01T17:55:06.807 回答
0
function spanFirstWord(str) {
    return str.replace(/^(\w+)/, '<span class="first">$1</span>');
}

function animate() {
    var leftword = spanFirstWord(words[num].left);
    var rightword = spanFirstWord(words[num].right);

fadel
    .stop()
    .css('top', '18px')
    .css('left', '0')
    .css('opacity', 0)
    .html(leftword).show();

fader
    .stop()
    .css('top', '68px')
    .css('left', '400px')
    .css('opacity', 0)
    .html(rightword).show();

fadel.animate({
    opacity: 1,
    left: (350 - fadel.width()) + 'px'
    }, 3000, function() {
        fadel.fadeOut("fast", function() {
        queueNext();
    });
});
于 2013-04-01T18:00:54.733 回答