Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.  
  3.     // Cesty
  4.     var classBase           = '.orbit';
  5.     var classMove           = classBase + ' .move';
  6.     var classZoom           = classBase + ' .zoom';
  7.     var classItem           = classBase + ' .item';
  8.     var classSource         = classBase + ' .source';
  9.     var classMask           = classBase + ' .mask';
  10.            
  11.     // Nastaveni
  12.    
  13.         // Rozmery
  14.         var baseWidth           = 800;
  15.         var baseHeight          = 400;
  16.  
  17.         // Limit zoomu
  18.         var zoomInLimit         = 200;
  19.         var zoomOutLimit        = 50;
  20.    
  21.         // Rychlost zoomu
  22.         var zoomSpeed           = 20;
  23.  
  24.         // Vychozi obrazek
  25.         var startImageIndex    = 40; // Pokud je vetsi nez posledni indexovany, nastavi se na 0
  26.  
  27.     // Vychozi stavy eventu
  28.     var isMouseWheelUp, isMouseWheelDown, isMouseLeftDown, isMouseRightDown, isMouseMove, isMouseMoveLeft, isMouseMoveRight, mouseMovePrev;
  29.  
  30.     // CSS
  31.     $(classBase).css({
  32.         'width'     : baseWidth,
  33.         'height'    : baseHeight,
  34.         'position'  : 'relative',
  35.         'overflow'  : 'hidden',
  36.     });
  37.     $(classZoom + ', ' + classMove).css({
  38.         'width'     : '100%',
  39.         'height'    : '100%',
  40.         'position'  : 'absolute',
  41.         'left'      : 0,
  42.         'top'       : 0,
  43.     });
  44.     $(classItem + ' img').hide();
  45.     $(classSource).hide();
  46.                
  47.     // Prvni a posledni obrazek
  48.     var firstImageIndex = 0;
  49.     var lastImageIndex = 0;
  50.  
  51.     // Index obrazku
  52.     $(classSource + ' img').each(function(index, value) {
  53.         lastImageIndex++; // Aktualizace posledniho obrazku
  54.         $(this).attr('id', 'orbit-index-'+index);
  55.     });
  56.    
  57.     // Prvni aktivni obrazek
  58.     var activeImageIndex = ((startImageIndex !== 0 && startImageIndex <= lastImageIndex) ? startImageIndex : firstImageIndex);
  59.     var firstImageSrc = $(classSource + ' img#orbit-index-'+activeImageIndex+'').attr('src');
  60.  
  61.     // Nezbytne HTML
  62.     $(classBase).append('<div class="move"><div class="zoom"><div class="item" style="background-image:url('+firstImageSrc+');"><img src="'+firstImageSrc+'"></div></div></div>');
  63.  
  64.     // Funkce rotace
  65.     function orbitRotate(data) {
  66.        
  67.         // Aktivni obrazek
  68.  
  69.         var activeImageSrc = $(classSource + ' img#orbit-index-'+activeImageIndex+'').attr('src');
  70.  
  71.         // Dalsi
  72.         if(data == 'next') {
  73.             if(activeImageIndex == lastImageIndex) {
  74.                 activeImageIndex = firstImageIndex;
  75.             } else {
  76.                 activeImageIndex++;
  77.             }
  78.         }
  79.    
  80.         // Predchozi
  81.         if(data == 'prev') {
  82.             if(activeImageIndex == firstImageIndex) {
  83.                 activeImageIndex = lastImageIndex;
  84.             } else {
  85.                 activeImageIndex--;
  86.             }
  87.         }
  88.                
  89.         $(classItem).css('background-image', 'url('+activeImageSrc+')');
  90.         $(classItem + ' img').attr('src', activeImageSrc);
  91.                
  92.     }
  93.    
  94.     // Funkce priblizeni a oddaleni
  95.     function orbitZoom(data) {
  96.  
  97.         // Kontrola nastaveni
  98.         if(zoomInLimit < 100) zoomInLimit = 100;
  99.         if(zoomOutLimit > 100) zoomInLimit = 100;
  100.            
  101.         // Puvodni velikost
  102.         var origWidth = $(classZoom).parent().width();
  103.         var origHeight = $(classZoom).parent().height();
  104.    
  105.         // Stavajici velikost
  106.         var curWidth = $(classZoom).width();
  107.         var curHeight = $(classZoom).height();
  108.  
  109.         // Koeficient rychlosti
  110.         var cWidth = (curWidth / zoomSpeed);
  111.         var cHeight = (curHeight / zoomSpeed);
  112.  
  113.         // Maxinalni, minimalni sirka a vyska
  114.         var maxWidth = ((origWidth / 100) * zoomInLimit);
  115.         var maxHeight = ((origHeight / 100) * zoomInLimit);
  116.         var minWidth = ((origWidth / 100) * zoomOutLimit);
  117.         var minHeight = ((origHeight / 100) * zoomOutLimit);
  118.  
  119.         // Pusteni dle smeru
  120.         if(data == 'in') {
  121.             curWidth = Math.round(curWidth + cWidth);
  122.             curHeight = Math.round(curHeight + cHeight);
  123.         }
  124.         if(data == 'out') {
  125.             curWidth = Math.round(curWidth - cWidth);
  126.             curHeight = Math.round(curHeight - cHeight);
  127.         }
  128.    
  129.         // Kontrola a zapis CSS
  130.         if (maxWidth > curWidth && maxHeight > curHeight && minWidth < curWidth && minHeight < curHeight) {
  131.             $(classZoom).css({
  132.                 'width'         : curWidth,
  133.                 'height'        : curHeight,
  134.                 'left'          : '50%',
  135.                 'top'           : '50%',
  136.                 'margin-left'   : -(curWidth / 2),
  137.                 'margin-top'    : -(curHeight / 2),
  138.             });
  139.         }
  140.  
  141.     }
  142.            
  143.     // Funkce pohyb
  144.     function orbitMove(pageX, pageY) {
  145.         $(classMove).css({
  146.             'left' : pageX - $(this).outerHeight() / 2,
  147.             'top' : pageY - $(this).outerHeight() / 2,
  148.         });
  149.     }
  150.  
  151.  
  152.  
  153.    
  154.    
  155.    
  156.    
  157.     // Event (mousewheel)
  158.     $(classBase).on('mousewheel DOMMouseScroll wheel', function(e){
  159.        
  160.         // Plneni promennych dle cinosti
  161.         if(e.type == 'mousewheel' || e.type == 'wheel') { // IE, Safari, Chrome, Opera
  162.             if(e.originalEvent.wheelDelta < 0) {
  163.                 isMouseWheelDown = true;
  164.                 isMouseWheelUp = false;
  165.             } else if (e.originalEvent.wheelDelta > 0) {
  166.                 isMouseWheelUp = true;
  167.                 isMouseWheelDown = false;
  168.             }
  169.         }
  170.         if(e.type == 'DOMMouseScroll') { // Firefox
  171.             if(e.originalEvent.detail > 0) {
  172.                 isMouseWheelDown = true;
  173.                 isMouseWheelUp = false;
  174.             } else if (e.originalEvent.detail < 0) {
  175.                 isMouseWheelUp = true;
  176.                 isMouseWheelDown = false;
  177.             }
  178.         }
  179.                
  180.                 // Vynulovani promennych pri necinosti
  181.                 clearTimeout($.data(this, 'stopTimer'));
  182.                 $.data(this, 'stopTimer', setTimeout(function() {
  183.                     isMouseWheelUp = false;
  184.                     isMouseWheelDown = false;
  185.                 }, 100));
  186.                
  187.                 // Spousteni funkci
  188.                 if(isMouseWheelUp == true) orbitZoom('in');
  189.                 if(isMouseWheelDown == true) orbitZoom('out');
  190.                
  191.             });
  192.  
  193.             // Event (mousewheel)
  194.             $(classBase).on('mousedown mouseup mousemove contextmenu', function(e){
  195.        
  196.                 // Mousedown, mouseup
  197.                 if(e.type == 'mousedown') {
  198.                     if(e.which == 1) isMouseLeftDown = true;
  199.                     if(e.which == 3) isMouseRightDown = true;
  200.                 }
  201.                 if(e.type == 'mouseup') {
  202.                     if(e.which == 1) isMouseLeftDown = false;
  203.                     if(e.which == 3) isMouseRightDown = false;
  204.                 }
  205.                 if(e.type == 'mousemove') {
  206.                     isMouseMove = true;
  207.                     if(mouseMovePrev < e.pageX) {
  208.                         isMouseMoveLeft = false;
  209.                         isMouseMoveRight = true;
  210.                     } else {
  211.                         isMouseMoveLeft = true;
  212.                         isMouseMoveRight = false;
  213.                     }
  214.                     mouseMovePrev = e.pageX;
  215.        
  216.                     // Vynulovani promennych pri necinosti
  217.                     clearTimeout($.data(this, 'stopTimer'));
  218.                     $.data(this, 'stopTimer', setTimeout(function() {
  219.                         isMouseMove = false;
  220.                         isMouseMoveLeft = false;
  221.                         isMouseMoveRight = false;
  222.                     }, 100));
  223.    
  224.                 }
  225.                 if(e.type == 'contextmenu') {
  226.                     return false;
  227.                 }
  228.                
  229.                 // Spousteni funkci
  230.                 if(isMouseLeftDown && isMouseMoveLeft) orbitRotate('next');
  231.                 if(isMouseLeftDown && isMouseMoveRight) orbitRotate('prev');
  232.                 // if(isMouseRightDown && isMouseMove) orbitMove(e.pageX, e.pageY);
  233.                
  234.             });
  235.  
  236.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement