g3x0

[jQuery] Hover Zoom Effect: Vertical & Horizontal

Oct 16th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.22 KB | None | 0 0
  1. <!--
  2.    Everything in here is required for the effect to work.
  3.    This is the source code for both the effects.
  4.  
  5.    author Gecko @RSTForums.com
  6. -->
  7.  
  8. <!-- Stylesheet -->
  9. <style>
  10. .container {
  11.     overflow: hidden;
  12.     width: 400px; /* Container width */
  13.     height: 400px; /* Container height */
  14. }
  15. .container img {
  16.     position: relative;
  17. }
  18. </style>
  19.  
  20. <!-- Markup -->
  21. <div class="container">
  22.     <img src="http://www.g3x0.com/rst/images/mago-huge.jpg" />
  23. </div>
  24.  
  25. <!-- jQuery -->
  26. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  27.  
  28. <!-- Hover Zoom Effect -->
  29. <script>
  30. $(function() {
  31.     var container = $(".container");
  32.    
  33.     container.mousemove(function(e) {
  34.         var parentOffset = $(this).offset();
  35.         var x = e.pageX - parentOffset.left;
  36.         var y = e.pageY - parentOffset.top;
  37.         var image = $(this).find('img');
  38.  
  39.         // Vertical
  40.         if (image.height() > $(this).height())
  41.             image.css({ top: - y / $(this).height() * image.height() + y + 'px' });
  42.        
  43.         // Horizontal
  44.         if (image.width() > $(this).width())
  45.             image.css({ left: - x / $(this).width() * image.width() + x + 'px' });
  46.     });
  47. });
  48. </script>
Advertisement
Add Comment
Please, Sign In to add comment