1, 一直来说轮播图都是困扰刚进业内小白的一大难点, 因为我们不仅需要自己作出一个比较完美的运动框架(虽然网上一抓一大把, 但是哪有比自己做出来实现的有成就感, 不是吗?^_^), 还必须需要非常关键性的把握住轮播的原理, 这样才能把一个轮播图完美的呈现出来.
2, 废话不多说, 请看下面代码
- // 首先我们必须都明确, 一个好的运动框架那必须是能够同时承载两种或多种以上需求的, 所以我们应该避免单运动框架通过行内样式的局限性, 就需要我们知道怎么去获取非行间样式
- function getStyle(obj,attr){
- if(obj.currentStyle){
- return obj.currentStyle[attr];
- }else{
- return getComputedStyle(obj,false)[attr]
- }
- }
- // 以上我们用函数去封装一个获取非行间样式的方法方便我们以后使用
- // 开始封装运动框架
- function move(obj,json,fn){
- clearInterval(obj.timer)
- obj.timer =setInterval(function(){
- var bStop = true;
- for(var attr in json){
- var initialVal = 0;
- if(attr == "opacity"){
- initialVal = parseInt(parseFloat(getStyle(obj,attr))*100);
- }else{
- initialVal = parseInt(getStyle(obj,attr));
- }
- var speed = (json[attr]-initialVal)/8;
- speed = speed>0?Math.ceil(speed):Math.floor(speed);
- if(initialVal != json[attr]){
- bStop = false;
- }
- if(attr == "opacity"){
- obj.style.opacity = (initialVal+speed)/100;
- obj.style.filter = "alpha(opacity:"+(initialVal+speed)+")";
- }else{
- obj.style[attr] = initialVal+speed+"px";
- }
- if(bStop){
- clearInterval(obj.timer);
- fn && fn();
- }
- }
- },30)
- }
- // 上面一个运动框架就做好了, 现在只需要我们调用即可, 当然这种类型的框架还不是最完美的, 有些功能还无法实现, 所以我上面一直说比较完美, 但这已经足够用啦...
第二步: 轮播图
- <!-- 首先我们来写一个简单的 html 和 CSS-->
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
- <title>Document</title>
- <style type="text/css">
- *{padding: 0;margin: 0}
- #banner{
- width: 800px;
- height:400px;
- position: relative;
- margin: 50px auto;
- overflow: hidden;
- }
- #banner>ul{
- position: absolute;
- }
- #banner>ul>li{
- float: left;
- list-style: none;
- }
- #banner>ul>li>img{
- width:800px;
- height: 400px;
- border-radius: 15px;
- }
- #cut>a{
- width: 80px;
- height: 40px;
- background:rgba(228,23,221,0.5);
- ;border-radius: 10px;
- text-decoration: none;
- text-align: center;
- font-weight: bold;
- font-size: 30px;
- color: pink;
- position: absolute;
- top:180px;
- display: block;
- line-height: 40px;
- }
- #cut>a:nth-child(2){
- right:0;
- }
- #btn{
- position: absolute;
- top: 350px;
- left: 350px;
- }
- #btn>a{
- width: 20px;
- height: 20px;
- border-radius: 50%;
- background:yellowgreen;
- margin-right:6px;
- float: left;
- }
- #btn>.active{background:yellow;}
- </style>
- </head>
- <body>
- <div id="banner">
- <ul>
- <li><img src="images/1.jpg"></li>
- <li><img src="images/2.jpg"></li>
- <li><img src="images/3.jpg"></li>
- <li><img src="images/4.jpg"></li>
- <li><img src="images/5.jpg"></li>
- </ul>
- <div id="cut">
- <a href="##"><</a>
- <a href="##">></a>
- </div>
- <div id="btn">
- <a href="##" class="active"></a>
- <a href="##"></a>
- <a href="##"></a>
- <a href="##"></a>
- <a href="##"></a>
- </div>
- </div>
- </body>
- </html>
- <!-- 上面我的一个简单的布局, 我就不多说了, 下面请看我们的关键轮播图吧 -->
无缝轮播图 ---> 当我们拿到需求时, 我们会要立马想到无缝轮播是一种图片从左至右的滑动式切换, 那肯定是通过切换 ul(按我上述的 HTML 来说)的 left 值从而达到效果的, 请看下面代码
- var oBan =document.getElementById("banner");
- var oli =oBan.getElementsByTagName("li");
- var oul= oBan.getElementsByTagName("ul")[0];
- var index = 0;
- var timer = null;
- // 想要实现无缝轮播, 关键就是使用一种偷梁换柱的障眼法去掩盖用户的视觉差
- var li =oli[0].cloneNode(true);
- oul.appendChild(li);
- // 为了方便以后的代码维护
- var iw =oli[0].offsetWidth;
- oul.style.width =iw*oli.length+"px";
- // 轮播原理
- function cutImg(){
- move(oul,{left:-index*iw});
- // 当图片切换时, 需要下面的小店同步进行切换
- for(var i = 0;i<oBtn.length;i++){
- oBtn[i].className = "" ;
- }
- oBtn[index>oBtn.length-1?0:index].className = "active";
- }
- // 轮播的自动播放
- function autoP(){
- // 这一步的目的在于当图片处于最后一张图时, 我们怎么通过一个障眼法使图片变成我们想要的下一张图
- timer=setInterval(function(){
- if(index == oli.length-1){
- index = 1;
- oul.style.left = 0;
- }else{
- index++
- }
- cutImg()
- },3000)
- }
- autoP()
- // 当我们鼠标移上时, 轮播停止, 鼠标移开轮播继续
- oBan.onmouseover = function(){
- clearInterval(timer);
- }
- oBan.onmouseout = function(){
- autoP();
- }
- // 轮播图中的点击小点图片切换
- var oBtn = document.getElementById("btn").getElementsByTagName("a");
- for(var i = 0;i<oBtn.length;i++){
- oBtn[i].onclick = function(){
- oBtn[i].iNow = i;
- for(var j = 0;j<oBtn.length;j++){
- oBtn[j].className = "";
- }
- this.className ="active" ;
- move(oul,{left:-this.iNow*iw})
- }
- }
- // 轮播图中左右切换按钮切换
- var oCut =document.getElementById("cut").getElementsByTagName("a");
- // 点击右边按钮切换到第一张图时, 我们下一步想要得到的倒数第二张图
- oCut[1].onclick = function(){
- if(index == oli.length-1){
- index = 1;
- oul.style.left = 0;
- }else{
- index++
- }
- cutImg()
- }
- oCut[0].onclick = function(){
- if(index == 0){
- index = oli.length -2;
- oul.style.left = -(oli.length-1)*iw;
- }else{
- index--;
- }
- cutImg()
- }
- // 这样一个无缝轮播图就做好啦!!
上面是我对轮播图的一个基本构思和大致框架, 希望融合大家各位技术大牛们 的思想加以优化, 谢谢大家....
来源: https://www.cnblogs.com/kevinTangwen/p/Kevin.html