JS + CSS 图片切换效果

来源:互联网 发布:足球黑色三分钟 知乎 编辑:程序博客网 时间:2024/06/04 17:41
[javascript] view plain copy
  1. <div id="xxx">  
  2.      <script>  
  3.      var box =new PPTBox();  
  4.      box.width = 998; //宽度  
  5.      box.height = 323;//高度  
  6.      box.autoplayer = 3;//自动播放间隔时间  
  7.   
  8.      //box.add({"url":"图片地址","title":"悬浮标题","href":"链接地址"})  
  9.      box.add({"url":"images/home_27.jpg","href":"#","title":"悬浮提示标题1"})  
  10.      box.add({"url":"images/home_27.jpg","href":"#","title":"悬浮提示标题2"})  
  11.      box.add({"url":"images/home_27.jpg","href":"#","title":"悬浮提示标题3"})  
  12.      box.add({"url":"images/home_27.jpg","href":"#","title":"悬浮提示标题4"})  
  13.      box.show();  
  14.     </script>  
  15. </div>  


 

[css] view plain copy
  1. <script type="text/javascript" src="js/pptBox.js"></script>  
  2. <style>  
  3. /* 焦点图 */  
  4. .mainbox{  
  5.     overflow:hidden;  
  6.     position:relative;  
  7. }  
  8. .flashbox{  
  9.    overflow:hidden;  
  10.     position:relative;  
  11. }  
  12. .imagebox{  
  13.     text-align:center;position:relative;z-index:999;  
  14. }  
  15.   
  16. .bitdiv{display:inline-block;width:18px;height:18px;margin:0 10px 10px 0px;cursor:pointer;}  
  17. .defimg{background-image:url(images/02.png)}  
  18. .curimg{background-image:url(images/01.png)}  
  19. </style>  


js/pptBox.jsjs/pptBox.js 文件

