Simple JavaScript Animation for 1 Second to Highlight Text?

后端 未结 3 1363
小蘑菇
小蘑菇 2021-01-21 10:59

I searched by google but I found that much I can\'t overview it all, so I need your experience:

I need to highlight a text for 1 second. For example a simple \"blink\"-e

3条回答
  •  伪装坚强ぢ
    2021-01-21 11:56

    function highlightFor(id,color,seconds){
        var element = document.getElementById(id)
        var origcolor = element.style.backgroundColor
        element.style.backgroundColor = color;
        var t = setTimeout(function(){
           element.style.backgroundColor = origcolor;
        },(seconds*1000));
    }
    

    should work. Supply it with the id of the element, the highlight color and the time you want the highlight to display for (in seconds). For example

    highlightFor('object','yellow',3);
    

    EDIT: As far as colours set in stylesheets is concerned, I strongly suggest using a javascript library (like jQuery). I know you say that you've had some problems with it, but it's most likely there's a tiny bug in the code you've written that's giving you that opinion. Just ask on SO if you have any questions about jQuery!

提交回复
热议问题