原生js实现图片轮播效果

时间:2023-03-08 19:14:52
原生js实现图片轮播效果

思路:设置父容器(一定宽度,一定高度,相对定位,子容器超出部分进行隐藏),子容器图片并排(浮动,绝对定位,每次点击进行相应的左或右偏移量)

1.html:

 <!DOCTYPE html>
<html>
<head>
<title>Carousel figure</title>
<meta charset="utf-8">
<!-- 浏览器标签页显示图标 -->
<link rel="icon" type="image/x-icon" href="./images/1.jpg">
<link rel="stylesheet" type="text/css" href="1.css">
</head>
<body>
<!-- 构建container父容器 -->
<div class="container">
<!-- 图片部分,style部分设置便于js中改变偏移量,从而实现图片轮播-->
<div class="pic" style="left: 0px;">
<!-- 图片部分,建议均加上alt,利于seo -->
<img src="./images/1.jpg" alt="1">
<img src="./images/2.jpg" alt="2">
<img src="./images/3.jpg" alt="3">
<img src="./images/4.jpg" alt="4">
<img src="./images/5.jpg" alt="5">
<img src="./images/6.jpg" alt="6">
</div> <!-- 箭头部分,实现下一张,上一张效果 -->
<a href="javascript:void(0)" class="arrow arrow_left">&lt;</a>
<a href="javascript:void(0)" class="arrow arrow_right">&gt;</a> <!-- 当前图片id显示 -->
<div class="point">
<span class="on">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</div> <!-- html body部分加载完成,最后调用js,进行操作;否则无法有效操作 -->
<script type="text/javascript" src="1.js"></script>
</body>
</html>

1.css:

 /* body部分清一下外边距与内边距,不建议*(可能影响效率),主流大网站均未采用*进行清边距 */
body{
margin:;
padding:;
}
a{
text-decoration: none;
color: green;
}
a:visited{
color: siver;
}
a:hover{
color: gold;
} .container{
/* container采用相对定位relative,便于子容器进行绝对定位absolute */
position: relative; /* 左右居中对齐 */
margin: 0 auto; /* 每张图片的宽度高度均为320px, */
width: 320px;
height: 320px;
/* 图片超出部分隐藏 */
overflow: hidden;
/* border: 1px solid */
/* 设置阴影:水平阴影偏离0,垂直阴影偏移0,模糊距离10px,颜色 */
box-shadow: 0 0 10px orange;
}
.pic{
position: absolute;
/* 6张图片进行排放,故宽度为1920px */
width: 1920px;
height: 320px;
}
.pic img{
/* 设置左浮动,使6张图片排成一排 */
float: left;
width: 320px;
height: 320px;
}
.arrow{
display: block; border-radius: 50%;
/* 采用字符实体,故设置字体使用font-size */
font-size: 60px;
默认隐藏箭头,
display: none;
}
/* 当悬浮在container区域显示箭头 */
.container:hover .arrow{
display: block;
}
/* 当悬浮在arrow区域显示箭头 */
.container .arrow:hover{
background-color: rgba(0, 0, 0, 0.2);
}
.arrow_left{
position: absolute;
left: 30px;
top: 40%;
text-align: center;
width: 80px;
height: 80px;
}
.arrow_right{
position: absolute;
right: 30px;
top: 40%;
text-align: center;
width: 80px;
height: 80px;
}
.point{
position: absolute;
margin: 280px auto 0 80px;
}
.point span{
display: inline-block;
width: 30px;
height: 30px;
border-radius: 50%;
background-color: orange;
text-align: center;
cursor: pointer;
}
.point span.on{
background-color: red;
}
.point span:active{
background-color: gold;
}

1.js:

 // 获取pic组第一个
var pic = document.getElementsByClassName('pic')[0];
// 获取arrow_left
var arrow_left = document.getElementsByClassName('arrow_left')[0];
// 获取arrow_right
var arrow_right = document.getElementsByClassName('arrow_right')[0];
// 获取span组
var points=document.getElementsByTagName('span');
var index=0; // 点击右箭头,下一张图片
arrow_right.onclick = function() {
next_pic();
}
// 点击左箭头,上一张图片
arrow_left.onclick = function() {
prev_pic();
} // 函数实现下一张浏览效果
function next_pic() {
// 最后一张,下一张变为第一张(left值为0)
if (parseInt(pic.style.left) === -1600) {
pic.style.left = 0 + "px";
} else {
// 下一张
var pos = parseInt(pic.style.left) - 320;
pic.style.left = pos + 'px';
}
index++;
if(index>5){
index=0;
}
showPoint();
} function prev_pic() {
if (parseInt(pic.style.left) === 0) {
pic.style.left = -1600 + "px";
} else {
var pos = parseInt(pic.style.left) + 320;
pic.style.left = pos + 'px';
}
index--;
if(index<0){
index=5;
}
showPoint();
} var timer = null; // 自动图片轮播,设置1s间隔
function autoPlay() {
// timer=setInterval(function(){
// next_pic();
// },1000);
timer = setInterval(next_pic, 1000);
}
autoPlay(); var container = document.getElementsByClassName('container')[0];
// 鼠标移动到container区域,暂停自动播放
container.onmouseenter = function() {
clearInterval(timer);
}
// 鼠标离开container区域,自动播放
container.onmouseleave = function() {
autoPlay();
} // 实现点击相应的小按钮图片跳转到相应图片功能
for (var i = 0; i < points.length; i++) {
(function(i){
points[i].onclick=function (){
//0~0,1~-320...5~-1600
pic.style.left=-320*i +"px";
};
index=i;
//无法点击point,使其变色
showPoint();
}
)(i);
} // 实现相应图片对应相应小按钮功能
function showPoint(){
for(var i=0;i<points.length;i++){
points[i].className="";
}
points[index].className="on";
}