vue 在移动端实现红包雨 (兼容性好)
2018-02-28 23:12
531 查看
## 红包雨、复制代码就能用
下面是代码:<template> <div class="ser_home">
<ul class="red_packet" id="red_packet">
<template v-for="(item, index) in liParams">
<li :style="{ left: item.left, animationDuration: item.durTime, webkitAnimationDuration: item.durTime}" :class="item.cls" :data-index="index" @webkitAnimationEnd="removeDom">
<a href='javascript:;'>
<i :style="{ transform: item.transforms, webkitTransform: item.transforms}"></i>
</a>
</li>
</template>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
liParams: [],
timer: null,
duration: 10000 // 定义时间
}
},
mounted () {
this.startRedPacket()
},
methods: {
/**
* 开启动画
*/
startRedPacket() {
let win = document.documentElement.clientWidth || document.body.clientWidth
let left = parseInt(Math.random() * (win - 50) + 0);
let rotate = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg"; // 旋转角度
let scales = (Math.random() * (12 - 8 + 1) + 8) * 0.1; // 图片尺寸
let durTime = (Math.random() * (2.5 - 1.2 + 1) + 1.2) + 's'; // 时间 1.2和1.2这个数值保持一样
console.log(durTime)
this.liParams.push({left: left+'px', cls: 'move_1', transforms: 'rotate('+ rotate +') scale('+ scales +')', durTime: durTime})
setTimeout( () => { // 多少时间结束
clearTimeout(this.timer)
return;
}, this.duration)
this.timer = setTimeout( () => {
this.startRedPacket()
},100)
},
/**
* 回收dom节点
*/
removeDom (e) {
let target = e.currentTarget;
document.querySelector('#red_packet').removeChild(target)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.ser_home {
width: 100%;
height: 100%;
}
.red_packet {
display: block;
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
i {
width: 48px;
height: 69px;
display: block;
background: url('/hongbao.png') no-repeat;
}
li {
position: absolute;
animation: all 3s linear;
top:-100px;
z-index: 10;
&.move_1 {
-webkit-animation: aim_move 5s linear 1 forwards;
animation: aim_move 5s linear 1 forwards;
}
}
a {
display: block;
}
}
@keyframes aim_move {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
}
100% {
-webkit-transform: translateY(120vh);
transform: translateY(120vh);
}
}
</style>
下面是代码:<template> <div class="ser_home">
<ul class="red_packet" id="red_packet">
<template v-for="(item, index) in liParams">
<li :style="{ left: item.left, animationDuration: item.durTime, webkitAnimationDuration: item.durTime}" :class="item.cls" :data-index="index" @webkitAnimationEnd="removeDom">
<a href='javascript:;'>
<i :style="{ transform: item.transforms, webkitTransform: item.transforms}"></i>
</a>
</li>
</template>
</ul>
</div>
</template>
<script>
export default {
data () {
return {
liParams: [],
timer: null,
duration: 10000 // 定义时间
}
},
mounted () {
this.startRedPacket()
},
methods: {
/**
* 开启动画
*/
startRedPacket() {
let win = document.documentElement.clientWidth || document.body.clientWidth
let left = parseInt(Math.random() * (win - 50) + 0);
let rotate = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg"; // 旋转角度
let scales = (Math.random() * (12 - 8 + 1) + 8) * 0.1; // 图片尺寸
let durTime = (Math.random() * (2.5 - 1.2 + 1) + 1.2) + 's'; // 时间 1.2和1.2这个数值保持一样
console.log(durTime)
this.liParams.push({left: left+'px', cls: 'move_1', transforms: 'rotate('+ rotate +') scale('+ scales +')', durTime: durTime})
setTimeout( () => { // 多少时间结束
clearTimeout(this.timer)
return;
}, this.duration)
this.timer = setTimeout( () => {
this.startRedPacket()
},100)
},
/**
* 回收dom节点
*/
removeDom (e) {
let target = e.currentTarget;
document.querySelector('#red_packet').removeChild(target)
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.ser_home {
width: 100%;
height: 100%;
}
.red_packet {
display: block;
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
i {
width: 48px;
height: 69px;
display: block;
background: url('/hongbao.png') no-repeat;
}
li {
position: absolute;
animation: all 3s linear;
top:-100px;
z-index: 10;
&.move_1 {
-webkit-animation: aim_move 5s linear 1 forwards;
animation: aim_move 5s linear 1 forwards;
}
}
a {
display: block;
}
}
@keyframes aim_move {
0% {
-webkit-transform: translateY(0);
transform: translateY(0);
}
100% {
-webkit-transform: translateY(120vh);
transform: translateY(120vh);
}
}
</style>
相关文章推荐
- vue移动端实现红包雨效果
- vue + vue-touch 来实现移动端左右导航效果(有点像京东移动站导航)
- vue2.0 移动端实现下拉刷新和上拉加载更多的示例
- 原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus
- Vue.js移动端左滑删除组件的实现代码
- CORS跨域、Cookie传递SessionID实现单点登录后的权限认证的移动端兼容性测试报告
- vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)
- vue移动端实现下拉刷新
- vue移动端UI框架实现QQ侧边菜单组件
- Vue利用canvas实现移动端手写板
- 基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多
- 基于vue-cli配置手淘的lib-flexible + rem,实现移动端自适应
- wc-swiper:基于 Vue 实现的 移动端图片轮播组件
- 基于Vue2.js全家桶的移动端AppDEMO实现
- vue+vux实现移动端文件上传样式
- vue实现一个移动端屏蔽滑动的遮罩层
- vue 移动端今天写了一个根据后台传过来的http状态码,来实现跳转error页面
- Vue.js实现移动端短信验证码功能
- jquery实现移动端抢红包功能
- 移动端刷新组件XtnScroll--Vue实现