Nick Craver beat me to answer for about 10 minutes, but this is my code for this, using background-image to put the image in place of the actual image.
var img = $('#outer'), imgWidth = 1600, imgHeight = 1200, eleWidth = img.width(), eleHeight = img.height(), offsetX = img.offset().left, offsetY = img.offset().top, moveRatioX = imgWidth / eleWidth - 1, moveRatioY = imgHeight / eleHeight - 1; img.mousemove(function(e){ var x = imgWidth - ((e.pageX - offsetX) * moveRatioX), y = imgHeight - ((e.pageY - offsetY) * moveRatioY); this.style.backgroundPosition = x + 'px ' + y + 'px'; });
There are a huge number of variables, because the mousemove event mousemove should be as efficient as possible. This is a bit more restrictive because you need to know the sizes, but I think the code can be easily changed to work with img , for which the size can be easily calculated.
A simple demonstration of this: http://www.jsfiddle.net/yijiang/fq2te/1/
Yi jiang
source share