封装组件
<template>
<ul id="main">
<li v-for="(item, index) in circleData" :key="index" :class="{'active': item.is_latest_sign_user}">
<div>
<span>{{ item.nick_name }}</span>
<span>签到</span>
</div>
</li>
</ul>
</template>
<script>
export default {
data() {
return {
circleData: [],
moveDom: [],
moveArr: [],
timer: null,
timerArr: [],
count: 0
};
},
mounted() {
this.getLatest_sign_users('init')
this.timer = setInterval((i) => {
this.count++
this.getLatest_sign_users()
}, 5000)
},
methods: {
getLatest_sign_users(type = '') {
let data = []
if (this.count === 0) {
data = [
{
id: '1',
nick_name: '萧一',
is_latest_sign_user: true,
gender: 0
},
{
nick_name: '杨二',
is_latest_sign_user: true,
gender: 0
},
{
nick_name: '张三',
is_latest_sign_user: true,
gender: 0
}
]
} else if (this.count === 1) {
data = [
{
nick_name: '萧一',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '杨二',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '张三',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '李四',
is_latest_sign_user: true,
gender: 1
},
{
nick_name: '王五',
is_latest_sign_user: true,
gender: 0
},
{
nick_name: '徐六',
is_latest_sign_user: true,
gender: 1
},
{
nick_name: '刘七',
is_latest_sign_user: true,
gender: 1
}
]
} else if (this.count === 2) {
data = [
{
nick_name: '萧一',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '杨二',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '张三',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '李四',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '王五',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '徐六',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '刘七',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '何八',
is_latest_sign_user: true,
gender: 0
},
{
nick_name: '柳九',
is_latest_sign_user: true,
gender: 0
},
{
nick_name: '甄十',
is_latest_sign_user: true,
gender: 1
},
{
nick_name: '十一',
is_latest_sign_user: true,
gender: 1
},
{
nick_name: '十二',
is_latest_sign_user: true,
gender: 1
}
]
} else {
data = [
{
nick_name: '萧一',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '杨二',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '张三',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '李四',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '王五',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '徐六',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '刘七',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '何八',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '柳九',
is_latest_sign_user: false,
gender: 0
},
{
nick_name: '甄十',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '十一',
is_latest_sign_user: false,
gender: 1
},
{
nick_name: '十二',
is_latest_sign_user: false,
gender: 1
}
]
}
this.circleData = [...data]
if (type === 'init') {
data.forEach(item => item.is_latest_sign_user = true)
}
this.$nextTick(() => {
if (data.length) {
this.initBubble()
}
})
},
initBubble() {
let main = document.getElementById("main");
let divDom = main.getElementsByClassName("active");
if (!divDom.length) return;
this.timerArr.forEach(item => {
clearInterval(item)
})
this.timerArr = []
for (let i = 0; i < divDom.length; i++) {
let colors = [
"#EF250A",
"#830AF6"
];
divDom[i].style.boxShadow = "0 0 20px" + " " + colors[this.circleData[i].gender] + " " + "inset";
divDom[i].style.width = this.moveDom.length > 9 ? "46px" : "64px";
divDom[i].style.height = this.moveDom.length > 9 ? "46px" : "64px";
divDom[i].style.fontSize = this.moveDom.length > 9 ? "12px" : "14px";
divDom[i].style.lineHeight = this.moveDom.length > 9 ? "16px" : "20px";
this.moveDom.push(divDom[i])
}
let maxW = 0;
let maxH = 0;
window.onresize = function () {
maxW = main.clientWidth - divDom[0].clientWidth;
maxH = main.clientHeight - divDom[0].clientHeight;
};
onresize();
for (let i = 0; i < this.moveDom.length; i++) {
let obj = {};
if (this.moveDom[i].getAttribute("class") === 'active') {
obj.x = Math.floor(Math.random() * (maxW + 1));
obj.y = Math.floor(Math.random() * (maxH + 1));
obj.movex = Math.floor(Math.random() * 2);
obj.movey = Math.floor(Math.random() * 2);
obj.speed = 0.2;
obj.timer = null;
obj.index = i;
this.moveArr.push(obj)
} else {
obj = this.moveArr[i]
}
this.move(obj, this.moveDom, maxH, maxW);
}
},
move(balls, divDom, maxH, maxW) {
balls.timer = setInterval(() => {
if (balls.movex === 1) {
balls.x += balls.speed;
if (balls.x + balls.speed >= maxW) {
balls.x = maxW;
balls.movex = 0;
}
} else {
balls.x -= balls.speed;
if (balls.x - balls.speed <= 0) {
balls.x = 0;
balls.movex = 1;
}
}
if (balls.movey === 1) {
balls.y += balls.speed;
if (balls.y + balls.speed >= maxH) {
balls.y = maxH;
balls.movey = 0;
}
} else {
balls.y -= balls.speed;
if (balls.y - balls.speed <= 0) {
balls.y = 0;
balls.movey = 1;
}
}
if (divDom[balls.index]) {
divDom[balls.index].style.left = balls.x + "px";
divDom[balls.index].style.top = balls.y + "px";
}
}, 25);
this.timerArr.push(balls.timer)
}
},
beforeDestroy() {
this.timerArr.forEach(item => {
clearInterval(item)
})
clearInterval(this.timer)
}
};
</script>
<style lang='less' scoped>
#main {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
padding: 0;
li {
position: absolute;
overflow: hidden;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
&.active {
animation: scaleBox 1s 1;
}
@keyframes scaleBox {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
div {
span {
display: block;
width: 100%;
color: #fff;
text-align: center;
}
}
}
}
</style>
实现效果
|