微信小程序(组件收藏案例)

样式分析:
未收藏时为灰色态,点击收藏按钮,图标变色,同时弹框实体收藏成功;
收藏成功后,点击已收藏的商品,取消收藏,此时图标恢复为灰色态,同时弹框提示取消收藏成功。

逻辑分析:
每个商品单独绑定个数据,负责管理收藏状态,点击时判断状态,然后切换。

(1)先将图片静态样式写入
在这里插入图片描述
因为收藏按钮是在商品展示栏上一层,所以可以用绝对定位到右下角,此时点击便不会跳页。


    .goodsArea{
  width: 100%;
  height: auto;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.goodBox{
  width: 45%;
  position: relative;
}
.goods{
  width: 100%;
  height: 400rpx;
  margin:20rpx 0 0 0;
  position: relative;
}
/* 实现移动端的1px */
 .goods::after{
  content: "";
  width: 200%;
  height: 200%;
  border: 1px solid #ddd;
  transform: scale(0.5,0.5);
  transform-origin:0 0;
  position: absolute;
  left: 0;
  top: 0rpx;
}
.goods image{
  width: 100%;
  height: 320rpx;
  display: block;
}
.goods view{
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
}
.goodBox image.star{
  position: absolute;
  right: 5rpx;
  bottom:20rpx; 
  width: 40rpx;
  height: 40rpx;
}

接下来绑定数据
在这里插入图片描述
改动商品收藏状态值,指向性修改商品收藏状态,添加取消收藏操作,添加用户界面反馈操作

//改动商品收藏状态值
  selectFn(event){
    // console.log(event.currentTarget.dataset.index); //商品索引
    const goodsList = this.data.goodsList;
    const _this=this;
    const now_index = event.currentTarget.dataset.index; //商品索引
    // 重点:先设一个变量用字符串将修改值拼接起来
    goodsList.forEach(function(item,index,self){
      const collectStatus = "goodsList[" + index +"].collectStatus";
      // 根据索引判断元素
      if (now_index==index){
        if (_this.data.goodsList[index].collectStatus){
          _this.setData({
            [collectStatus]:false
          })
          // 用户反馈操作
          wx.showToast({
            title: '取消收藏',
            duration:1000
          })
        }else{
          _this.setData({
            [collectStatus]: true
          })
          // 用户反馈操作
          wx.showToast({
            title: '收藏成功',
            duration: 1000
          })
        }
      }
    })
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值