Change text (html) with .animate

后端 未结 7 2102
灰色年华
灰色年华 2020-12-23 20:04

I am trying to animate the html part of a tag ( This Text! ) using jQuery\'s Animate function, like so:

7条回答
  •  星月不相逢
    2020-12-23 20:29

    Following the suggestion by JiminP....

    I made a jsFiddle that will "smoothly" transition between two spans in case anyone is interested in seeing this in action. You have two main options:

    1. one span fades out at the same time as the other span is fading in
    2. one span fades out followed by the other span fading in.

    The first time you click the button, number 1 above will occur. The second time you click the button, number 2 will occur. (I did this so you can visually compare the two effects.)

    Try it Out: http://jsfiddle.net/jWcLz/594/

    Details:

    Number 1 above (the more difficult effect) is accomplished by positioning the spans directly on top of each other via CSS with absolute positioning. Also, the jQuery animates are not chained together, so that they can execute at the same time.

    HTML

    Hello


    CSS

    .onTopOfEachOther {
        position: relative;
    }
    .onTopOfEachOther span {
        position: absolute;
        top: 0px;
        left: 0px;
    }
    

    JavaScript

    $('#btnTest').click(function() {        
        fadeSwitchElements('a', 'b');    
    }); 
    
    function fadeSwitchElements(id1, id2)
    {
        var element1 = $('#' + id1);
        var element2 = $('#' + id2);
    
        if(element1.is(':visible'))
        {
            element1.fadeToggle(500);
            element2.fadeToggle(500);
        }
        else
        {
             element2.fadeToggle(500, function() {
                element1.fadeToggle(500);
            });   
        }    
    }
    

提交回复
热议问题