纯JS实现
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0
padding: 0
}
width: 520px
height: 333px
margin: 50px auto
background-color: greenyellow
padding: 10px 0px
position: relative
overflow: hidden
}
list-style: none
position: absolute
left: 0px
}
float: left
margin: 0px 10px
}
position: absolute
bottom: 20px
}
float: left
width: 15px
height: 15px
background-color: red
margin: 0 5px
opacity: 0.5
filter: alpha(opacity=50)
}
background-color: black
}
</style>
<script type="text/javascript" src="js/tools.js"></script>
<script type="text/javascript">
window.onload = function () {
var timer
var imgList = document.getElementById("imgList")
var imgArr = document.getElementsByTagName("img")
imgList.style.width = 520 * imgArr.length + "px"
var outer = document.getElementById("outer")
var nav = document.getElementById("nav")
nav.style.left = (outer.offsetWidth - nav.offsetWidth) / 2 + "px"
var aArr = document.getElementsByTagName("a")
var num = 0
setA()
for (var i = 0
aArr[i].index = i
aArr[i].onclick = function () {
clearInterval(timer)
num = this.index
setA()
move(imgList, "left", -520 * num, 20, function () {
autoChange()
})
}
}
autoChange()
function setA() {
if (num == imgArr.length - 1) {
num = 0
imgList.style.left = 0
}
for (var i = 0
aArr[i].style.backgroundColor = ""
}
aArr[num].style.backgroundColor = "black"
}
function autoChange() {
timer = setInterval(function () {
num++
if (num >= imgArr.length) {
num = 0
imgList.style.left = 0
}
move(imgList, "left", -520 * num, 20, function () {
setA()
})
}, 3000)
}
}
</script>
</head>
<body>
<div id="outer">
<ul id="imgList">
<li><img src="img/1.jpg" alt="冰棍" /></li>
<li><img src="img/2.jpg" alt="冰棍" /></li>
<li><img src="img/3.jpg" alt="冰棍" /></li>
<li><img src="img/4.jpg" alt="冰棍" /></li>
<li><img src="img/5.jpg" alt="冰棍" /></li>
<li><img src="img/1.jpg" alt="冰棍" /></li>
</ul>
<div id="nav">
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</div>
</div>
</body>
</html>
tools.js文件
function getStyle(obj, name) {
return window.getComputedStyle && getComputedStyle(obj, null)[name] || obj.currentStyle[name];
}
function move(obj, attr, target, speed, callback) {
clearInterval(obj.timer);
var current = parseInt(getStyle(obj, attr));
if(current > target) {
speed = -speed;
}
obj.timer = setInterval(function() {
var oldValue = parseInt(getStyle(obj, attr));
var newValue = oldValue + speed;
if(speed > 0 && newValue > target || speed < 0 && newValue < target) {
newValue = target
}
obj.style[attr] = newValue + "px";
if(newValue == target) {
clearInterval(obj.timer);
callback && callback();
}
}, 30);
}
function addClass(obj, cn) {
if(!hasClass(obj, cn)) {
obj.className += " " + cn;
}
}
function hasClass(obj, cn) {
var reg = new RegExp("\\b" + cn + "\\b");
return reg.test(obj.className);
}
function removeClass(obj, cn) {
var reg = new RegExp("\\b" + cn + "\\b");
obj.className = obj.className.replace(reg, "");
}
function toggleClass(obj, cn) {
if(hasClass(obj, cn)) {
removeClass(obj, cn);
} else {
addClass(obj, cn);
}
}
Vue3+TS实现
<template>
<div class="slideshowBox">
<div class="box">
<div class="slideshow" ref="slideshow">
<img v-for="(item, index) in slideshowData" :key="index" :src="item.slideshowImg">
</div>
</div>
<div class="slideshowSelect">
<img v-for="(item, index) in slideshowData" :key="index" :src="item.slideshowImg"
@click="slideshowSelectClick(index)" ref="slideshowSelectImg" :class="{ 'border': index == 0 }" />
</div>
</div>
</template>
<script lang="ts">
import { ref, reactive, defineComponent, onMounted } from 'vue';
import move from '@/utils/slideshow';
export default defineComponent({
name: 'App',
setup() {
let slideshowData = reactive([
{
slideshowImg: require('@/assets/image/home/slideshow1_1.jpg'),
},
{
slideshowImg: require('@/assets/image/home/slideshow1_2.jpg'),
},
{
slideshowImg: require('@/assets/image/home/slideshow1_3.jpg'),
},
{
slideshowImg: require('@/assets/image/home/slideshow1_4.jpg'),
},
{
slideshowImg: require('@/assets/image/home/slideshow1_5.jpg'),
},
{
slideshowImg: require('@/assets/image/home/slideshow1_1.jpg'),
}
])
let slideshow = ref()
let slideshowSelectImg = ref()
let ind: number = 0;
let timer: any;
function allDot() {
if (ind == slideshowData.length - 1) {
ind = 0;
slideshow.value.style.left = 0;
}
for (var i = 0; i < slideshowSelectImg.value.length; i++) {
slideshowSelectImg.value[i].classList.remove("border");
}
slideshowSelectImg.value[ind].classList.add("border");
}
function loopSlideshow() {
timer = setInterval(function () {
ind++;
if (ind >= slideshowData.length) {
ind = 0;
slideshow.value.style.left = 0;
}
move(slideshow.value, "left", -550 * ind, 10, function () {
allDot();
});
}, 3000);
}
function slideshowSelectClick(index: number) {
clearInterval(timer);
ind = index;
allDot();
move(slideshow.value, "left", -550 * ind, 100, function () {
loopSlideshow();
});
}
onMounted(() => {
if (slideshow.value) {
slideshow.value.style.width = 550 * slideshowData.length + "px";
loopSlideshow();
}
})
return {
slideshowData,
slideshow,
slideshowSelectImg,
slideshowSelectClick,
}
},
})
</script>
<style lang="less" scoped>
// 轮播
.slideshowBox {
width: 550px;
height: 700px;
.box {
width: 550px;
height: 550px;
overflow: hidden;
.slideshow {
height: 550px;
position: relative;
left: 0;
img {
width: 550px;
height: 550px;
}
}
}
}
// 轮播下的选择
.slideshowSelect {
width: 400px;
height: 80px;
margin-top: 50px;
display: flex;
justify-content: space-between;
img {
width: 80px;
height: 80px;
cursor: pointer;
margin-right: 10px;
border: 2px solid transparent;
box-sizing: border-box;
}
&>img:last-child {
display: none;
}
.border {
border: 2px solid red;
}
}
</style>
slideshow.ts文件
// 轮播工具
function getStyle(obj: any, name: string) {
return getComputedStyle(obj, null)[name as any]
}
function move(obj: any, attr: string, target: number, speed: number, callback: any): any {
clearInterval(obj.timer)
var current = parseInt(getStyle(obj, attr))
if (current > target) {
speed = -speed
}
obj.timer = setInterval(function () {
var oldValue = parseInt(getStyle(obj, attr))
var newValue = oldValue + speed
if ((speed < 0 && newValue < target) || (speed > 0 && newValue > target)) {
newValue = target
}
obj.style[attr] = newValue + "px"
if (newValue == target) {
clearInterval(obj.timer)
callback && callback()
}
}, 30)
}
export default move