Exactly 50px, to be precise. I know it’s in the code somewhere, but I think i’ve been staring at it for too long.
Backstory: I’m making a simple jQuery plugin which will add a magnifying lens effect when hovering over images. It was working fine, then I added some code, and it started acting odd when entering from the top of the image. I used firebug to see if there was a difference between the top offset of the image and the pageY value of the cursor and there was.
In the link below you can see that moving in from above it doesn’t activate on time.
Code:
update: function(event) {
img.stop(); // To stop the image from constantly animating
img.lens.stop(); // Same for the lens
img.lens.leftmax = img.width + img.left;
img.lens.topmax = img.height + img.top;
img.lens.x = event.pageX - (vars.size / 2);
img.lens.y = event.pageY - (vars.size / 2);
img.lens.css({
left: img.lens.x + 'px',
top: img.lens.y + 'px'
});
console.log(img.top + ' : ' + event.pageY);
if (event.pageX >= img.lens.leftmax || event.pageY >= img.lens.topmax || event.pageX <= img.left || event.pageY <= img.top) {
img.lens.hide(); // Hide lens
if (options.caption) {
img.lens.caption.hide();
} // Hide Caption
if (options.overlay) {
methods.nooverlay();
}
// API Callback Exit
vars.onexit(img, img.lens);
} else {
// API Callback on entering
vars.onenter(img, img.lens);
if (options.overlay) {
methods.overlay();
}
methods.doMath(event); // Recalculate pos
img.lens.show(); // Show lens
img.lens.css({
backgroundPosition: img.lens.bgleft + 'px ' + img.lens.bgtop + 'px'
}); // Assign coordinates
if (options.caption) {
img.lens.caption.show(); // Show caption
img.lens.caption.x = img.lens.x + ((vars.size - img.lens.caption.outerWidth()) / 2); // Center caption
img.lens.caption.css({
left: img.lens.caption.x + 'px',
top: img.lens.y + vars.size + 'px'
}); // Assign coordinates
}
}
},
doMath: function(event) {
img.wr = img.Owidth / img.width;
img.hr = img.Oheight / img.height;
img.lens.bgleft = String(((event.pageX - img.left) * img.wr - vars.size / 2) * (-1));
img.lens.bgtop = String(((event.pageY - img.top) * img.hr - vars.size / 2) * (-1));
},
These are the main functions (part of a map) that make the hover action work and where i think the problem should lie. The code is pretty long, so for the sake of readability i’ll add a link to where i’m hosting the page in case you want to look at the full plugin code.
Link removed
Where am I going wrong?
EDIT:
So apparently the cause of the problem is a floating span element that is nested in a sibling of the parent, of the parent tag of the image:
<div class="header">
<!-- This span tag -->
<span class="note">→ If you're using IE8 or lower, you will see a square lens</span>
<h1>jQuery Images Lens Preview</h1>
<span><i>The most customizable zoom tool there is.</i> <a href="#">GET IT NOW!</a></span>
<div style="clear: both;"></div>
</div>
<div class="example">
<div class="desc">
<h3>Demonstration #1 - Basic Usage</h3>
<pre>$('img.basic').superLens();</pre>
</div>
<div class="theimages">
<img src="images/demo_img1.jpg" width="214" height="320" class="basic" />
<img src="images/demo_img2.jpg" width="214" height="320" class="basic" />
</div>
<div style="clear: both;"></div>
</div>
Any idea why this is happening?
It doesn’t actually return a 50px difference.
Basically the
img.topis the offset from thedocumentto the top of the image. Theevent.pageYis the offset of the cursor to the top of thedocument, as the hover event is the exact point of the cursor, not the point of the hovered object.If you want, you could use
event.targetand get the top value from that.