javascript-焦点图实现(二)

知识点:

  javascript的原型链继承

  function A(){

     }

     A.prototype={

     }

    function B(){

    A.call(this);//调用父类构造函数         

    }

    B.prototype=new A;//原型链继承

  B.constructor=B;//重新设置构造函数

 

完成功能:  

  扩展Focus类 参见:http://www.cnblogs.com/wengxuesong/archive/2013/05/16/3081966.html

  添加渐隐渐现效果

  自动播放

  停止播放

  设置切换间隔

  设置效果

代码:

  1 <style>
  2 #focus_view td{
  3     height:200px;
  4 }
  5 
  6 </style>
  7 <table width="500" border="0" cellspacing="0" cellpadding="0" id="focus_view">
  8   <tr>
  9     <td style="background:#f00; display:none;">&nbsp;</td>
 10     <td style="background:#ff0; display:none;">&nbsp;</td>
 11     <td style="background:#f0f; display:none;">&nbsp;</td>
 12     <td style="background:#0f0; display:none;">&nbsp;</td>
 13     <td style="background:#000; display:none;">&nbsp;</td>
 14     <td style="background:#00f; display:none;">&nbsp;</td>
 15   </tr>
 16 </table>
 17 <button id="focus_prev">上一项</button><button id="focus_next">下一项</button>
 18 
 19 <script>
 20 
 21 /*
 22     导入 Focus类 *必需
 23     以下代码为不支持IE
 24     继承自Focus类,新添加方法
 25     play,//播放
 26     stop,//停止
 27     setSpeed,//间隔
 28     setEffect//效果
 29 */
 30 var FocusOpacity=function(options){
 31     this._focusItems=options.focusItems;
 32     this.autoPlay=options.autoPlay||true;
 33     this._speed=options.speed||3000;
 34     this._interval=null;
 35     this.play();
 36     this.init();
 37     Focus.call(this,this._focusItems,options.callBackFn,options.effectFn);
 38 }
 39 FocusOpacity.prototype=Focus.prototype;//Focus构造函数里有运行的数据,直接用类原型对象。(重构时解决)
 40 FocusOpacity.prototype.constructor=FocusOpacity;
 41 FocusOpacity.prototype.setSpeed=function(speed){
 42     this._speed=speed;
 43 }
 44 FocusOpacity.prototype.play=function(){
 45     var that=this;
 46     that._interval=    setInterval(function(){that.next()},that._speed)
 47 }
 48 FocusOpacity.prototype.stop=function(){
 49     this._interval&&clearInterval(this._interval)    
 50 }
 51 FocusOpacity.prototype.setEffect=function(fun){
 52     this._effectFn=fun;
 53 }
 54 
 55 //添加每项切换时的过滤动画
 56 function playEffect(index,arr){    
 57     for(var i=0,len=arr.length;i<len;i++){
 58         arr[i].style.display="none";    
 59     }
 60     arr[index].style.display="block";
 61     var playTime,old=0;
 62     function play(){        
 63         old+=0.01
 64         if(old>1){
 65             arr[index].style.opacity=1;
 66             return clearTimeout(playTime);
 67         };
 68         arr[index].style.opacity=old;        
 69         playTime=setTimeout(function(){
 70             play();
 71         },1);    
 72     }
 73     play();
 74 }
 75 
 77 
 78 //以下为测试代码
 79 
 80 var prev=document.getElementById("focus_prev");
 81 var next=document.getElementById("focus_next");
 82 
 83 var focusView=document.getElementById("focus_view");
 84 var focusItems=focusView.getElementsByTagName("td");
 85 
 86 //声明
 87 var fos=new FocusOpacity({'focusItems':focusItems});
 88 //设置切换效果
 89 fos.setEffect(playEffect);
 90 
 91 //后一项
 92 next.onclick=function(){
 93     fos.next();    
 94 }
 95 //前一项
 96 prev.onclick=function(){
 97     fos.prev();    
 98 }
 99 //停止
100 focusView.onmouseover=function(){
101     fos.stop();    
102 }
103 //播放
104 focusView.onmouseout=function(){
105     fos.play();    
106 }
107 
115 </script>

下面就要对现有的两段代码进行重构。

以使结构更合理,方便以后扩展。

未完待续。。。 

 

转载于:https://www.cnblogs.com/wengxuesong/archive/2013/05/17/3084143.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用HTML、CSS和JavaScript实现焦点轮播的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>焦点轮播</title> <style> /* 轮播容器 */ #slider { width: 600px; height: 300px; margin: 0 auto; position: relative; overflow: hidden; } /* 图片容器 */ #slider .slider-img { width: 6000px; height: 300px; position: absolute; left: 0; top: 0; } /* 图片 */ #slider .slider-img img { float: left; width: 600px; height: 300px; } /* 箭头 */ #slider .slider-arrow { position: absolute; top: 50%; margin-top: -20px; width: 40px; height: 40px; background-color: rgba(0, 0, 0, 0.5); color: #fff; text-align: center; line-height: 40px; font-size: 24px; cursor: pointer; } #slider .slider-arrow.prev { left: 0; } #slider .slider-arrow.next { right: 0; } /* 按钮 */ #slider .slider-btn { position: absolute; bottom: 20px; left: 50%; margin-left: -75px; z-index: 1; } #slider .slider-btn span { display: inline-block; width: 10px; height: 10px; margin: 0 5px; background-color: #fff; border-radius: 50%; cursor: pointer; } #slider .slider-btn span.active { background-color: #f00; } </style> </head> <body> <div id="slider"> <div class="slider-img"> <img src="img/1.jpg" alt=""> <img src="img/2.jpg" alt=""> <img src="img/3.jpg" alt=""> <img src="img/4.jpg" alt=""> <img src="img/5.jpg" alt=""> <img src="img/6.jpg" alt=""> </div> <div class="slider-arrow prev"><</div> <div class="slider-arrow next">></div> <div class="slider-btn"> <span class="active"></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div> </div> <script> // 获取元素 var slider = document.getElementById('slider'); var sliderImg = slider.getElementsByClassName('slider-img')[0]; var sliderImgs = sliderImg.getElementsByTagName('img'); var sliderArrow = slider.getElementsByClassName('slider-arrow'); var sliderBtn = slider.getElementsByClassName('slider-btn')[0]; var sliderBtns = sliderBtn.getElementsByTagName('span'); // 定义变量 var index = 0; // 当前显示的图片索引 var timer = null; // 定时器 // 上一 sliderArrow[0].onclick = function() { index--; if (index < 0) { index = sliderImgs.length - 1; } changeImg(); } // 下一 sliderArrow[1].onclick = function() { index++; if (index >= sliderImgs.length) { index = 0; } changeImg(); } // 切换图片 function changeImg() { // 切换图片 for (var i = 0; i < sliderImgs.length; i++) { sliderImgs[i].style.display = 'none'; } sliderImgs[index].style.display = 'block'; // 切换按钮 for (var i = 0; i < sliderBtns.length; i++) { sliderBtns[i].className = ''; } sliderBtns[index].className = 'active'; } // 切换按钮 for (var i = 0; i < sliderBtns.length; i++) { sliderBtns[i].index = i; sliderBtns[i].onclick = function() { index = this.index; changeImg(); } } // 自动播放 function autoPlay() { timer = setInterval(function() { index++; if (index >= sliderImgs.length) { index = 0; } changeImg(); }, 2000); } autoPlay(); // 鼠标移入停止播放,移出继续播放 slider.onmouseover = function() { clearInterval(timer); } slider.onmouseout = function() { autoPlay(); } </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值