Skip to content

Instantly share code, notes, and snippets.

@egstad
Created May 11, 2015 20:37
Show Gist options
  • Save egstad/33696f342c2d7c6399b2 to your computer and use it in GitHub Desktop.
Save egstad/33696f342c2d7c6399b2 to your computer and use it in GitHub Desktop.
Changing background color based on cursor position
// background color change according to cursor position
$(document).mousemove(function (e) {
var $width = ($(document).width()) / 255;
var $height = ($(document).height()) / 255;
var $pageX = parseInt(e.pageX / $width, 10);
var $pageY = parseInt(e.pageY / $height, 10);
// rgba($a-$b-$c);
var $a = $pageX,
$b = ($pageY - 255) * (-1),
$c = 0;
// if cursor is in bottom left, adjust $c accordingly
if ($pageX <= $pageY) {
$c = $pageY - $pageX;
}
// change element to whatever you need to target
$(element).css('background-color', 'rgb('+$a+','+$b+','+$c+')');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment