体验地址:http://150.158.10.192/web
根据 b站大佬的js代码进行更改使用 vue 进行实现,主要用于学习css的动画效果
资源下载地址:https://goatchen.coding.net/public/html-css/HTML-CSS/git/files
<template>
<div class="body">
<div class="a">
<div class="b" :style="{'background-image':'url('+bigImg.backgroundImg+')'}">
</div>
<div class="c">
<div :class="i === 0 ? 'd dd' : 'd'" v-for="(img,i) in srcs" @mouseover="mouseOver(i)">
<img :src="require('@/assets/shuffling/' + img)" alt="">
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Shuffling',
data () {
return {
srcs : [
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"5.jpg"
],
bigImg: {
backgroundImg: ""
},
index : 0,
time: null,
d: [],
}
},
methods: {
init() {
console.log("初始化")
this.index = 0;
this.d = document.getElementsByClassName("d");
this.ts();
},
repeat() {
let length = this.d.length;
for (let i = 0; i < length; i++) {
this.d[i].className = 'd'
}
},
selected(i) {
this.repeat()
if (this.d[i]) {
this.d[i].className = 'd dd'
console.log("选中图片:" + i)
this.setImg(i)
}
},
mouseOver(i) {
this.setImg(i)
this.selected(i)
if (this.time) {
clearInterval(this.time)
}
this.index = i
this.index++
this.ts()
},
ts() {
this.time = setInterval(() => {
if (this.index >= 5) {
this.index = 0;
}
this.selected(this.index)
this.index++
}, 1500)
},
setImg(i) {
this.bigImg.backgroundImg = require(`@/assets/shuffling/${i+1}.jpg`)
}
},
created() {
this.bigImg.backgroundImg = require("@/assets/shuffling/1.jpg")
this.init()
}
}
</script>
<style scoped>
.body {
background-color: rgba(206, 182, 182, 0.637);
display: flex;
height: 100vh;
flex-wrap:wrap;
justify-content: center;
align-content: center;
}
.a {
position: relative;
width: 650px;
height: 500px;
display: flex;
justify-content: space-evenly;
}
.b {
width: 400px;
height: 500px;
transition: .4s;
background-size: cover;
}
.c {
width: 200px;
height: 500px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.d {
position: relative;
width: 200px;
height: 90px;
right: 0;
transition:.5s;
overflow: hidden;
}
.d img {
position: absolute;
width: 200px;
transform: translate(0,-50px);
transition: .5s;
right: 0;
}
.d.dd {
opacity: 0;
right: 250px;
}
.d:hover img {
opacity: 0;
right: 250px;
}
</style>
|