2

I am trying to make the background change colour based on idle / movement.

that works, but i cannot get it to fade using fadeIn.

JSFiddle - Working example without the fade

What is the best solution to fade the backgrounds at the point of

$("body").css('background-color','#000');

 $(document).ready(function () {
        var idleState = false;
        var idleTimer = null;
        $('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
            clearTimeout(idleTimer);
            if (idleState == true) { 

                    $("body").css('background-color','#fff'); 

            }
            idleState = false;
            idleTimer = setTimeout(function () { 

                    $("body").css('background-color','#000');

                idleState = true; }, 2000);
        });
        $("body").trigger("mousemove");
});
Derple
  • 847
  • 10
  • 28

2 Answers2

6

You can use animate instead of css to achieve what you want.

 $('body').animate({"background-color":"#fff"}, 1000);
Robin Carlo Catacutan
  • 12,366
  • 11
  • 50
  • 83
2

Use CSS.

body {
    transition: background-color 1s;
}
Ruslanas Balčiūnas
  • 6,765
  • 3
  • 22
  • 40