Share/Bookmark

Featured Posts

SharePoint Ticker Web Part from Existing Data Ever wanted to create a ticker in SharePoint without having to buy the $99 add-on from Microsoft, well here's the code: Simply create a new Web Part using the 'Content Editor' and open the 'Source Editor...'...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Whats next for Nintendo? Today I noticed that Nintendo have had falling profits for 2010 and predict even less sales for next year. The Wii 2 hasn`t been officially announced and from what I can see is not really on the radar....

Read more

7 Great Free Android Apps The Android mobile operating system is both fast, reliable and can do everything that the Apple OS can and a little more (all for normally a fraction of the price.) plus it supports Flash and soon Flash...

Read more

Move a background image in relation to the mouse

Posted by matthew | Posted in Code Snippets | Posted on 19-02-2010

Tags: ,

0

To add a bit of flare to your site you may want to move a background image in relation to where the users cursor is, this can create some very cool animation.

Using this code snippet can produce some very funky results, if could use it for accessibility highlighting where to mouse is for instance, or to create a crude animation.

It works very well when you just want to add a bit of dynamic movement to the site but don’t want to use flash because of accessbility issues.

We tend to find it works best with tiled backgrounds, as you then never see the clipping where the image ends and it makes it look perspective.

First of all add an id and the initial style to your element, in our example we are going to dynamically move the body background image so we add this:


<body id="doc" style="background-position:0px 0px">

Then simply include this piece of lovely JavaScript:


var IE = document.all?true:false

if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft
tempY = event.clientY + document.body.scrollTop
} else {  // grab the x-y pos.s if browser is NS
tempX = e.pageX
tempY = e.pageY
}

if (tempX < 0){tempX = 0}
if (tempY < 0){tempY = 0}

var pos = tempX + "px " + tempY + "px";

document.getElementById('doc').style.backgroundPosition = pos;

}

If you need to you can change the id of the element with the background image or even include delays to create momentum etcetera.

Write a comment