微信小程序直播疯狂点赞实现与优化

2,081 阅读1分钟
<view class="box" hover-class="hover-class" hover-stay-time="100" bindtap="handlerAdd">
    <view class="list">
        <text class="item" wx:for="{{list}}" wx:key="id">{{item.text}}</text>
    </view>
</view>
const TEXT = ['🤣', '👍', '😂', '👏', '😆', '💋', '🎁', '🧑', '🎉', '🎇', '🎋', '🎍', '🧧', '⚽', '🍔', '🍿', '🌰']
let num = 0

Page({
    data: {
        list: []
    },
    handlerAdd() {
        num += 1
        let key = `item${num}`
        const index = Math.ceil(Math.random() * TEXT.length) - 1
        let list = JSON.parse(JSON.stringify(this.data.list))
        list.push({text: TEXT[index], id: key})
	// 数组长度阈值,超过此阈值直接改变长度,避免卡顿
        let max = 100
        if (list.length === max) {
            list.reverse()
            list.length = list.length - (max - 70)
            list.reverse()
        }
        this.setData({
            list
        })
    }
})
@keyframes ani {
    0% {
        bottom: 0;
        transform: scale(1);
    }
    25% {
        margin-left: -8px;
        transform: scale(1.5);
    }
    50% {
        margin-left: 10px;
        opacity: 1;
        transform: scale(1.5);
    }
    75% {
        margin-left: -10px;
    }
    100% {
        margin-left: 10px;
        bottom: 600px;
        opacity: 0;
    }
}

.box {
    position: fixed;
    bottom: 140px;
    right: 50px;
    width: 90px;
    height: 90px;
    border-radius: 50%;
    background: rgba(0, 0, 0, .3) url("data:image/svg+xml,%3Csvg class='icon' viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='64' height='64'%3E%3Cpath d='M903.637 545.877l.555.598L512 938.667 119.808 546.475l.555-.598A277.333 277.333 0 0 1 512 155.307a277.333 277.333 0 0 1 391.637 390.57z' fill='%23fff'/%3E%3C/svg%3E") no-repeat center center;
    background-size: 50%;
    border: .5px solid #fff;

    &.hover-class {
        background-color: rgba(0, 0, 0, .1);
    }

    .list {
        position: absolute;
        top: -100%;
        left: 0;
        width: 90px;
        height: 90px;
    }

    .item {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 90px;
        height: 90px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 35px;
        transition: all .3s;
        animation: ani 2s;
        animation-fill-mode: forwards;
    }
}