Solved

jQuery: Enlarge portion of image being hovered over

Posted on 2016-09-08
5
64 Views
Last Modified: 2016-09-08
I want the enlarged image to display the portion of the image that is being moused-over.

In this code, the enlarged image does not correspond to the hover.

https://jsfiddle.net/yqrvmszx/

<!DOCTYPE html>
<html lang="en">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css">

#img {
margin: 10px auto;
border: 0;
width: 400px;
display: block;
cursor: crosshair;
}

#imageLarge {
margin: 100px auto;
border: 1px solid #111;
width: 300px;
height: 350px;
background: #888 url('http://www.adas.uk/Portals/0/EasyDNNnews/102/102GreatCrestedNewt.jpg') no-repeat scroll 0 0;
}

</style>

<script type="text/javascript">
$(document).ready(function() {

 $('#img').mousemove(function( event ) {
  $('#imageLarge').css('background-position', ((event.pageX-$(this).position().left) / $(this).width() * 100) + '% ' + ((event.pageY-$(this).position().top) / $(this).height() * 100) + '% ');
 });
});

</script>
</head>
<body>
 <img src="http://www.adas.uk/Portals/0/EasyDNNnews/102/102GreatCrestedNewt.jpg" id="img" alt="" />
 <div id="imageLarge"></div>
</body>
</html>

Open in new window

0
Comment
Question by:skij
  • 3
5 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41790136
Why reinvent the wheel?

Try one of the jQuery image zoom plugins:
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=jquery%20image%20zoom
0
 
LVL 10

Author Comment

by:skij
ID: 41790152
That plugin is 1790 lines long!  Why use a 60kb plugin when you could accomplish the same think with less than 10 lines of code?
1
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41790156
Well, it looks to me like your calculations are incorrect. I believe you should also be using screenX and screenY vs. pageX and pageY.
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41790201
Try using offset instead of position
$(document).ready(function() {
 $('#img').mousemove(function( event ) {
  $('#imageLarge').css('background-position', ((event.pageX-$(this).offset().left) / $(this).width() * 100) + '% ' + ((event.pageY-$(this).offset().top) / $(this).height() * 100) + '% ');
 });
});

Open in new window

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41790216
Julian should get the points.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question