js实现效果:循环轮播图

JavaScript09

js实现效果:循环轮播图,第1张

跟普通的左右切换轮播图类似,但是它看起来是首尾相连的。

首先来讲一下我的思路:

我们要想无缝切换图片,必须要在所有图片的最后添加一张第一张图片,因为要实现从最后一张切换到第一张时有过渡效果;

之后我们要让你处在第一张时,向前向后切换都要进行 隐式切换

我们也可以将所有的轮播图的图片外面套一层div,并设置绝对定位,然后使用 父相子绝 ,来让图片可以相对父元素定位(父元素就是装轮播图的盒子)

之后,只要我们让不该显示的图片定位到盒子范围外的位置(父元素设置overflow:hidden),在需要切换图片时,我们就把当前图片前后的图片定位到对应位置,然后通过setInterval方法循环改变其位置实现轮播效果。之后更改当前图片的索引即可。

如果是朝左翻页,就把当前页朝左偏移100%的宽度,让下一页同样朝左偏移100%宽度。以下是代码部分:

<html><head lang="en"><meta charset="UTF-8"><title></title><style>.banner{ width:300pxheight:250pxposition: relativez-index: 100background: skybluemargin:100px autooverflow:hidden } .banner .first{ left:0} .banner a{ width: 100%height: 100%position: absolutedisplay:blocktop:0left:100%} .banner a img{ width: 100%height: 100%} .banner .pre{ position: absoluteleft:0top:120pxbackground: graywidth:30pxheight:30pxborder-radius: 30pxline-height: 30pxtext-align: centeropacity: 0.4z-index: 1000cursor: pointer} .banner .next{ position: absoluteright:0top:120pxbackground: graywidth:30pxheight:30pxborder-radius: 30pxline-height: 30pxtext-align: centeropacity: 0.4z-index: 1000cursor: pointer} .