Advertisement
Guest User

Untitled

a guest
Feb 14th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.85 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.       <meta charset="utf-8" />
  5.         <title>Slideshow</title>
  6.         <style type="text/css">
  7.             #slideshow
  8.             {
  9.                 border: 3px solid black;
  10.                 height: 400px;
  11.                 overflow: hidden;
  12.                 padding: 0;            
  13.                 width: 980px;
  14.             }
  15.             #slides
  16.             {
  17.                 margin: 0;
  18.                 padding: 0;
  19.                 height: 500px;             
  20.                 position: relative;
  21.                 width: 3600px; /* кол-во элементов на ширину каждого */
  22.             }          
  23.             #slides li
  24.             {
  25.                 float: left;
  26.                 height: 500px;
  27.                 list-style: none;
  28.                 margin: 0;
  29.                 width: 780px;
  30.             }
  31.             #slides li div
  32.             {
  33.                 height: 100%;
  34.             }
  35.             #slides li.red div { background-color: red; width: 780px;}
  36.             #slides li.green div { background-color: limegreen; width: 200px;}
  37.             #slides li.blue div { background-color: blue; width: 780px;}
  38.         </style>
  39.         <script type="text/javascript">
  40.             /* Конструктор. minSize - размер, до которого сжимаются боковые дивы в % */
  41.             function Slideshow(id, minSize) {
  42.                 this.ul = document.getElementById(id);;
  43.                 this.items = this.ul.getElementsByTagName('li');
  44.                 /* запоминаем нормальные размеры дива */
  45.                 this.normalHeight = window.getComputedStyle(this.items[0].childNodes[0]).pixelHeight;
  46.                 this.normalWidth = window.getComputedStyle(this.items[0].childNodes[0]).pixelWidth;
  47.                 /* насколько будем сдвигать список */
  48.                 this.width = this.items[0].clientWidth;
  49.                
  50.                 this.slide = function(speed) {
  51.                     var ul = this.ul,
  52.                             items = this.items,
  53.                           width = this.width;
  54.                    
  55.                     var currentLeft = ul.style.pixelLeft;
  56.  
  57.                     // выходим, если достигли конца списка
  58.                     if ((currentLeft >= 0 && speed > 0) || (currentLeft <= (3 - items.length) * width && speed < 0) || speed == 0) {
  59.                         return;
  60.                     }
  61.  
  62.                     // Находим дивы, размеры которых будут меняться при движении
  63.                     var index = Math.abs(currentLeft) / width - speed / Math.abs(speed);
  64.  
  65.                     var left = items[index].childNodes[0],
  66.                             center = items[index + 1].childNodes[0],
  67.                             right = items[index + 2].childNodes[0];
  68.  
  69.                     var normalHeight = this.normalHeight,
  70.                             normalWidth = this.normalWidth,
  71.                             /* вычисляем значения, на которые будут меняться размеры на каждой итерации (они округлены в меньшую по модулю сторону) */
  72.                             leftHeightDiff = Math.round(((minSize * normalHeight / 100) - window.getComputedStyle(left).pixelHeight) / Math.abs(width / speed)),
  73.                             leftWidthDiff = Math.round(((minSize * normalWidth / 100) - window.getComputedStyle(left).pixelWidth) / Math.abs(width / speed)),
  74.                             rightHeightDiff = Math.round(((minSize * normalHeight / 100) - window.getComputedStyle(right).pixelHeight) / Math.abs(width / speed)),
  75.                             rightWidthDiff = Math.round(((minSize * normalWidth / 100) - window.getComputedStyle(right).pixelWidth) / Math.abs(width / speed)),
  76.                             centerHeightDiff = Math.round((normalHeight  - window.getComputedStyle(center).pixelHeight) / Math.abs(width / speed)),
  77.                             centerWidthDiff = Math.round((normalWidth  - window.getComputedStyle(center).pixelWidth) / Math.abs(width / speed));
  78.  
  79.                     function makeStep() {
  80.                         // двигаем список
  81.                         ul.style.pixelLeft += speed;
  82.                         // меняем размеры
  83.  
  84.  
  85.                         if ((speed > 0 && ul.style.pixelLeft < (currentLeft + width)) || (speed < 0 && ul.style.pixelLeft > (currentLeft - width))) {
  86.                             setTimeout(function() { makeStep(); }, 10);
  87.                         } else {
  88.                             // корректируем размеры дивов
  89.                             left.style.height = minSize * normalHeight / 100 + 'px';
  90.                             left.style.width = minSize * normalWidth / 100 + 'px';
  91.                             right.style.height = minSize * normalHeight / 100  + 'px';
  92.                             right.style.width = minSize * normalWidth / 100 + 'px';
  93.                             center.style.height = normalHeight + 'px';
  94.                             center.style.width = normalWidth + 'px';
  95.                         }
  96.                     }
  97.  
  98.                     makeStep();
  99.                     return false;
  100.                 }
  101.             }
  102.        
  103.             function init() {
  104.                 slideshow = new Slideshow('slides', 100);
  105.                 /* Триггеры. Отрицательная скорость - слево, положительная - вправо. Скорости могут и не совпадать */
  106.                 document.getElementById("left").onclick = function() { slideshow.slide(-10); };
  107.                 document.getElementById("right").onclick = function() { slideshow.slide(20); };
  108.             }      
  109.         </script>
  110.     </head>
  111.     <body onload="init()">
  112.         <div id="slideshow">
  113.             <!-- первый и последний дивы пусты -->
  114.             <ul id="slides">
  115.                 <li class="red"><div>тест1</div></li>
  116.                 <li class="green"><div>тест2</div></li>
  117.                 <li class="blue"><div>тест3</div></li>
  118.                 <li><div></div></li>
  119.             </ul>
  120.         </div>
  121.         <a href="#" id="left">Left</a>
  122.         <a href="#" id="right">Right</a>
  123.     </body>
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement