这期内容当中小编将会给大家带来有关使用javascript怎么编写一个移动端的轮播图效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
html部分:
<!-- 结构 --> <!-- li*6>a[href=#]>img[src=./images/$.jpg] --> <div class="box"> <ul> <!-- 为了无缝连接,我们在图片这里前后增加了2个图片 --> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/6.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/1.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/2.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/3.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/4.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/5.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/6.jpg" alt=""></a> </li> <li> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/1.jpg" alt=""></a> </li> </ul> </div>
css部分:
* { margin: 0; padding: 0; } ul { list-style: none; } .box { width: 100%; overflow: hidden; } ul { /* 把li变成8张之后,需要,把ul的宽度变宽 */ width: 800%; /* transition: all 1s; */ } li { float: left; width: 12.5%; } img { width: 100%; }
js部分:
//获取DOM var box = $(".box"); var img = $("ul img"); var ul = $("ul"); var imgWidth = img.width(); var index = 1; var right = index * imgWidth; ul.css("transform", `translateX(-${right}px)`); setTimeout(function() { ul.css("transition", "all 500ms"); }, 100); //往左滑,坐标在增大 box.on("swipeLeft", function() { index++; // if (index == img.length) { // index = 0; // } var left = index * imgWidth; ul.css("transform", `translateX(-${left}px)`); }); //往左滑,坐标在增大 box.on("swipeRight", function() { index--; // if (index == -1) { // index = img.length - 1; // } var right = index * imgWidth; ul.css("transform", `translateX(-${right}px)`); }); //过渡结束之后时进行 ul.on("transitionend", function() { //1.判读 index //往左面滑(滑倒倒数第一张的时候,其实显示的已经是用户想看的第一张) if (index == img.length - 1) { index = 1; //index修改完毕之后需要重新执行一遍 var right = index * imgWidth; ul.css("transform", `translateX(-${right}px)`); //2.取消过渡效果 ul.css("transition", "none"); //3.已经判断完毕了,重新打开过渡效果 //这里设置一个1毫秒的延迟,否则会一起进行 setTimeout(function() { ul.css("transition", "all 500ms"); }, 1); } //1.判读 index //往右面滑(滑倒index为0的时候,显示的是客户想看的第6张图) if (index == 0) { //2.取消过渡效果 ul.css("transition", "none"); index = img.length - 2; //index修改完毕之后需要重新执行一遍 var left = index * imgWidth; ul.css("transform", `translateX(-${left}px)`); //延迟开启过渡效果 setTimeout(function() { ul.css("transition", "all 500ms"); }, 1) } })
上述就是小编为大家分享的使用javascript怎么编写一个移动端的轮播图效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。