Solved

jQuery: Enlarge portion of image being hovered over

Posted on 2016-09-08
5
47 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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now