给网页中添加翻板图片效果

通过javascript 给网页中添加类似microsoft首页翻板图片效果:

 
 

html代码:

 <div class="content">
    <p><span class="flip"> 总标题 </span>
    <div id="slider">
  <div class="slide">
   <a href="http://blog.sina.com.cn/stylesheet"><img class="diapo" src="1.jpg" alt=""></a>
   <div class="text">
      <span class="title">标题1</span>
   </div>
  </div>
        <div class="slide">
            <a href="http://blog.sina.com.cn/stylesheet"><img class="diapo" src="2.jpg" alt="" />  </a>
            <div class="text">
    <span class="title">标题2</span>
   </div>
        </div>
  <div class="slide">
   <a href="http://blog.sina.com.cn/stylesheet"><img class="diapo" src="3.jpg" alt="" /></a>
   <div class="text">
    <span class="title">标题3</span>
   </div>
  </div>
  <div class="slide">
   <a href="http://www.cnblogs.com/stylesheet"><img class="diapo" src="4.jpg" alt=""></a>
   <div class="text">
      <span class="title">标题4</span>
   </div>
  </div>
  <div class="slide">
   <a href="http://www.cnblogs.com/stylesheet"><img class="diapo" src="5.jpg" alt=""></a>
   <div class="text">
    <span class="title">标题5</span>
   </div>
  </div>
 </div>
    <script type="text/javascript">

slider.init();
</script>
   </p>
  </div>

 

css代码:

.flip {
 font-size: medium;
 font-weight:bold;
 font-family: Arial, Helvetica, sans-serif;
 height:50px;
 margin:10px 10px;
 paddint:0 5px;
}

   #slider {
    position:relative;
  width: 545px;
  height: 200px;
  border:#c2daff 6px inset;
  overflow: hidden;
 }
 #slider .slide {
  position: absolute;
  top: 0px;
  height: 200px;
  width: 400px;
  background: #000;
  overflow: hidden;
  border-left: #000 solid 1px;
  cursor: default;
 }
 #slider .title   {
  color: #F80;
  font-weight: bold;
  font-size: 1.2em;
  margin-right: 1.5em;
  text-decoration: none;
 }
 #slider .backgroundText {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 100%;
  background: #000;
  filter: alpha(opacity=40);
  opacity: 0.4;
 }
 #slider .text {
  position: absolute;
  top: 50%;
  color: #FFF;
  font-family: verdana, arial, Helvetica, sans-serif;
  font-size: 1.0em;
  text-align: justify;
  width: 370px;
  margin-left:10px;
  margin-top:10px;
 }
 #slider .diapo {
  position: absolute;
  filter: alpha(opacity=100);
  opacity: 1;
  visibility: hidden;
  width:370px;
 }

 

javascript代码:

// JavaScript Document

