vladkras

zoomer

Aug 29th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // зум фотки на странице детального просмотра
  2.     $('.zoomer').mouseenter(function(e) {
  3.         var $this = $(this), $img = $(this).children('img'),
  4.             containerHeight = $this.outerHeight(), containerWidth = $this.outerWidth();
  5.         // ratio
  6.         var kX, kY;
  7.         // fix container size
  8.         $this.css({
  9.             height: containerHeight+'px',
  10.             width: containerWidth+'px',
  11.             backgroundImage: 'url('+$img.attr("src")+')'
  12.         });
  13.         $img.hide();
  14.         // prepare image
  15.         $("<img/>").attr("src", $img.attr("src")).load(function() {
  16.             kX = (this.width-containerWidth)/containerWidth;  
  17.             kY = (this.height-containerHeight)/containerHeight;
  18.         });
  19.         // catch movement
  20.         $this.mousemove(function(e) {
  21.             var posX = e.pageX-$(this).offset().left,
  22.                 posY = e.pageY-$(this).offset().top;
  23.             var bgX = -1*posX*kX, bgY = -1*posY*kY;
  24.             $this.css('background-position', bgX+'px '+bgY+'px');
  25.         });
  26.         $this.mouseleave(function() {
  27.             $this.css('background-image', 'none');
  28.             $img.show();
  29.         });
  30.     });
Advertisement
Add Comment
Please, Sign In to add comment