首页 > javascript相关 > jquery教程 > 正文

jQuery实现轮播图及其原理详解_jquery

2018-11-17 16:46:19

本文实例为大家分享了jQuery实现轮播图及其原理的具体代码,供大家参考,具体内容如下

<!DOCTYPE html><html><head> <meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>JQuery轮播图</title> <style>  *{   padding:0;   margin:0;  }  .container{   width:600px;   height:400px;   overflow: hidden;   position:relative;   margin:0 auto;  }  .list{   width:3000px;   height:400px;   position:absolute;  }  .list>img{   float:left;   width:600px;   height:400px;  }  .pointer{   position:absolute;   width:100px;   bottom:20px;   left:250px;  }  .pointer>span{   cursor:pointer;   display:inline-block;   width:10px;   height:10px;   background: #7b7d80;   border-radius:50%;   border:1px solid #fff;  }  .pointer .on{   background: #28a4c9;  }  .arrow{   position:absolute;   text-decoration:none;   width:40px;   height:40px;   background: #727d8f;   color:#fff;   font-weight: bold;   line-height:40px;   text-align:center;   top:180px;   display:none;  }  .arrow:hover{   background: #0f0f0f;  }  .left{   left:0;  }  .right{   right:0;  }  .container:hover .arrow{   display:block;  } </style></head><body> <div class="container">  <div class="list" style="left:0px;">   <!--<img src="../static/image/photo1.jpg" alt="5"/>-->   <img src="../static/image/banner.jpg" alt="1"/>   <img src="../static/image/slide1.jpg" alt="2"/>   <img src="../static/image/slide1.jpg" alt="3"/>   <img src="../static/image/slide1.jpg" alt="4"/>   <img src="../static/image/photo1.jpg" alt="5"/>   <!--<img src="../static/image/banner.jpg" alt="1"/>-->  </div>  <div class="pointer">   <span index="1" class="on"></span>   <span index="2"></span>   <span index="3"></span>   <span index="4"></span>   <span index="5"></span>  </div>  <a href="#" rel="external nofollow" rel="external nofollow" class="arrow left">></a>  <a href="#" rel="external nofollow" rel="external nofollow" class="arrow right"><</a> </div> <script src="../static/js/jquery-3.2.1.min.js"></script> <script>  var imgCount = 5;  var index = 1;  var intervalId;  var buttonSpan = $('.pointer')[0].children;//htmlCollection 集合  //自动轮播功能 使用定时器  autoNextPage();  function autoNextPage(){   intervalId = setInterval(function(){    nextPage(true);   },3000);  }  //当鼠标移入 停止轮播  $('.container').mouseover(function(){   console.log('hah');   clearInterval(intervalId);  });  // 当鼠标移出,开始轮播  $('.container').mouseout(function(){   autoNextPage();  });  //点击下一页 上一页的功能  $('.left').click(function(){   nextPage(true);  });  $('.right').click(function(){   nextPage(false);  });  //小圆点的相应功能 事件委托  clickButtons();  function clickButtons(){   var length = buttonSpan.length;   for(var i=0;i<length;i++){    buttonSpan[i].onclick = function(){     $(buttonSpan[index-1]).removeClass('on');     if($(this).attr('index')==1){      index = 5;     }else{      index = $(this).attr('index')-1;     }     nextPage(true);    };   }  }  function nextPage(next){   var targetLeft = 0;   //当前的圆点去掉on样式   $(buttonSpan[index-1]).removeClass('on');   if(next){//往后走    if(index == 5){//到最后一张,直接跳到第一张     targetLeft = 0;     index = 1;    }else{     index++;     targetLeft = -600*(index-1);    }   }else{//往前走    if(index == 1){//在第一张,直接跳到第五张     index = 5;     targetLeft = -600*(imgCount-1);    }else{     index--;     targetLeft = -600*(index-1);    }   }   $('.list').animate({left:targetLeft+'px'});   //更新后的圆点加上样式   $(buttonSpan[index-1]).addClass('on');  } </script></body></html>

效果图:

原理:

页面结构方面:

将轮播图容器设置成相对定位,宽度设置成图片的宽度;容器中分为四部分:轮播的图片;点击的小按钮;前一张;后一张

样式方面:

  • 轮播图容器为相对定位,宽度/高度设置成图片的宽度/高度,设置overflow为hidden;
  • 容器中的每一部分都设置成绝对定位,放到相应的位置;
  • 轮播图片的容器宽度设置成所有图片的宽度和,left开始先设置为0;
  • 每张图片宽度一样,都设成左浮动,左右图片都在一行排列,所以轮播图的实质是装有图片的容器的移动,每次移动的距离为一张图片的宽度,这样每次就只显示一张图片;
  • 前一张/后一张设置成display为none,当鼠标移入总容器时,再设置成display为block

功能方面:

自动轮播为一个定时循环机制setInteval,鼠标移入,循环停止,移除循环继续;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 相关标签:jquery教程
  • 本文发布HTML5中文学习网 ,转载请注明出处,感谢您!
  • 相关文章


  • 曝网友假装外国人写投诉信 ofo秒退押金并回函致歉
  • 苹果市值缩水逾2000亿美元 遭多家投行下调目标价
  • Asp.net Core与类库读取配置文件信息的方法_实用技巧
  • asp.net在Repeater嵌套的Repeater中使用复选框详解_实用技巧
  • 利用IIS调试ASP.NET网站程序的完整步骤_实用技巧
  • Asp.Net Core轻松学习系列之配置文件_实用技巧
  • ASP.NET 页生命周期概述(小结)_实用技巧
  • 详解ASP.NET Core WebApi 返回统一格式参数_实用技巧
  • 2018年网络流行语有哪些?2018年十大网络流行语盘点
  • 华为首席财务官孟晚舟被暂扣 深圳市政府要求加方立即放人!
  • 独孤九贱(4)_PHP视频教程

    江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。 PHP中文网出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...

    独孤九贱(5)_ThinkPHP5视频教程

    ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《php.cn独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...

    独孤九贱(1)_HTML5视频教程

    《php.cn原创html5视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。 ...

    ThinkPHP5实战之[教学管理系统]

    本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。

    PHP入门视频教程之一周学会PHP

    所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...

    作者信息

    kevin

    永远在学习的路上!

    相关教程

  • javascript初级视频教程 javascript初级视频教程
  • jquery 基础视频教程 jquery 基础视频教程
  • javascript三级联动视频教程 javascript三级联动视频教程
  • 独孤九贱(3)_JavaScript视频教程 独孤九贱(3)_JavaScript视频教程
  • 独孤九贱(6)_jQuery视频教程 独孤九贱(6)_jQuery视频教程
  • 热门教程