jQuery animate “text-shadow” css property

前端 未结 2 1463
说谎
说谎 2021-02-20 13:05

So as i\'ve worked with jQuery\'s .animate() method i\'ve learned that in order to animate the left margin you would have to use something like this:



        
相关标签:
2条回答
  • 2021-02-20 13:39

    After looking around for a while and realizing that pretty much all solutions out there are for older versions of JQuery I gave up and wrote these functions that mostly do the trick for a 500ms fade in or fade out:

        function AddShadow(ControlID)
        {
            for (i = 0; i < 11; i++)
            {
                setTimeout('$("#' + ControlID + '").css("text-shadow", "0 0 ' + i + 'px White");', i * 50);
            }
        }
    
        function RemoveShadow(ControlID)
        {
            for (i = 0; i < 11; i++)
            {
                setTimeout('$("#' + ControlID + '").css("text-shadow", "0 0 ' + (10 - i) + 'px White");', i * 50);
            }
        }
    

    Then you just implement it with a JQuery hover handler like this:

    $('.class').hover(function () {AddShadow($(this).attr('id'))}, function () {RemoveShadow($(this).attr('id'))};
    

    The only thing I don't like about them is that if you hover over the object quickly it will flicker as it alternates between the two functions, but at least it always leaves them in a final state of being un-faded.

    My implementation wasn't that critical so I didn't go any further, but this could theoretically be overcome by adding a flag property to the object that sets whatever the latest action is, and then have the function check the flag each time it performs a step.

    Other reasons it's not ideal:

    • It is not very intuitive to change the timing or level since you have to mess with both the loop and the multiplier
    • It only does linear steps
    • It's probably not a very efficient way of doing it.
    • Any control you want to use these functions for has to have an ID or it won't work.

    But on the bright side, it should work with every JQuery version and it's stable.

    0 讨论(0)
  • 2021-02-20 13:42

    CSS transitions are the best way for this, as every browser in common usage that supports text-shadow also supports transitions.

    In that case, you just set the transition properties, then change the style either with JS or by changing the class.

    Basic example: http://jsfiddle.net/adZ42/1/

    More info on retrofitting this into jQuery: http://css3.bradshawenterprises.com/legacy/

    0 讨论(0)
提交回复
热议问题