Solved

jQuery: Enlarge portion of image being hovered over

Posted on 2016-09-08
5
40 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 51

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

759 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

22 Experts available now in Live!

Get 1:1 Help Now