<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>图片轮播</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
list-style: none;
font-family: 'Microsoft Yahei';
font-size: 62.5%;
}
section {
position: relative;
margin: 50px auto;
width: 800px;
height: 850px;
overflow: hidden;
}
section>div {
position: absolute;
left: 0;
width: 5600px;
}
section>div>img {
width: 800px;
float: left;
}
section>ol {
position: absolute;
right: 5%;
bottom: 2.5%;
}
section>ol>li {
display: inline-block;
padding: 2.5px 10px;
border: 1px solid #ffffff;
background-color: rgba(255, 255, 255, .5);
color: #333333;
cursor: pointer;
font-size: 1.2rem;
}
section>ol>li:hover,
section>ol>li.current {
background-color: gold;
}
section>span {
position: absolute;
top: 0;
bottom: 0;
z-index: 5;
display: none;
margin: auto;
border: 1px solid #ffffff;
width: 50px;
height: 50px;
background-color: #333333;
color: #ffffff;
opacity: .25;
font-size: 3.6rem;
font-weight: bold;
font-family: '黑体';
text-align: center;
line-height: 50px;
cursor: pointer;
}
section>span:hover {
opacity: .5;
}
section>.left {
left: 0;
}
section>.right {
right: 0;
}
</style>
</head>
<body>
<section>
<div>
<img src="https://img-blog.csdnimg.cn/20191027170147588.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/20191027170209135.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/20191027170231784.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/2019102717034648.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/20191027170405814.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/20191027170429792.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
<img src="https://img-blog.csdnimg.cn/20191027170449509.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYzNDUzMw==,size_16,color_FFFFFF,t_70" alt="">
</div>
<ol>
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ol>
<span class="left"><</span>
<span class="right">></span>
</section>
</body>
<script src=""></script>
<script>
window.onload = function () {
// 获取大盒子
var section = document.querySelector('section')
// 获取所有页码
var page = document.querySelectorAll('li')
// 获取左箭头
var left = document.querySelector('.left')
// 获取右箭头
var right = document.querySelector('.right')
// 获取所有图片
var picture = document.querySelector('div')
// 用一个全局变量 index 记录当前需要展示的图片的索引
var index = 0
// 计时器
var timeID;
timeID = setInterval(nextPage, 2000)
// 鼠标移入图片轮播视觉范围时显示左右箭头,移除时隐藏
section.onmouseover = function () {
left.style.display = 'block'
right.style.display = 'block'
// 鼠标移入清除计时器
clearInterval(timeID)
}
// 鼠标移出隐藏
section.onmouseout = function () {
left.style.display = ''
right.style.display = ''
// 鼠标移出开启计时器,图片轮播
timeID = setInterval(nextPage, 2000);
}
// 左右箭头按钮注册点击事件,在事件中修改当前索引 index,索引 index 表示的是当前应该展示第几张图片
left.onclick = function () {
if (index == 0) {
index = picture.children.length - 1
picture.style.left = -1 * section.offsetWidth * index + 'px';
}
index--;
animateMove(picture, -1 * section.offsetWidth * index)
pageChange()
}
right.onclick = function () {
// 调用下一页函数
nextPage()
}
// 页码注册点击事件
for (let i = 0; i < page.length; i++) {
page[i].onclick = function () {
// 页码要与图片保持一致
var num = parseInt(this.innerHTML)
// 图片数组的长度-1然后赋值给下标
index = num - 1
var target = -1 * section.offsetWidth * index
animateMove(picture, target);
pageChange();
}
}
// 下一页
function nextPage() {
// 下标位图片的子元素的长度-1
if (index == picture.children.length - 1) {
// 下标为0时为第一张图
index = 0
picture.style.left = '0px';
}
index++;
animateMove(picture, -1 * section.offsetWidth * index);
pageChange();
}
// 页码与图片保持同步(当前显示的是第几张图片,下方页码对应索引高亮)
function pageChange() {
for (let i = 0; i < page.length; i++) {
page[i].className = ''
// 当图片是最后一张的下标是,页码应该显示第一张图
} if (index == picture.children.length - 1) {
page[0].className = 'current';
} else {
page[index].className = 'current';
}
}
}
var timeID
function animateMove(obj,target) {
// 0. 获取要移动的元素
// var div = document.querySelector('div')
clearInterval(timeID)
// 1. 创建一个计时器,让 style.left 的值每隔一段时间增加一点,直到到达目标位置为止
timeID = setInterval(() => {
// 2. 获取盒子当前位置
var current = obj.offsetLeft
// 每次移动的距离
var step = 100
// 3. 添加单次运动距离
// current = current + step
// 声明一个开关变量
var isRigh = true
// 4. 判断盒子是否到达目的地,到达就终止移动,否则就继续移动
// if (current <= target) {
// obj.style.left = current + 'px'
// } else {
// obj.style.left = target + 'px'
// clearInterval(timeID)
// }
// 判断元素的当前位置与目标位置的位置关系
if (current < target) {
// 当前位置小于目标位置,说明元素在目标位置的左边,向右移动
current += step
isRigh = true
} if (current > target) {
// 当前位置大于目标位置,说明元素在目标位置的右边,向左移动
current -= step
isRigh = false
}
// 修改left的值
obj.style.left = current + 'px'
if (isRigh ? current >= target : current <= target) {
// 让元素到达目标位置
obj.style.left = target + 'px'
clearInterval(timeID)
}
}, 10)
}
</script>
</html>
好玩的原生轮播图
最新推荐文章于 2021-06-04 11:12:50 发布