vue实现点击图片显示全部内容,再次点击隐藏部分内容

3 篇文章 0 订阅

效果:超过两行,点击箭头显示全部内容,再次点击图片显示部分内容。

 

 1.shopObj.skuName是全部商品名称(换成你自己的字段),判断超过42个字符就显示向下箭头,

   <!-- {{ shopObj.skuName }} -->
            {{ shopObj.skuName.length > 42 ? skuName : shopObj.skuName }}
            <image
              v-if="!isShowAll && isRoll"
              @click="toDown"
              class="shopname-hide"
              src="../../static/products/zkicon.png"
            ></image>
            <image
              v-if="!isShowAll && !isRoll"
              @click="toUp"
              class="shopname-hide"
              src="../../static/products/sqicon.png"
            ></image>

2.data中定义字段

 3.watch中监听名称字段是否发生变化,超过42长度就截取到42显示,不超过就全部显示

watch: {
    'shopObj.skuName': function () {
      console.log(this.shopObj.skuName.length,"商品名称的长度======")
      if (this.shopObj.skuName.length > 42) {
        this.skuName = this.shopObj.skuName.substr(0, 42)
        this.isShowAll = false
      } else {
        this.isShowAll = true
      }
    }
  },

4.在图片点击方法中判断

// 点击按钮展开
    toDown() {
      this.skuName = this.shopObj.skuName
      console.log(this.shopObj.skuName, '点击之后数据=====')
      this.isRoll = false
    },
    // 点击按钮收起
    toUp() {
      this.skuName = this.shopObj.skuName.slice(0, 42)
      console.log(this.shopObj.skuName, '收起之后数据=====')
      this.isRoll = true
    }

图片或按钮的css自己根据实际情况开发,最后就完成啦

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值