var slider = function() {
 
 function getElementsByClass(object, tag, className) {
  var o = object.getElementsByTagName(tag);
  for ( var i = 0, n = o.length, ret = []; i < n; i++) {
   if (o[i].className == className) ret.push(o[i]);
  }
  if (ret.length == 1) ret = ret[0];
  return ret;
 }
 function setOpacity (obj,o) {
  if (obj.filters) obj.filters.alpha.opacity = Math.round(o);
  else obj.style.opacity = o / 100;
 }
 
 function Slider(oCont, speed, iW, iH, oP) {
  this.slides = [];
  this.over   = false;
  this.S      = this.S0 = speed;
  this.iW     = iW;
  this.iH     = iH;
  this.oP     = oP;
  this.oc     = document.getElementById(oCont);
  this.frm    = getElementsByClass(this.oc, 'div', 'slide');
  this.NF     = this.frm.length;
  this.resize();
  for (var i = 0; i < this.NF; i++) {
   this.slides[i] = new Slide(this, i);
  }
  this.oc.parent = this;
  this.view      = this.slides[0];
  this.Z         = this.mx;
  
  this.oc.onmouseout = function () {
   this.parent.mouseout();
   return false;
  }
 }
 Slider.prototype = {
  
  run : function () {
   this.Z += this.over ? (this.mn - this.Z) * .5 : (this.mx - this.Z) * .5;
   this.view.calc();
   var i = this.NF;
   while (i--) this.slides[i].move();
  },
  
  resize : function () {
   this.wh = this.oc.clientWidth;
   this.ht = this.oc.clientHeight;
   this.wr = this.wh * this.iW;
   this.r  = this.ht / this.wr;
   this.mx = this.wh / this.NF;
   this.mn = (this.wh * (1 - this.iW)) / (this.NF - 1);
  },
  
  mouseout : function () {
   this.over      = false;
   setOpacity(this.view.img, this.oP);
  }
 }
 
 Slide = function (parent, N) {
  this.parent = parent;
  this.N      = N;
  this.x0     = this.x1 = N * parent.mx;
  this.v      = 0;
  this.loaded = false;
  this.cpt    = 0;
  this.start  = new Date();
  this.obj    = parent.frm[N];
  this.txt    = getElementsByClass(this.obj, 'div', 'text');
  this.img    = getElementsByClass(this.obj, 'img', 'diapo');
  this.bkg    = document.createElement_x('div');
  this.bkg.className = 'backgroundText';
  this.obj.insertBefore(this.bkg, this.txt);
  if (N == 0) this.obj.style.borderLeft = 'none';
  this.obj.style.left = Math.floor(this.x0) + 'px';
  setOpacity(this.img, parent.oP);
  
  this.obj.parent = this;
  this.obj.onmouseover = function() {
   this.parent.over();
   return false;
  }
 }
 Slide.prototype = {
  
  calc : function() {
   var that = this.parent;
   // left slides
   for (var i = 0; i <= this.N; i++) {
    that.slides[i].x1 = i * that.Z;
   }
   // right slides
   for (var i = this.N + 1; i < that.NF; i++) {
    that.slides[i].x1 = that.wh - (that.NF - i) * that.Z;
   }
  },
  
  move : function() {
   var that = this.parent;
   var s = (this.x1 - this.x0) / that.S;
   
   if (this.N && Math.abs(s) > .5) {
    this.obj.style.left = Math.floor(this.x0 += s) + 'px';
   }
   
   var v = (this.N < that.NF - 1) ? that.slides[this.N + 1].x0 - this.x0 : that.wh - this.x0;
   if (Math.abs(v - this.v) > .5) {
    this.bkg.style.top = this.txt.style.top = Math.floor(2 + that.ht - (v - that.Z) * that.iH * that.r) + 'px';
    this.v = v;
    this.cpt++;
   } else {
    if (!this.pro) {
     
     this.pro = true;
     var tps = new Date() - this.start;
     if(this.cpt > 1) {
      that.S = Math.max(2, (28 / (tps / this.cpt)) * that.S0);
     }
    }
   }
   if (!this.loaded) {
    if (this.img.complete) {
     this.img.style.visibility = 'visible';
     this.loaded = true;
    }
   }
  },
  
  over : function () {
   this.parent.resize();
   this.parent.over = true;
   setOpacity(this.parent.view.img, this.parent.oP);
   this.parent.view = this;
   this.start = new Date();
   this.cpt = 0;
   this.pro = false;
   this.calc();
   setOpacity(this.img, 100);
  }
 }
 
 return {
  init : function() {
   // create instances of sliders here
   // parameters : HTMLcontainer name, speed (2 fast - 20 slow), Horizontal ratio, vertical text ratio, opacity
   this.s1 = new Slider("slider", 12, 1.84/3, 1/3.2, 70);
   setInterval("slider.s1.run();", 16);
  }
 }
}();

完整代码下载

 

参考:中国站长站 素材

posted on 2010-03-09 14:34  Candyroot  阅读(980)  评论(1编辑  收藏  举报