uniapp 实现双击点赞出现特效

更新一下 老板改了需求要加上特效 

1. 创建点赞按钮

首先,在你的页面中创建一个点赞按钮 全局点赞的话就写在最外面的标签就行了。你可以使用 <button> 组件或者自定义一个视图组件。

<template>  
  <view class="container">  
    <button @click="handleTap" :class="{ doubleTap: isDoubleTap }">点赞</button>  
    <image v-if="showHeart" src="/static/heart.png" class="heart-animation" />  
  </view>  
</template>

这里我们使用了 @click 事件监听器来监听按钮的点击事件,并且使用了 :class 绑定来根据 isDoubleTap 的值改变按钮的样式(可选)。showHeart 用于控制小心心的显示与隐藏。

2. 实现双击检测

在 JavaScript 部分,你需要实现双击检测的逻辑。你可以使用一个计时器来判断两次点击是否发生在短时间内。

<script>  
export default {  
  data() {  
    return {  
      tapCount: 0,  
      tapTimer: null,  
      isDoubleTap: false,  
      showHeart: false  
    };  
  },  
  methods: {  
    handleTap() {  
      this.tapCount++;  
      if (this.tapCount === 1) {  
        // 第一次点击,设置计时器  
        this.tapTimer = setTimeout(() => {  
          this.tapCount = 0; // 重置点击次数  
          this.isDoubleTap = false; // 重置双击状态  
        }, 200); // 设定双击的时间间隔,比如200毫秒  
      } else if (this.tapCount === 2) {  
        // 如果在短时间内发生了第二次点击,则认为是双击  
        clearTimeout(this.tapTimer); // 清除计时器  
        this.isDoubleTap = true; // 设置双击状态为true  
        this.tapCount = 0; // 重置点击次数  
        this.showHeart = true; // 显示小心心  
        // 你可以在这里添加发送点赞请求的代码  
          
        // 小心心显示一段时间后隐藏  
        setTimeout(() => {  
          this.showHeart = false;  
        }, 1000); // 设定小心心显示的持续时间,比如1秒  
      }  
    }  
  }  
};  
</script>

3. 添加样式

在 CSS 部分,你可以添加一些样式来增强视觉效果。

<style>  
.container {  
  display: flex;  
  flex-direction: column;  
  align-items: center;  
  justify-content: center;  
  height: 100vh;  
}  
  
button {  
  padding: 10px 20px;  
  background-color: #f4f4f4;  
  border: none;  
  border-radius: 5px;  
  cursor: pointer;  
}  
  
.doubleTap {  
  /* 这里可以添加双击时的样式变化,比如放大、变色等 */  
  transform: scale(1.1);  
}  
  
.heart-animation {  
  width: 50px; /* 根据你的小心心图片大小调整 */  
  height: 50px; /* 根据你的小心心图片大小调整 */  
  margin-top: 20px;  
  animation: heartBounce 1s ease-in-out forwards; /* 添加动画效果 */  
}  
  
@keyframes heartBounce {  
  0% { transform: scale(1); opacity: 1; }  
  50% { transform: scale(1.2); opacity: 0.8; }  
  100% { transform: scale(1); opacity: 1; }  
}  
</style>

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app实现双击点赞功能可以通过以下步骤进行操作: 1. 首先,在likesCom.vue文件中,给点赞图标添加一个点击事件,用来触发点赞操作。可以使用`@click`指令来绑定点击事件。 2. 在data中添加一个变量`lastClickTime`,用来记录上一次点击的时间戳。 3. 在点击事件的处理函数中,判断当前点击的时间戳与上一次点击的时间戳的差值。如果差值小于一定的阈值(比如500毫秒),则认为是双击操作,执行点赞逻辑;否则,只执行单击操作。 4. 在点赞逻辑中,可以根据需求进行相应的操作,比如增加点赞数、改变点赞图标的样式等。 下面是一个示例代码: ```vue <template> <view class="likes-container"> <u-icon v-for="(item, index) in likesData" :class="['icon-default', `${item.animate}`]" :name="item.name" :color="item.color" :key="item.id" ></u-icon> <u-icon class="icon-style" color="#fc5531" name="heart-fill" @click="handleLike" ></u-icon> </view> </template> <script> import _ from 'lodash'; export default { data() { return { likesData: [], lastClickTime: 0 }; }, methods: { randomNum(min, max) { return Math.floor(Math.random() * (max - min)) + min; }, getLikeParams() { // ...原来的获取参数事件 }, handleLike() { const currentTime = new Date().getTime(); if (currentTime - this.lastClickTime < 500) { // 双击操作 // 执行点赞逻辑,比如增加点赞数、改变点赞图标的样式等 // ... } else { // 单击操作 // ... } this.lastClickTime = currentTime; }, createLikes: _.throttle(function() { // ...原来的点击事件 }, 10) } }; </script> <style lang="scss" scoped> // 原来的样式代码 </style> ``` 通过以上步骤,你可以在uni-app实现双击点赞功能。根据点击时间戳的差值,可以判断用户是进行了单击操作还是双击操作,并根据需求执行相应的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值