使用了一个现成的js
先贴html
glider.js
还有2个js,内容太多,这里先不贴出来
先贴html
- <html>
- <head>
- <metahttp-equiv="content-type"content="text/html;charset=gb2312">
- <title>滑动页面效果示例</title>
- <linkrel="stylesheet"href="stylesheets/glider.css"type="text/css"media="screen"charset="utf-8">
- <scriptsrc="javascripts/prototype.js"type="text/javascript"charset="utf-8"></script>
- <scriptsrc="javascripts/effects.js"type="text/javascript"charset="utf-8"></script>
- <scriptsrc="javascripts/glider.js"type="text/javascript"charset="utf-8"></script>
- </head>
- <body>
- <h1>滑动页面效果示例(Glider.js)</h1>
- <divid="my-glider">
- <divclass="controls">
- <ahref="#HTML1">HTML1</a>|
- <ahref="#HTML2">HTML2</a>|
- <ahref="#HTML3">HTML3</a>|
- </div>
- <divclass="scroller">
- <divclass="content">
- <divclass="section"id="HTML1">
- HTML1
- <objectstyle="border:0px"type="text/x-scriptlet"data="1.html"width=100%height="500px"></object>
- </div>
- <divclass="section"id="HTML2">
- HTML2
- <objectstyle="border:0px"type="text/x-scriptlet"data="2.html"width=100%height="500px"></object>
- </div>
- <divclass="section"id="HTML3">
- HTML3
- <p>一个youtube篮球视频</p>
- <objectwidth="200"height="150">
- <paramname="movie"value="http://www.youtube.com/v/Ef-f7EeDpYI"></param>
- <paramname="wmode"value="transparent"></param>
- <embedsrc="http://www.youtube.com/v/Ef-f7EeDpYI"type="application/x-shockwave-flash"wmode="transparent"width="200"height="150"></embed>
- </object>
- </div>
- </div>
- </div>
- </div>
- <ahref="#"onClick="my_glider.previous();returnfalse;">Previous</a>|<ahref="#"onClick="my_glider.next();returnfalse">Next</a>
- <scripttype="text/javascript"charset="utf-8">
- varmy_glider=newGlider('my-glider',{duration:0.5});
- </script>
- </body>
- </html>
glider.js
- /**
- *@authorBrunoBornsztein<bruno@missingmethod.com>
- *@copyright2007CurblyLLC
- *@packageGlider
- *@licenseMIT
- *@urlhttp://www.missingmethod.com/projects/glider/
- *@version0.0.3
- *@dependenciesprototype.js1.5.1+,effects.js
- */
- /*ThankstoAndrewDupontforrefactoringhelpandcodecleanup-http://andrewdupont.net/*/
- Glider=Class.create();
- Object.extend(Object.extend(Glider.prototype,Abstract.prototype),{
- initialize:function(wrapper,options){
- this.scrolling=false;
- this.wrapper=$(wrapper);
- this.scroller=this.wrapper.down('div.scroller');
- this.sections=this.wrapper.getElementsBySelector('div.section');
- this.options=Object.extend({duration:1.0,frequency:3},options||{});
- this.sections.each(function(section,index){
- section._index=index;
- });
- this.events={
- click:this.click.bind(this)
- };
- this.addObservers();
- if(this.options.initialSection)this.moveTo(this.options.initialSection,this.scroller,{duration:this.options.duration});//initialSectionshouldbetheidofthesectionyouwanttoshowuponload
- if(this.options.autoGlide)this.start();
- },
- addObservers:function(){
- varcontrols=this.wrapper.getElementsBySelector('div.controlsa');
- controls.invoke('observe','click',this.events.click);
- },
- click:function(event){
- this.stop();
- varelement=Event.findElement(event,'a');
- if(this.scrolling)this.scrolling.cancel();
- this.moveTo(element.href.split("#")[1],this.scroller,{duration:this.options.duration});
- Event.stop(event);
- },
- moveTo:function(element,container,options){
- this.current=$(element);
- Position.prepare();
- varcontainerOffset=Position.cumulativeOffset(container),
- elementOffset=Position.cumulativeOffset($(element));
- this.scrolling=newEffect.SmoothScroll(container,
- {duration:options.duration,x:(elementOffset[0]-containerOffset[0]),y:(elementOffset[1]-containerOffset[1])});
- returnfalse;
- },
- next:function(){
- if(this.current){
- varcurrentIndex=this.current._index;
- varnextIndex=(this.sections.length-1==currentIndex)?0:currentIndex+1;
- }elsevarnextIndex=1;
- this.moveTo(this.sections[nextIndex],this.scroller,{
- duration:this.options.duration
- });
- },
- previous:function(){
- if(this.current){
- varcurrentIndex=this.current._index;
- varprevIndex=(currentIndex==0)?this.sections.length-1:
- currentIndex-1;
- }elsevarprevIndex=this.sections.length-1;
- this.moveTo(this.sections[prevIndex],this.scroller,{
- duration:this.options.duration
- });
- },
- stop:function()
- {
- clearTimeout(this.timer);
- },
- start:function()
- {
- this.periodicallyUpdate();
- },
- periodicallyUpdate:function()
- {
- if(this.timer!=null){
- clearTimeout(this.timer);
- this.next();
- }
- this.timer=setTimeout(this.periodicallyUpdate.bind(this),this.options.frequency*1000);
- }
- });
- Effect.SmoothScroll=Class.create();
- Object.extend(Object.extend(Effect.SmoothScroll.prototype,Effect.Base.prototype),{
- initialize:function(element){
- this.element=$(element);
- varoptions=Object.extend({
- x:0,
- y:0,
- mode:'absolute'
- },arguments[1]||{});
- this.start(options);
- },
- setup:function(){
- if(this.options.continuous&&!this.element._ext){
- this.element.cleanWhitespace();
- this.element._ext=true;
- this.element.appendChild(this.element.firstChild);
- }
- this.originalLeft=this.element.scrollLeft;
- this.originalTop=this.element.scrollTop;
- if(this.options.mode=='absolute'){
- this.options.x-=this.originalLeft;
- this.options.y-=this.originalTop;
- }
- },
- update:function(position){
- this.element.scrollLeft=this.options.x*position+this.originalLeft;
- this.element.scrollTop=this.options.y*position+this.originalTop;
- }
- });