使用JS实现图片轮播的实例(前后首尾相接)


Posted in Javascript onSeptember 21, 2017

最近各种跑面试,终于还是被问到这个,一脑子浆糊,当时没想出来首尾相接怎么搞,回来之后研究了一波,终于搞出来了,不多说,直接看代码

代码参考了一位已经写好了图片轮播功能的(再次表示感谢),但是没有首尾相接的功能,本人在此基础上增加了首尾相接功能。

效果如下:

使用JS实现图片轮播的实例(前后首尾相接)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>图片轮播</title>
 <style type="text/css">
 body,div,ul,li,a,img{margin: 0;padding: 0;}
 ul,li{list-style: none;}
 a{text-decoration: none;}
 #wrapper{
  position: relative;
  margin: 30px auto; /* 上下边距30px,水平居中 */
  width: 400px;
  height: 200px;
 }
 #banner{
  position:relative;
  width: 400px;
  height: 200px;
  overflow: hidden;
 }
 .imgList{
  position:relative;
  width:2000px;
  height:200px;
  z-index: 10;
  overflow: hidden;
 }
 .imgList li{float:left;display: inline;}
 #prev,
 #next{
  position: absolute;
  top:80px;
  z-index: 20;
  cursor: pointer;
  opacity: 0.2;
  filter:alpha(opacity=20);
 }
 #prev{left: 10px;}
 #next{right: 10px;}
 #prev:hover,
 #next:hover{opacity: 0.5;filter:alpha(opacity=50);}

</style>
</head>
<body>
 <div id="wrapper"><!-- 最外层部分 -->
 <div id="banner"><!-- 轮播部分 -->
  <ul class="imgList"><!-- 图片部分 -->
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/1.jpg" width="400px" height="200px" alt="1"></a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/2.jpg" width="400px" height="200px" alt="2"></a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/3.jpg" width="400px" height="200px" alt="3"></a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/4.jpg" width="400px" height="200px" alt="4"></a></li>
  <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./img/5.jpg" width="400px" height="200px" alt="5"></a></li>
 </ul>
 <img src="./img/prev.png" width="40px" height="40px" id="prev">
 <img src="./img/next.png" width="40px" height="40px" id="next">
</div>
</div>
<script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var curIndex = 0, //当前index
 imgLen = $(".imgList li").length; //图片总数
$(".imgList").css("width", (imgLen+1)*400+"px");
// 定时器自动变换3秒每次
var autoChange = setInterval(function(){
 if(curIndex < imgLen-1){
  curIndex ++;
 }else{
  curIndex = 0;
 }
 //调用变换处理函数
 changeTo(curIndex);
},3000);

//左箭头滑入滑出事件处理
$("#prev").hover(function(){
 //滑入清除定时器
 clearInterval(autoChange);
}, function(){
 //滑出则重置定时器
 autoChangeAgain();
});

//左箭头点击处理
$("#prev").click(function(){
 //根据curIndex进行上一个图片处理
 // curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
 if (curIndex == 0) {
  var element = document.createElement("li");
  element.innerHTML = $(".imgList li")[imgLen - 1].innerHTML;
  // $(".imgList li")[imgLen - 1].remove();
  $(".imgList").prepend(element);
  $(".imgList").css("left", -1*400+"px");
  changeTo(curIndex);
  curIndex = -1;
 } else if (curIndex == -1) {
  $(".imgList").css("left", -(imgLen-1)*400+"px");
  curIndex = imgLen-2;
  $(".imgList li")[0].remove();
  changeTo(curIndex);
 } else {
  --curIndex;
  changeTo(curIndex);
 }

});

//右箭头滑入滑出事件处理
$("#next").hover(function(){
 //滑入清除定时器
 clearInterval(autoChange);
}, function(){
 //滑出则重置定时器
 autoChangeAgain();
});
//右箭头点击处理
$("#next").click(function(){
 // curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
 console.log(imgLen);
 if (curIndex == imgLen-1) {
  var element = document.createElement("li");
  element.innerHTML = $(".imgList li")[0].innerHTML;
  // $(".imgList li")[0].remove();
  $(".imgList").append(element);
  ++curIndex;
 } else if (curIndex == imgLen) {
  curIndex = 0;
  $(".imgList").css("left", "0px");
  $(".imgList li")[imgLen].remove();
  curIndex++;
 } else {
  ++curIndex;
 }
 changeTo(curIndex);
});

