I have a page with the gradient used when loading the page through CSS, and I would like to animate the page by alternating the colors of the gradient and the degree (linear gradient - 4 different colors, all worsening to white) when moving the mouse. If I use only 2 colors, it works fine. But I want to get a random color from the array when moving the mouse, but it flickers. Any solution for this?
Here is my fiddle
var colorArr = ['#dfa7ca', '#f7c2b3', '#bae0f1', '#a6d6cb'];
var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];
var grTo = '#FFFFFF';
$("body").mousemove(function( e ) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var grFrom = colorArr[Math.floor(Math.random()*colorArr.length)];
var xy = (x + y) / 8;
var w = $(this).width(),
pct = 360*(+e.pageX)/w,
bg = "linear-gradient(" + xy + "deg,"+grFrom+","+grTo+")";
$("body").css("background-image", bg);
});
source
share