1. 程式人生 > >vue 在移動端實現紅包雨 (相容性好)

vue 在移動端實現紅包雨 (相容性好)

: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>