前言
原生js制作的轮播图,不需要引入插件。
实现功能:
1)定时器效果,隔一秒转下一张图
2)鼠标移上去停止轮播,离开则继续
3)轮播图02,03采用了css3的3d效果
4)可点击左右箭头进行切图
5)小圆点切图功能懒得做了…
轮播图展示
轮播图01
轮播图02
轮播图03
源码(可直接使用)
轮播图01
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>轮播图01</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrap {
width: 500px;
height: 280px;
margin: 100px auto;
perspective: 500px;
position: relative;
}
#wrap-ul {
width: 500px;
height: 280px;
list-style: none;
position: relative;
}
.wrap img {
/* border-radius: 15%;
这里可以修改图片的大小,最好改的时候把ul和li的一并改为相同的*/
transition: all 1s ease 0s;
width: 500px;
height: 280px;
display: inline-block;
opacity: 0;
}
.cc img {
opacity: 1;
}
#wrap-ul li {
width: 500px;
height: 280px;
position: absolute;
left: 0;
top: 0;
}
.wrap span {
width: 40px;
height: 70px;
background-color: rgba(0, 0, 255, .5);
color: white;
text-align: center;
font-size: 50px;
position: absolute;
left: -60px;
top: 40%;
z-index: 11;
cursor: pointer;
opacity: .4;
transition: all 0.5s ease 0s;
}
.wrap span:hover {
opacity: 1;
}
.wrap #right-btn1 {
right: -60px;
left: auto;
}
</style>
</head>
<body>
<div class="wrap">
<ul id="wrap-ul">
<li class="b1"><img src="./images/1.jpg" alt=""></li>
<li class="b1"><img src="./images/2.jpg" alt=""></li>
<li class="cc"><img src="./images/3.jpg" alt=""></li>
<li class="b1"><img src="./images/4.jpg" alt=""></li>
<li class="b1"><img src="./images/5.jpg" alt=""></li>
</ul>
<span id="toleft1"><</span>
<span id="right-btn1">></span>
</div>
</body>
<script>
window.onload = () => {
var lb = document.querySelector("#toleft1")
var rb = document.querySelector("#right-btn1")
var ul = document.querySelector("#wrap-ul")
var lis = ul.getElementsByTagName("li")
var classes = ["b1", "b1", "cc", "b1", "b1"]
// 加多少个图片 这里加多少个b1 同时body里加多少个<li class="b1"><img src="地址" alt=""></li>
// var classes = ["b1", "b1", "cc", "b1", "b1", "b1", "b1", "b1", "b1"]
function toright() {
var last_class = classes.pop()
classes.unshift(last_class)
for (var i = 0; i < lis.length; i++) {
lis[i].className = classes[i]//因为cl发生了变化 所以lis[i]的classname也要跟着变
}
}
function toleft1() {
var first_class = classes.shift()
classes.push(first_class)
for (var i = 0; i < lis.length; i++) {
lis[i].className = classes[i] //因为classes发生了变化 所以lis[i]的className也要跟着变
}
}
rb.onclick = toright;
lb.onclick = toleft1;
// 定时器
timer = setInterval(toright, 2000)
// 鼠标进来暂停 清除定时器
ul.onmouseenter = function () {
clearInterval(timer)
timer = null
}
// 鼠标出去 重开定时器
ul.onmouseleave = () => {
timer = setInterval(toright, 2000)
}
}
</script>
</html>
轮播图02,03
集合源码,顺便附赠了一个老式的轮播图 调用jquery使用的。
下载地址:传送门