$('body').on({ mousemove:
function(event) {
// Get mouse location relative to the element's top left-most point
var mouse_x = event.pageX - $(this).offset().left;
var mouse_y = event.pageY - $(this).offset().top;
// Get element's width and height
var element_w = $(this).width();
var element_h = $(this).height();
// Shortest side will be used for circle of opacity change
// We'll split it in half for later
// Is element tall or wide?
if(element_w <= element_h){
var shortside_len = Math.round(parseInt(element_w) / 2);
}
else{
var shortside_len = Math.round(parseInt(element_h) / 2);
}
// Find center of image x & y
var center_x = Math.round(parseInt(element_w) / 2);
var center_y = Math.round(parseInt(element_h) / 2);
/*
Treat the center of the element as x=0 y=0, but with no negative area
so we'll have to math that out
*/
// Mouse to the left of center
if(mouse_x <= center_x){
var plane_x = parseInt(center_x) - parseInt(mouse_x);
}
else{
// Mouse to right of center
var plane_x = parseInt(mouse_x) - parseInt(center_x);
}
// Mouse above center
if(mouse_y <= center_y){
var plane_y = parseInt(center_y) - parseInt(mouse_y);
}
else{
// Mouse below center
var plane_y = parseInt(mouse_y) - parseInt(center_y);
}
/*
Now we need to find the distance between the mouse
and the center of the element.
Thanks to http://www.mathopenref.com/coorddist.html for the formula.
Since we're only looking for the distance from the center
dx = plane_x and dy = plane_y
*/
var distance = Math.round(Math.sqrt(Math.pow(parseInt(plane_x), 2) + Math.pow(parseInt(plane_y), 2)));
/*
Now we're going to use the distance from the center of the element to
the closest edge to create a circle of opacity change. Any time the
mouse enters that circle, the opacity of the element will decrease as
the mouse gets closer to 0;
*/
// Find average between 0% and 100%...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.