JavaScript -Change CSS color for 5 seconds - How to add easing effect?


Tags: javascript,css,jquery-easing

Problem :

With the reference to this question:-

JavaScript -Change CSS color for 5 seconds

Working demo of the answer:-

http://jsfiddle.net/maniator/dG2ks/

I need to know how i can add an easing effect to it, so that slowly and slowly color get 100% opaque and similarly get 100% transperent.



Solution :

Code

function makeRGBStr(obj) {
    if (obj.a == null) return "rgb(" + obj.r + "," + obj.g + "," + obj.b + ")";
    else return "rgba(" + obj.r + "," + obj.g + "," + obj.b + "," + obj.a + ")";
}


window["highlight"] = function(obj, color) {
    var highlightColor = color || {
        "r": 255,
        "g": 0,
        "b": 0
    };


    var orig = obj.style.backgroundColor;
    var curAlpha = 1;
    obj.style.backgroundColor = makeRGBStr(highlightColor);
    setTimeout(function() {
        curAlpha -= 0.1;
        var newColor = highlightColor;
        newColor.a = curAlpha;
        obj.style.backgroundColor = makeRGBStr(newColor);

        if (curAlpha <= 0) {
            obj.style.backgroundColor = orig;
        }
        else {

            setTimeout(arguments.callee, 100);
        }
    });
}

jsFiddle: http://jsfiddle.net/dG2ks/32/

Some examples


    CSS Howto..

    In Python, I can print HTML to the browser, how can I ensure that the CSS that relates to it also get “printed”

    How to change CSS styling from within Angular 2 Component

    How can I make this jQuery effect responsive?

    How to create table with resizable row and columns like HTML, CSS, JS boxes on JSFiddle?

    How to wrap a vertical overflow horizontally in css?

    CSS: How to move contents of one div into another one

    How to set size of a checkbox in table vaadin

    How to organize CSS Sheets [closed]

    How to reach CSS zen?

    How to Change CSS style of the same element that is NOT Hovered

    How can I tell if a particular CSS property is inherited with jQuery?

    How to add bold text with CSS? [duplicate]

    How to Implement different backgrounds on scroll [closed]

    How to use CSS on HTML made by TextEdit?

    How to set the padding of QTableView cells through CSS?

    How do I make this CSS work to have semi-transparent border?

    Using bootstrap, how do you get columns to clear properly?

    How to override mixins in LESS CSS 1.4+

    How to make entire div change color on hover using css?

    How to set the specificity of a css over-ride class for XPages

    How to make table borders invisible with CSS

    How to set width and text-align for button with CSS

    How to add padding to the outside of an input in css

    How to set background-image to take remaining section of the page in semantic-ui

    how to make table header freeze/fixed when scrolling in html/css?

    How to remove CSS spaghetti in legacy web app?

    Pure CSS3 show/hide full height div with transition

    How to make link color change based on location on page?

    How to align slideshow element below header?

    activating “authorization” causes CSS stop working! how solve it?