Chinaunix首页 | 论坛 | 博客
  • 博客访问: 197937
  • 博文数量: 95
  • 博客积分: 2383
  • 博客等级: 大尉
  • 技术积分: 1135
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 15:14
文章分类
文章存档

2012年(84)

2011年(11)

我的朋友
最近访客

分类: 系统运维

2012-01-21 11:03:03

 

这篇文章介绍了如何使用幻灯方式来进行全屏幕页面导航,包括了16个幻灯,4个一行,一共4行。并且通过了CSS3箭头添加了动画导航效果。而且提供一个迷你的导航条能够帮助用户记录访问过的幻灯页面。希望大家喜欢!

Javascript代码
  1. var navi = {
  2.     // variables
  3.     aPages : [],
  4.     aLinks : [],
  5.     tween : {},
  6.     oParent : null,
  7.     oPages : null,
  8.     bRunning : null,
  9.     sTargPage : null,
  10.     sCurPage : null,
  11.     bHash : null,
  12.     sOldH : null,
  13.     bUpdateH : true,

  14.     // initialization
  15.     init : function (aParams) {
  16.         this.oPages = document.getElementById(aParams.pages_id);
  17.         this.oParent = document.getElementById(aParams.parent_id);

  18.         var aAEls = this.oParent.getElementsByTagName('a');
  19.         var i = 0; var p = null;
  20.         while (p = aAEls[i++]) {
  21.             if (p.className && p.className.indexOf('go') >= 0) {
  22.                 var sHref = p.href.split('#')[1];
  23.                 var oDst = document.getElementById(sHref);
  24.                 if (oDst) {
  25.                     // fill-in pages array
  26.                     this.aPages[sHref] = {
  27.                         oObj: oDst,
  28.                         iXPos: -oDst.offsetLeft,
  29.                         iYPos: -oDst.offsetTop
  30.                     };

  31.                     // fill-in links array
  32.                     this.aLinks.push({a: p, oObj: oDst});

  33.                     p.onclick = function () {
  34.                         navi.goto(this.href.split('#')[1], aParams.duration);
  35.                         return false;
  36.                     }
  37.                 }
  38.             }
  39.         }

  40.         this.resize();

  41.         if ('onhashchange' in window) {
  42.             if (location.hash !== '' && location.hash !== '#') {
  43.                 this.sOldH = location.hash.substring(1);
  44.                 this.goto(this.sOldH, -1);
  45.             } else
  46.                 this.goto('page1', -1);
  47.             this.bHash = true;

  48.             window.onhashchange = function() {
  49.                 if (location.hash.substring(1) !== navi.sOldH) {
  50.                     navi.sOldH = location.hash.substring(1);
  51.                     if (navi.sOldH == '') {
  52.                         navi.bUpdateH = false;
  53.                     }
  54.                     navi.goto(navi.sOldH, aParams.duration);
  55.                 }
  56.                 return false;
  57.             }
  58.         }
  59.     },

  60.     // on resize
  61.     resize : function () {
  62.         var iCurW = this.oParent.offsetWidth; // current sizes
  63.         var iCurH = this.oParent.offsetHeight;
  64.         for (var i in this.aPages) { // for each page
  65.             var oPage = this.aPages[i];
  66.             var iNewX = Math.round(oPage.oObj.offsetLeft * iCurW / oPage.oObj.offsetWidth); // new sizes
  67.             var iNewY = Math.round(oPage.oObj.offsetTop * iCurH / oPage.oObj.offsetHeight);
  68.             oPage.oObj.style.left = iNewX + 'px';
  69.             oPage.oObj.style.top = iNewY + 'px';
  70.             oPage.oObj.style.width = iCurW + 'px';
  71.             oPage.oObj.style.height = iCurH + 'px';
  72.             oPage.iXPos = -iNewX;
  73.             oPage.iYPos = -iNewY;

  74.             if (this.sCurPage)
  75.                 this.goto(this.sCurPage, -1);
  76.         }
  77.     },

  78.     goto : function (sHref, iDur) {
  79.         this.tween.iStart = new Date() * 1;
  80.         this.tween.iDur = iDur;
  81.         this.tween.fromX = this.oPages.offsetLeft;
  82.         this.tween.fromY = this.oPages.offsetTop;
  83.         this.tween.iXPos = this.aPages[sHref].iXPos - this.tween.fromX;
  84.         this.tween.iYPos = this.aPages[sHref].iYPos - this.tween.fromY;
  85.         this.sTargPage = sHref;

  86.         if (! this.bRunning)
  87.             this.bRunning = window.setInterval(this.animate, 24);
  88.     },

  89.     animate : function () {
  90.         var iCurTime = (new Date() * 1) - navi.tween.iStart;
  91.         if (iCurTime < navi.tween.iDur) {
  92.             var iCur = Math.cos(Math.PI * (iCurTime / navi.tween.iDur)) - 1;
  93.             navi.oPages.style.left = Math.round(-navi.tween.iXPos * .5 * iCur + navi.tween.fromX) + 'px';
  94.             navi.oPages.style.top = Math.round(-navi.tween.iYPos * .5 * iCur + navi.tween.fromY) + 'px';
  95.         } else {
  96.             navi.oPages.style.left = Math.round(navi.tween.fromX + navi.tween.iXPos) + 'px';
  97.             navi.oPages.style.top = Math.round(navi.tween.fromY + navi.tween.iYPos) + 'px';

  98.             window.clearInterval(navi.bRunning);
  99.             navi.bRunning = false;
  100.             navi.sCurPage = navi.sTargPage;

  101.             var i = 0; var p = null;
  102.             while (p = navi.aLinks[i++]) {
  103.                 if (p.oObj.id == navi.sCurPage) {
  104.                     if (p.a.className.indexOf('visited') >= 0 ) {
  105.                         p.a.className = p.a.className.replace('visited', 'active');
  106.                     } else p.a.className += ' active';
  107.                     p.visited = true;
  108.                 } else if (p.visited) {
  109.                     p.a.className = p.a.className.replace('active', 'visited');
  110.                 }
  111.             }

  112.             if (navi.bHash) {
  113.                 if (navi.bUpdateH) {
  114.                     navi.sOldH = navi.sCurPage;
  115.                     window.location.hash = navi.sCurPage;
  116.                 }
  117.                 navi.bUpdateH = true;
  118.             }
  119.         }
  120.     }
  121. }

  122. window.onload = function() { // page onload
  123.     navi.init({parent_id: 'container', pages_id: 'pages', duration: 1000});
  124. }
  125. window.onkeydown = function(event){ // keyboard alerts
  126.     switch (event.keyCode) {
  127.         case 37: // Left key
  128.             var iPage = navi.sCurPage.substring(4) * 1;
  129.             iPage--;
  130.             if (iPage < 1) {
  131.                 iPage = 1;
  132.             }
  133.             navi.goto('page' + iPage, 1000);
  134.             break;
  135.         case 39: // Right key
  136.             var iPage = navi.sCurPage.substring(4) * 1;
  137.             iPage++;
  138.             if (iPage > 16) {
  139.                 iPage = 16;
  140.             }
  141.             navi.goto('page' + iPage, 1000);
  142.             break;
  143.     }
  144. };
