现在的位置: 首页 > 综合 > 正文

使用js实现滑动页面效果,很漂亮

2013年09月18日 ⁄ 综合 ⁄ 共 6344字 ⁄ 字号 评论关闭
使用了一个现成的js

先贴html

  1. <html>
  2. <head>
  3.     <meta http-equiv="content-type" content="text/html; charset=gb2312">
  4.     <title>滑动页面效果示例</title>
  5.     <link rel="stylesheet" href="stylesheets/glider.css" type="text/css" media="screen" charset="utf-8">
  6.     <script src="javascripts/prototype.js" type="text/javascript" charset="utf-8"></script>
  7.     <script src="javascripts/effects.js" type="text/javascript" charset="utf-8"></script>
  8.     <script src="javascripts/glider.js" type="text/javascript" charset="utf-8"></script>
  9. </head>
  10. <body>
  11.     <h1>滑动页面效果示例(Glider.js)</h1>
  12.     <div id="my-glider">
  13.         <div class="controls">
  14.             <a href="#HTML1">HTML 1</a> | 
  15.             <a href="#HTML2">HTML 2</a> |
  16.             <a href="#HTML3">HTML 3</a> |           
  17.         </div>
  18.         
  19.         <div class="scroller">
  20.             <div class="content">
  21.                 <div class="section" id="HTML1">
  22.                     HTML 1
  23.                     <object style="border:0px" type="text/x-scriptlet" data="1.html" width=100height="500px"></object>
  24.                 </div>
  25.                 <div class="section" id="HTML2">
  26.                     HTML 2
  27.                     <object style="border:0px" type="text/x-scriptlet" data="2.html" width=100height="500px"></object>
  28.                 </div>
  29.                 <div class="section" id="HTML3">
  30.                     HTML 3
  31.                     <p>一个youtube篮球视频</p>    
  32.                     <object width="200" height="150">
  33.                     <param name="movie" value="http://www.youtube.com/v/Ef-f7EeDpYI"></param>
  34.                     <param name="wmode" value="transparent"></param>
  35.                     <embed src="http://www.youtube.com/v/Ef-f7EeDpYI" type="application/x-shockwave-flash" wmode="transparent" width="200" height="150"></embed>
  36.                     </object>                   
  37.                 </div>
  38.             </div>
  39.         </div>          
  40.     </div>
  41.     <a href="#" onClick="my_glider.previous();return false;">Previous</a> | <a href="#" onClick="my_glider.next();return false">Next</a>
  42.     <script type="text/javascript" charset="utf-8">
  43.         var my_glider = new Glider('my-glider', {duration:0.5});
  44.     </script>
  45. </body>
  46. </html>

glider.js

  1. /**
  2.  * @author Bruno Bornsztein <bruno@missingmethod.com>
  3.  * @copyright 2007 Curbly LLC
  4.  * @package Glider
  5.  * @license MIT
  6.  * @url http://www.missingmethod.com/projects/glider/
  7.  * @version 0.0.3
  8.  * @dependencies prototype.js 1.5.1+, effects.js
  9.  */
  10. /*  Thanks to Andrew Dupont for refactoring help and code cleanup - http://andrewdupont.net/  */
  11. Glider = Class.create();
  12. Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
  13.     initialize: function(wrapper, options){
  14.         this.scrolling  = false;
  15.         this.wrapper    = $(wrapper);
  16.         this.scroller   = this.wrapper.down('div.scroller');
  17.         this.sections   = this.wrapper.getElementsBySelector('div.section');
  18.         this.options    = Object.extend({ duration: 1.0, frequency: 3 }, options || {});
  19.         this.sections.each( function(section, index) {
  20.           section._index = index;
  21.         });    
  22.         this.events = {
  23.           click: this.click.bind(this)
  24.         };
  25.         this.addObservers();
  26.             if(this.options.initialSection) this.moveTo(this.options.initialSection, this.scroller, { duration:this.options.duration });  // initialSection should be the id of the section you want to show up on load
  27.             if(this.options.autoGlide) this.start();
  28.       },
  29.     
  30.   addObservers: function() {
  31.     var controls = this.wrapper.getElementsBySelector('div.controls a');
  32.     controls.invoke('observe''click'this.events.click);
  33.   },    
  34.   click: function(event) {
  35.         this.stop();
  36.     var element = Event.findElement(event, 'a');
  37.     if (this.scrolling) this.scrolling.cancel();
  38.     
  39.     this.moveTo(element.href.split("#")[1], this.scroller, { duration:this.options.duration });     
  40.     Event.stop(event);
  41.   },
  42.     moveTo: function(element, container, options){
  43.             this.current = $(element);
  44.             Position.prepare();
  45.         var containerOffset = Position.cumulativeOffset(container),
  46.          elementOffset = Position.cumulativeOffset($(element));
  47.           this.scrolling    = new Effect.SmoothScroll(container, 
  48.                 {duration:options.duration, x:(elementOffset[0]-containerOffset[0]), y:(elementOffset[1]-containerOffset[1])});
  49.           return false;
  50.         },
  51.         
  52.   next: function(){
  53.     if (this.current) {
  54.       var currentIndex = this.current._index;
  55.       var nextIndex = (this.sections.length - 1 == currentIndex) ? 0 : currentIndex + 1;      
  56.     } else var nextIndex = 1;
  57.     this.moveTo(this.sections[nextIndex], this.scroller, { 
  58.       duration: this.options.duration
  59.     });
  60.   },
  61.     
  62.   previous: function(){
  63.     if (this.current) {
  64.       var currentIndex = this.current._index;
  65.       var prevIndex = (currentIndex == 0) ? this.sections.length - 1 : 
  66.        currentIndex - 1;
  67.     } else var prevIndex = this.sections.length - 1;
  68.     
  69.     this.moveTo(this.sections[prevIndex], this.scroller, { 
  70.       duration: this.options.duration
  71.     });
  72.   },
  73.     stop: function()
  74.     {
  75.         clearTimeout(this.timer);
  76.     },
  77.     
  78.     start: function()
  79.     {
  80.         this.periodicallyUpdate();
  81.     },
  82.         
  83.     periodicallyUpdate: function()
  84.     { 
  85.         if (this.timer != null) {
  86.             clearTimeout(this.timer);
  87.             this.next();
  88.         }
  89.         this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
  90.     }
  91. });
  92. Effect.SmoothScroll = Class.create();
  93. Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
  94.   initialize: function(element) {
  95.     this.element = $(element);
  96.     var options = Object.extend({
  97.       x:    0,
  98.       y:    0,
  99.       mode: 'absolute'
  100.     } , arguments[1] || {}  );
  101.     this.start(options);
  102.   },
  103.   setup: function() {
  104.     if (this.options.continuous && !this.element._ext ) {
  105.       this.element.cleanWhitespace();
  106.       this.element._ext=true;
  107.       this.element.appendChild(this.element.firstChild);
  108.     }
  109.    
  110.     this.originalLeft=this.element.scrollLeft;
  111.     this.originalTop=this.element.scrollTop;
  112.    
  113.     if(this.options.mode == 'absolute') {
  114.       this.options.x -= this.originalLeft;
  115.       this.options.y -= this.originalTop;
  116.     } 
  117.   },
  118.   update: function(position) {   
  119.     this.element.scrollLeft = this.options.x * position + this.originalLeft;
  120.     this.element.scrollTop  = this.options.y * position + this.originalTop;
  121.   }
  122. });

还有2个js,内容太多,这里先不贴出来

抱歉!评论已关闭.