Solved

jQuery: Enlarge portion of image being hovered over

Posted on 2016-09-08
5
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 58

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…
Suggested Courses

623 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