HTML标签
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <title>Fullscreen Javascript Slide navigation | Script Tutorials</title>
  6.         <link href="css/layout.css" type="text/css" rel="stylesheet">
  7.         <script src="js/main.js"></script>
  8.     </head>
  9.     <body>
  10.         <div class="container" id="container">
  11.             <div id="pages">
  12.                 <div id="page1">
  13.                     <a href="#page2" class="go right"></a>
  14.                     <a href="#page5" class="go bottom"></a>
  15.                 </div>
  16.                 <div id="page2">
  17.                     <a href="#page1" class="go left"></a>
  18.                     <a href="#page3" class="go right"></a>
  19.                     <a href="#page6" class="go bottom"></a>
  20.                 </div>
  21.                 <div id="page3">
  22.                     <a href="#page2" class="go left"></a>
  23.                     <a href="#page4" class="go right"></a>
  24.                     <a href="#page7" class="go bottom"></a>
  25.                 </div>
  26.                 <div id="page4">
  27.                     <a href="#page3" class="go left"></a>
  28.                     <a href="#page8" class="go bottom"></a>
  29.                 </div>
  30.                 <div id="page5">
  31.                     <a href="#page1" class="go top"></a>
  32.                     <a href="#page6" class="go right"></a>
  33.                     <a href="#page9" class="go bottom"></a>
  34.                 </div>
  35.                 <div id="page6">
  36.                     <a href="#page5" class="go left"></a>
  37.                     <a href="#page2" class="go top"></a>
  38.                     <a href="#page7" class="go right"></a>
  39.                     <a href="#page10" class="go bottom"></a>
  40.                 </div>
  41.                 <div id="page7">
  42.                     <a href="#page6" class="go left"></a>
  43.                     <a href="#page3" class="go top"></a>
  44.                     <a href="#page8" class="go right"></a>
  45.                     <a href="#page11" class="go bottom"></a>
  46.                 </div>
  47.                 <div id="page8">
  48.                     <a href="#page7" class="go left"></a>
  49.                     <a href="#page4" class="go top"></a>
  50.                     <a href="#page12" class="go bottom"></a>
  51.                 </div>
  52.                 <div id="page9">
  53.                     <a href="#page5" class="go top"></a>
  54.                     <a href="#page10" class="go right"></a>
  55.                     <a href="#page13" class="go bottom"></a>
  56.                 </div>
  57.                 <div id="page10">
  58.                     <a href="#page9" class="go left"></a>
  59.                     <a href="#page6" class="go top"></a>
  60.                     <a href="#page11" class="go right"></a>
  61.                     <a href="#page14" class="go bottom"></a>
  62.                 </div>
  63.                 <div id="page11">
  64.                     <a href="#page10" class="go left"></a>
  65.                     <a href="#page7" class="go top"></a>
  66.                     <a href="#page12" class="go right"></a>
  67.                     <a href="#page15" class="go bottom"></a>
  68.                 </div>
  69.                 <div id="page12">
  70.                     <a href="#page11" class="go left"></a>
  71.                     <a href="#page8" class="go top"></a>
  72.                     <a href="#page16" class="go bottom"></a>
  73.                 </div>
  74.                 <div id="page13">
  75.                     <a href="#page9" class="go top"></a>
  76.                     <a href="#page14" class="go right"></a>
  77.                 </div>
  78.                 <div id="page14">
  79.                     <a href="#page13" class="go left"></a>
  80.                     <a href="#page10" class="go top"></a>
  81.                     <a href="#page15" class="go right"></a>
  82.                 </div>
  83.                 <div id="page15">
  84.                     <a href="#page14" class="go left"></a>
  85.                     <a href="#page11" class="go top"></a>
  86.                     <a href="#page16" class="go right"></a>
  87.                 </div>
  88.                 <div id="page16">
  89.                     <a href="#page15" class="go left"></a>
  90.                     <a href="#page12" class="go top"></a>
  91.                 </div>
  92.             </div>
  93.             <div id="nav">
  94.                 <div>Fullscreen Javascript Slide navigation</div>
  95.                 <div style="width:130px">
  96.                     <a href="#page1" class="go"></a>
  97.                     <a href="#page2" class="go"></a>
  98.                     <a href="#page3" class="go"></a>
  99.                     <a href="#page4" class="go"></a>
  100.                     <a href="#page5" class="go"></a>
  101.                     <a href="#page6" class="go"></a>
  102.                     <a href="#page7" class="go"></a>
  103.                     <a href="#page8" class="go"></a>
  104.                     <a href="#page9" class="go"></a>
  105.                     <a href="#page10" class="go"></a>
  106.                     <a href="#page11" class="go"></a>
  107.                     <a href="#page12" class="go"></a>
  108.                     <a href="#page13" class="go"></a>
  109.                     <a href="#page14" class="go"></a>
  110.                     <a href="#page15" class="go"></a>
  111.                     <a href="#page16" class="go"></a>
  112.                 </div>
  113.              </div>
  114.         </div>
  115.     </body>
  116. </html>
阅读(1052) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~