[javascript] view plain copy
  1. /** 
  2.  * 网络剑客 2012-10-27 
  3.  */  
  4.  function PPTBox()  
  5.  {  
  6.      this.uid = PPTBoxHelper.getId();  
  7.      PPTBoxHelper.instance[this.uid] = this;  
  8.      this._$ = function(id){return document.getElementById(id);};  
  9.      this.width = 988;//宽度  
  10.      this.height = 323;//高度  
  11.      this.picWidth = 15;//小图宽度  
  12.      this.picHeight = 12;//小图高度  
  13.      this.autoplayer = 4;//自动播放间隔(秒)  
  14.      this.target = "_blank";  
  15.      this._box = [];  
  16.      this._curIndex = 0;  
  17.  }  
  18.  PPTBox.prototype =  
  19.  {  
  20.      _createMainBox : function (){  
  21.          var flashBoxWidth = this.width * this._box.length + 5;  
  22.          var html="<div id='"+this.uid+"_mainbox' class='mainbox'  style='width:"+(this.width)+"px;height:"+(this.height+2)+"px;'>";  
  23.          html += "<div id='"+this.uid+"_flashbox' class='flashbox' style='width:"+flashBoxWidth+"px;height:"+(this.height+2)+"px;'></div>";  
  24.          html += "<div id='"+this.uid+"_imagebox' class='imagebox' style='width:"+this.width+"px;height:"+(this.picHeight+2)+"px;top:-"+(this.picHeight+20)+"px;'></div>";  
  25.          html += "</div>";  
  26.          document.write(html);  
  27.      },  
  28.      _init : function (){  
  29.          var picstyle= "";  
  30.          var eventstr = "PPTBoxHelper.instance['"+this.uid+"']";  
  31.          var imageHTML="";  
  32.          var flashbox = "";  
  33.          for(var i=0;i<this._box.length;i++){  
  34.              var parame = this._box[i];  
  35.              flashbox += this.flashHTML(parame.url,this.width,this.height,i);  
  36.              imageHTML ="<div class='bitdiv "+((i==0)?"curimg":"defimg")+"' title ="+parame.title+" src='bit01.gif' "+picstyle+" onclick = \""+eventstr+".clickPic("+i+")\"  onmouseover=\""+eventstr+".mouseoverPic("+i+")\"></div>" + imageHTML;  
  37.          }  
  38.          this._$(this.uid+"_flashbox").innerHTML = flashbox;  
  39.          this._$(this.uid+"_imagebox").innerHTML = imageHTML;  
  40.   
  41.      },  
  42.      _play : function(){  
  43.          clearInterval(this._autoplay);  
  44.          var idx = this._curIndex+1;  
  45.          if(idx>=this._box.length){idx=0;}  
  46.          this.changeIndex(idx);  
  47.          var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";  
  48.          this._autoplay = setInterval(eventstr,this.autoplayer*1000);  
  49.   
  50.      },  
  51.      flashHTML : function(url,width,height,idx) {  
  52.          var isFlash = url.substring(url.lastIndexOf('.')+1).toLowerCase()=="swf";  
  53.          var html = "";  
  54.          if(isFlash){  
  55.              html = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' "  
  56.              + "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+width+"' height='"+height+"'>"  
  57.              + "<param name=\"movie\" value=\""+url+"\" />"  
  58.              + "<param name='quality' value='high' />"  
  59.              + "<param name='wmode' value='transparent'>"  
  60.              + "<embed src='"+url+"' quality='high' wmode='opaque' pluginspage='http://www.macromedia.com/go/getflashplayer'"  
  61.              +"  type='application/x-shockwave-flash' width="+width+" height='"+height+"'></embed>"  
  62.              +"  </object>";  
  63.          } else {  
  64.              var eventstr = "PPTBoxHelper.instance['"+this.uid+"']";  
  65.              var style = "";  
  66.              if(this._box[idx].href){  
  67.                  style = "cursor:pointer"  
  68.              }  
  69.              html="<img src='"+url+"' style='width:"+width+"px;height:"+height+"px;"+style+"' onclick = \""+eventstr+".clickPic("+idx+")\"/>";  
  70.          }  
  71.          return html;  
  72.      },  
  73.      changeIndex : function(idx){  
  74.          var parame = this._box[idx];  
  75.          moveElement(this.uid+"_flashbox",-(idx*this.width),1);  
  76.          var imgs = this._$(this.uid+"_imagebox").getElementsByTagName("div");  
  77.          imgs[this._box.length-1-this._curIndex].className = "bitdiv defimg";  
  78.          imgs[this._box.length-1-idx].className = "bitdiv curimg";  
  79.          this._curIndex = idx;  
  80.      },  
  81.      mouseoverPic:function(idx){  
  82.          this.changeIndex(idx);  
  83.          if(this.autoplayer>0){  
  84.             clearInterval(this._autoplay);  
  85.             var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";  
  86.             this._autoplay = setInterval(eventstr,this.autoplayer*1000);  
  87.          }  
  88.      },  
  89.      clickPic:function(idx){  
  90.          var parame = this._box[idx];  
  91.          if(parame.href&¶me.href!=""){  
  92.              window.open(parame.href,this.target);  
  93.          }  
  94.      },  
  95.      add:function (imgParam){  
  96.          this._box[this._box.length] = imgParam;  
  97.      },  
  98.      show : function () {  
  99.         this._createMainBox();  
  100.         this._init();  
  101.         if(this.autoplayer>0){  
  102.             var eventstr = "PPTBoxHelper.instance['"+this.uid+"']._play()";  
  103.             this._autoplay = setInterval(eventstr,this.autoplayer*1000);  
  104.         }  
  105.      }  
  106.  }  
  107.  var PPTBoxHelper =  
  108.  {  
  109.      count: 0,  
  110.      instance: {},  
  111.      getId: function() { return '_ppt_box-' + (this.count++); }  
  112.  };  
  113.   
  114.  function moveElement(elementID,final_x,interval) {  
  115.    if (!document.getElementById) return false;  
  116.    if (!document.getElementById(elementID)) return false;  
  117.    var elem = document.getElementById(elementID);  
  118.    if (elem.movement) {  
  119.      clearTimeout(elem.movement);  
  120.    }  
  121.    if (!elem.style.left) {  
  122.      elem.style.left = "0px";  
  123.    }  
  124.    var xpos = parseInt(elem.style.left);  
  125.    if (xpos == final_x ) {  
  126.          return true;  
  127.    }  
  128.    if (xpos < final_x) {  
  129.      var dist = Math.ceil((final_x - xpos)/5);  
  130.      xpos = xpos + dist;  
  131.    }  
  132.    if (xpos > final_x) {  
  133.      var dist = Math.ceil((xpos - final_x)/5);  
  134.      xpos = xpos - dist;  
  135.    }  
  136.    elem.style.left = xpos + "px";  
  137.    var repeat = "moveElement('"+elementID+"',"+final_x+","+interval+")";  
  138.    elem.movement = setTimeout(repeat,interval);  
  139.  }  


  

0 0
原创粉丝点击