JS轮播图汇总
轮播(一)
1,需求
最近在写官网,里面有不同的轮播图要实现,包括旋转木马,这也是考验前端JS和CSS的基本功,也参考了网上很多其他人的写法,感觉各有千秋,再此,记录一下这段旅程,也方便一下其他有需要的同学,
2,原理
(1)定义个容器,里面放置五张以上的图片,设置position: absolute, 让所有的图片叠加,通过添加不同的样式,控制图片显示隐藏,默认第一张图片设置opactity=1, 也就是激活状态,其他的图片都是opactity=0,隐藏状态,然后通过点击事件,控制不同图片的现实隐藏。
下面的图片,我都用颜色代替
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首页轮播</title>
</head>
<style type="text/css">
* {
margin: 0px;
padding:0px;
}
.wrap {
width: 800px;
height: 400px;
position: relative;
}
.list {
width: 800px;
height: 400px;
list-style: none;
position: relative;
padding-left: 0px;
}
.item {
position: absolute;
width: 100%;
height: 100%;
color: white;
font-size: 55px;
opacity: 0;
transition: all .8s;
}
.item:nth-child(1) {
background-color:orchid
}
.item:nth-child(2) {
background-color: red
}
.item:nth-child(3) {
background-color: goldenrod;
}
.item:nth-child(4) {
background-color: green;