切换和轮播是前端遇到过最多的效果和写过最多的效果。网上也有很多类似插件。但是我在这里还是要记录一下自己写的轮播图效果,与大多数轮播不同,这次要分享的是一个垂直的轮播图切换效果。请我的唯一一个粉丝来赏析,哈哈哈。。。。

首先是页面布局代码丢在这里 

<div id="box">
  <ul>
    <li><img src="../img/1_1.jpg"/></li>   //这里的图片文件小伙伴需要换成自己的哈哈
    <li><img src="../img/1_2.jpg"/></li>   //这里的图片文件小伙伴需要换成自己的哈哈
    <li><img src="../img/1_4.jpg"/></li>   //这里的图片文件小伙伴需要换成自己的哈哈
  </ul>
  <ol>
    <li class="active">1</li>
    <li>2</li>
    <li>3</li>
  </ol>
</div>

 

然后是css代码:

<style>
*{margin:0;padding:0;}
body{line-height:24px;font-family: "微软雅黑";}
ul,ol,li{list-style:none;}
#box{width:178px;margin:200px auto 0;position:relative;height:108px;overflow:hidden;}
#box ul{height:108px;position:absolute;}
#box ul li{height:108px;}
#box ol{position:absolute;bottom:10px;right:10px;}
#box ol li{display:inline-block;width:14px;height:14px;border-radius:7px;background:#007AFF;font-size:12px;line-height:14px;text-align: center;color:#fff;cursor:pointer;}
#box ol li.active{background:#FFF;color:#007AFF;}
</style>

最后主角要登场了,那就是我们的大佬 ,JS!!!此效果是纯原生js完成,不掺杂任何五谷杂粮。

首先,在原生的世界里,我们要用上运动框架(也就是自己封装好的一个运动框架库,一库在手,原生写效果再也不愁了。),其实用jquery的话写起轮播来几乎不浪费力气,我也不知道我为何非要去用原生,大概是闲的。

// 运动框架 startMove(obj,{属性名:属性值},回调fn)

 1 function startMove(obj, json, fnEnd) {
 2   clearInterval(obj.timer);
 3   obj.timer = setInterval(function () {
 4   var attr = '';
 5   var iTotal = 0;
 6   var iArrived = 0;
 7   for (attr in json) {
 8     iTotal++;
 9     var iCur = 0;
10     if (attr == 'opacity') {
11       iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100)
12     } else {
13     iCur = parseInt(getStyle(obj, attr))
14   };
15   var iSpeed = (json[attr] - iCur) / 8;
16   iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
17   if (iCur == json[attr]) {
18   iArrived++
19   } else {
20   if (attr == 'opacity') {
21   obj.style.filter = 'alpha(opacity:' + (iCur + iSpeed) + ')';
22   obj.style.opacity = (iCur + iSpeed) / 100
23   } else {
24   obj.style[attr] = iCur + iSpeed + 'px'
25   }
26   }
27 };
28   if (iArrived == iTotal) {
29   clearInterval(obj.timer);
30   if (fnEnd) {
31   fnEnd()
32   }
33  }
34 },
35   30)
36 };
37 
38 //IE与非IE浏览器获取对象的非行内样式
39 function getStyle(obj, attr) {
40 if (obj.currentStyle) {
41 return obj.currentStyle[attr]
42 } else {
43 return getComputedStyle(obj, false)[attr]
44 }
45 }   //运动库引用到此结束的标记
46 
47  
48 
49 哈哈,下边就可以认真的写一下我们的轮播啦 哈哈
50 
51 window.onload=function(){
52   var oUl=document.getElementsByTagName("ul")[0];
53   var oUlLi=oUl.getElementsByTagName("li");
54   var oneHeight= oUlLi[0].offsetHeight;
55 
56   var oOl=document.getElementsByTagName("ol")[0];
57   var oOlLi=oOl.getElementsByTagName("li");
58   var iNow=0;
59 
60   var timer=null;
61 
62   for(var i=0;i<oOlLi.length;i++){
63     oOlLi[i].index=i;//索引
64     oOlLi[i].onmouseover=function(){
65     for(var i=0;i<oOlLi.length;i++){
66       oOlLi[i].className='';
67 
68       }
69       this.className='active';
70       iNow=this.index;
71     startMove(oUl, {top: -this.index*oneHeight});
72     clearInterval(timer);
73     };//
74 
75    oOlLi[i].onmouseout=function(){
76       timer=setInterval(toRun,2000);
77 
78     };//
79   };
80 
81   timer=setInterval(toRun,2000);
82 
83   function toRun(){
84     if(iNow==oOlLi.length-1){
85       iNow=0;
86     }
87     else{
88       iNow++;
89     }
90      for(var i=0;i<oOlLi.length;i++){
91      oOlLi[i].className='';
92      }
93      oOlLi[iNow].className='active';
94      startMove(oUl, {top: -iNow*oneHeight});
95   }
96 
97 }

 

 

结束~~~拜拜~~~下次见!

 

 posted on 2018-05-18 14:12  穿越黑暗  阅读(328)  评论(0)    收藏  举报