//清除定时器时候的重置定时器--封装
function autoChangeAgain(){
 autoChange = setInterval(function(){
  if(curIndex < imgLen-1){
   curIndex ++;
  }else{
   curIndex = 0;
  }
 //调用变换处理函数
 changeTo(curIndex);
 },3000);
}


function changeTo(num){
 var goLeft = num * 400;
 $(".imgList").animate({left: "-" + goLeft + "px"},500);
}
</script>
</body>
</html>

以上这篇使用JS实现图片轮播的实例(前后首尾相接)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript 面向对象编程 function也是类
Sep 17 Javascript
浅谈关于JavaScript的语言特性分析
Apr 11 Javascript
js中传递特殊字符(+,&amp;)的方法
Jan 16 Javascript
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
Jul 11 Javascript
jquery插件canvaspercent.js实现百分比圆饼效果
Jul 18 jQuery
webpack v4 从dev到prd的方法
Apr 02 Javascript
详解VS Code使用之Vue工程配置format代码格式化
Mar 20 Javascript
富文本编辑器vue2-editor实现全屏功能
May 26 Javascript
jQuery实现王者荣耀手风琴效果
Jan 17 jQuery
vue实现动态表格提交参数动态生成控件的操作
Nov 09 Javascript
vue-router中hash模式与history模式的区别
Jun 23 Vue.js
分享一个vue实现的记事本功能案例
Apr 11 Vue.js
Node调用Java的示例代码
Sep 20 #Javascript
浅谈react前后端同构渲染
Sep 20 #Javascript
微信页面弹出键盘后iframe内容变空白的解决方案
Sep 20 #Javascript
微信小程序 页面跳转事件绑定的实例详解
Sep 20 #Javascript
微信小程序 数据绑定及运算的简单实例
Sep 20 #Javascript
Angularjs使用过滤器完成排序功能
Sep 20 #Javascript
微信小程序 swiper组件构建轮播图的实例
Sep 20 #Javascript
You might like
php学习笔记之 函数声明(二)
2011/06/09 PHP
php+mysqli批量查询多张表数据的方法
2015/01/29 PHP
twig里使用js变量的方法
2016/02/05 PHP
jQuery 注意事项 与原因分析
2009/04/24 Javascript
javascript 获取图片尺寸及放大图片
2013/09/04 Javascript
使用jQuery和PHP实现类似360功能开关效果
2014/02/12 Javascript
javascript实现textarea中tab键的缩排处理方法
2015/06/26 Javascript
jQuery实现的表格展开伸缩效果实例
2016/09/07 Javascript
深入理解AngularJS中的ng-bind-html指令
2017/03/27 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
150行Node.js实现的dns代理工具
2019/08/02 Javascript
layer.prompt使文本框为空的情况下也能点击确定的方法
2019/09/24 Javascript
JavaScript代码异常监控实现过程详解
2020/02/17 Javascript
[54:56]DOTA2上海特级锦标赛主赛事日 - 5 总决赛Liquid VS Secret第三局
2016/03/06 DOTA
[01:14]DOTA2亚洲邀请赛小组赛赛前花絮
2017/03/27 DOTA
Python多进程multiprocessing用法实例分析
2017/08/18 Python
Python 数据处理库 pandas进阶教程
2018/04/21 Python
Python中的groupby分组功能的实例代码
2018/07/11 Python
python读取文本中的坐标方法
2018/10/14 Python
Python爬取破解无线网络wifi密码过程解析
2019/09/17 Python
Python pickle模块实现对象序列化
2019/11/22 Python
Pycharm常用快捷键总结及配置方法
2020/11/14 Python
常用的HTML5列表标签
2017/06/20 HTML / CSS
澳大利亚领先的运动鞋商店:Hype DC
2018/03/31 全球购物
医药代表个人的求职信分享
2013/12/08 职场文书
婚礼司仪主持词
2014/03/14 职场文书
办公室主任岗位承诺书
2014/05/29 职场文书
材料成型及控制工程专业求职信
2014/06/19 职场文书
片区教研活动总结
2014/07/02 职场文书
党的群众路线专项整治方案
2014/11/03 职场文书
党的群众路线教育实践活动学习笔记范文
2014/11/06 职场文书
商场营业员岗位职责
2015/04/14 职场文书
走进科学观后感
2015/06/18 职场文书
关于redisson缓存序列化几枚大坑说明
2021/08/04 Redis
PYTHON InceptionV3模型的复现详解
2022/05/06 Python
MySQL中正则表达式(REGEXP)使用详解
2022/07/07 MySQL