js css圖片轉場效果

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>js css圖片轉場效果(一)</title><meta http-equiv="content-type" content="text/html;charset=gb2312"><!--把下面代碼加到<head>與</head>之間--><style type="text/css"><!--span{overflow:hidden;font-size:0;line-height:0;}.shutter{position:relative;overflow:hidden;height:300px;width:500px;}.shutter li{position:absolute;left:0;top:0;}ul,li{list-style:none;margin:0;padding:0}img{display:block;border:none;}.shutter-nav{display:inline-block;margin-right:8px;color:#fff;padding:2px 6px;background:#333;border:1px solid #fff;font-family:Tahoma;font-weight:bold;font-size:12px;cursor:pointer;}.shutter-cur-nav{display:inline-block;margin-right:8px;color:#fff;padding:2px 6px;background:#ff7a00;border:1px solid #fff;font-family:Tahoma;font-weight:bold;font-size:12px;cursor:pointer;}--></style><script type="text/javascript"><!--var Hongru={};function H$(id){return document.getElementById(id)}function H$$(c,p){return p.getElementsByTagName(c)}Hongru.shutter = function(){function init(anchor,options){this.anchor=anchor; this.init(options);}init.prototype = {init:function(options){ //options參數:id(必選):圖片列表父標籤id;auto(可選):自動已耗用時間;index(可選):開始的啟動並執行圖片序號var wp = H$(options.id), //擷取圖片列表父元素ul = H$$('ul',wp)[0], //擷取li = this.li = H$$('li',ul);this.a = options.auto?options.auto:4; //自動運行間隔this.index = options.position?options.position:0; //開始啟動並執行圖片序號(從0開始)this.l = li.length;this.cur = 0; //當前顯示的圖片序號&&z-index變數this.stN = options.shutterNum?options.shutterNum:5;this.dir = options.shutterDir?options.shutterDir:'H';this.W = wp.offsetWidth;this.H = wp.offsetHeight;this.aw = 0;this.mask = [];this.nav = [];ul.style.display = 'none';var container = this.container = document.createElement('div'),con_a = this._a = document.createElement('a');con_a.target = '_blank';container.style.cssText = con_a.style.cssText = 'position:absolute;width:'+this.W+'px;height:'+this.H+'px;left:0;top:0';container.appendChild(con_a);for (var x=0; x<this.stN; x++) {var mask = document.createElement('span');mask.style.cssText = this.dir == 'H'?'position:absolute;width:'+this.W/this.stN+'px;height:'+this.H+'px;left:'+x*this.W/this.stN+'px;top:0' : 'position:absolute;width:'+this.W+'px;height:'+this.H/this.stN+'px;left:0px;top:'+x*this.H/this.stN+'px';this.mask.push(mask);con_a.appendChild(mask);}wp.appendChild(container);this.nav_wp = document.createElement('div'); //先建一個div作為控制器父標籤,你也可以用<ul>或<ol>來做,語義可能會更好,這裡我就不改了this.nav_wp.style.cssText = 'position:absolute;right:0;bottom:0;padding:8px 0;'; //為它設定樣式for(var i=0;i<this.l;i++){/* == 繪製控制器 == */var nav = document.createElement('a'); //這裡我就直接用a標籤來做控制器,考慮語義的話你也可以用linav.className = options.navClass?options.navClass:'shutter-nav'; //控制器class,預設為shutter-navthis.nav.push[nav];nav.innerHTML = i+1;nav.onclick = new Function(this.anchor+'.pos('+i+')'); //綁定onclick事件,直接調用之前寫好的pos()函數this.nav_wp.appendChild(nav);}wp.appendChild(this.nav_wp);this.curC = options.curNavClass?options.curNavClass:'shutter-cur-nav';this.pos(this.index); //變換函數},auto:function(){this.li.a = setInterval(new Function(this.anchor+'.move(1)'),this.a*1000); },move:function(i){//參數i有兩種選擇:1和-1,1代表運行到下一張,-1代表運行到上一張var n = this.cur+i; var m = i==1?n==this.l?0:n:n<0?this.l-1:n; //下一張或上一張的序號(注意三元選擇符的運用)this.pos(m); //變換到上一張或下一張},pos:function(i){clearInterval(this.li.a);clearInterval(this.li[i].a);this.aw = this.dir == 'H'?this.W/this.stN : this.H/this.stN;var src = H$$('img',this.li[i])[0].src;var _n = i+1>=this.l?0:i+1;var src_n = H$$('img',this.li[_n])[0].src;this.container.style.backgroundImage = 'url('+src_n+')';for(var n=0;n<this.stN;n++){this.mask[n].style.cssText = this.dir == 'H'?'position:absolute;width:'+this.W/this.stN+'px;height:'+this.H+'px;left:'+n*this.W/this.stN+'px;top:0' : 'position:absolute;width:'+this.W+'px;height:'+this.H/this.stN+'px;left:0px;top:'+n*this.H/this.stN+'px';this.mask[n].style.background = this.dir == 'H' ? 'url('+src+') no-repeat -'+n*this.W/this.stN+'px 0' : 'url('+src+') no-repeat 0 -'+n*this.H/this.stN+'px';}this.cur = i; //綁定當前顯示圖片的正確序號this.li.a = false;for(var x=0;x<this.l;x++){H$$('a',this.nav_wp)[x].className = x==i?this.curC:'shutter-nav'; //綁定當前控制器樣式}this._a.href = H$$('a',this.li[i])[0].href;//this.auto(); //自動運行this.li[i].a = setInterval(new Function(this.anchor+'.anim('+i+')'), 4*this.stN);},anim: function (i) {var tt = this.dir == 'H' ? parseInt(this.mask[this.stN-1].style.width) : parseInt(this.mask[this.stN-1].style.height);if(tt<=5){clearInterval(this.li[i].a);for(var n=0;n<this.stN;n++){this.dir == 'H' ? this.mask[n].style.width = 0 : this.mask[n].style.height = 0;}if(!this.li.a) {this.auto()}}else {for(var n=0;n<this.stN;n++){this.aw -= 1;this.dir == 'H' ? this.mask[n].style.width = this.aw + 'px' : this.mask[n].style.height = this.aw + 'px';}}}}return {init:init}}();//--></script></head><body><!--把下面代碼加到<body>與</body>之間--><div id="shutter" class="shutter" ><ul><li></li><li></li><li></li><li></li><li></li></ul></div><div id="shutter2" class="shutter" ><ul><li></li><li></li><li></li><li></li><li></li></ul></div><script type="text/javascript"><!--var shutterH = new Hongru.shutter.init('shutterH',{id:'shutter'});var shutterV = new Hongru.shutter.init('shutterV',{id:'shutter2',auto:2,shutterNum:4,shutterDir:'V',position:3});//--></script></body></html>
提示:您可以先修改部分代碼再運行
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.