mt-swipe mt-popup使用案例

本文详细介绍了HTML中两个常用的组件——mt-swipe和mt-popup的使用方法和应用场景。通过实例展示了如何集成这两个组件,实现滑动切换效果和弹出窗口功能,为网页交互增添更多可能性。
<script>
import numbox from '../../components/numbox.vue'
export default {
  components: {
    numbox,
  },
  methods: {
    countChange(res) {
      this.selectCount = res.count
    },
    goBuy() {
      this.$store.commit('shopcart/setBuy', {
        id: this.id,
        count: this.selectCount,
      })
      this.$router.push('/order/create')
    },
    addCar() {
      this.showNum = false
      this.goodsInfo.count = this.selectCount
      this.$store.commit('shopcart/addGoods', this.goodsInfo)
      this.$toast('已加入购物车')
    },
  },
}
</script>

<template>
  <div class="goods-info">
    <mt-swipe :auto="4000" class="banner">
      <mt-swipe-item v-for="item in goodsInfo.album" :key="item.id">
        <img :src="item.img" />
      </mt-swipe-item>
    </mt-swipe>
    <div class="main-info">
      <p class="price"><span>{{ goodsInfo.price }}</span></p>
      <p class="name">{{ goodsInfo.name }}</p>
    </div>
    <div style="height:10px;background-color:#F5F6F6;"></div>
    <div class="main-info" style="padding-right:0;">
      <mt-cell title="快速购买" :value="goodsInfo.num?'点击购买':'该商品暂时无货'" @click.native="goodsInfo.num?showNum=true:''"
        is-link></mt-cell>
    </div>
    <div style="height:10px;background-color:#F5F6F6;"></div>
    <div class="param">
      <p class="title">商品信息</p>
      <div class="cell">
        <p>商品卖点:{{ goodsInfo.sell_point }}</p>
        <p>库存情况:{{ goodsInfo.num }}件</p>
        <p>上架时间:{{ goodsInfo.create_time }}</p>
      </div>
    </div>
    <div style="height:10px;background-color:#F5F6F6;"></div>
    <div class="param">
      <p class="title">详情</p>
      <div class="content" v-html="goodsInfo.content"></div>
    </div>

    <mt-popup v-model="showNum" position="bottom">
      <div class="select-num">
        <div class="content">
          <p class="title">请选择数量</p>
          <numbox :initCount="1" :max="goodsInfo.num" :goods_id="goodsInfo.id" @countChange="countChange"></numbox>
        </div>
        <p class="handle"><span @click="goBuy">立即购买</span><span @click="addCar">加入购物车</span></p>
      </div>
    </mt-popup>
  </div>
</template>

<style scoped lang="scss">
.goods-info {
  background-color: #fff;
  .banner {
    height: 7.5rem;
    border-bottom: 1px solid #eee;
    img {
      width: 7.5rem;
      height: 7.5rem;
    }
  }
  .main-info {
    padding: 0.2rem 0.3rem;
    .name {
      text-align: left;
      color: #202020;
      font-size: 0.3rem;
      font-family: PingFang SC;
      font-weight: bold;
      span {
        background-color: #ef4f4f;
        color: #fff;
        padding: 2px 5px;
        border-radius: 4px;
        margin-right: 5px;
      }
    }
    .price {
      color: #fb2c1c;
      font-size: 0.48rem;
      font-weight: bold;
      span {
        font-size: 24px;
      }
    }
    /deep/ .mint-cell-wrapper {
      font-size: 14px;
      padding-left: 0;
    }
    .mint-cell {
      min-height: 30px;
      border-top: 0;
      border-bottom: 0;
    }
  }
  .param {
    padding: 0.2rem 0;
    .title {
      padding: 0 0.3rem 0.2rem 0.4rem;
      margin: 0;
      color: #333;
      &::before {
        position: absolute;
        z-index: 1;
        left: 0.3rem;
        margin-top: 3px;
        content: '';
        display: block;
        width: 2px;
        height: 15px;
        background-color: #1ca6ff;
      }
    }
    .cell {
      padding: 0 0.3rem;
      p {
        margin-bottom: 5px;
      }
    }
    .content {
      /deep/ p {
        margin: 0;
        img {
          width: 100%;
          height: auto;
        }
      }
    }
  }
  .select-num {
    width: 7.5rem;
    .title {
      text-align: left;
      padding: 0.2rem 0.3rem;
      font-size: 14px;
      color: #333;
      margin: 0;
    }
    .content {
      display: flex;
      justify-content: space-between;
      text-align: center;
      padding: 0.4rem 0 0.4rem 0;
      .mui-numbox {
        font-size: 22px;
        width: 170px;
        border: 0;
        /deep/.mui-btn {
          width: 40px;
          height: 100%;
          background-color: #fff;
        }
        /deep/ .mui-input-numbox {
          background-color: #ececec;
          border-right: 0 !important;
          border-left: 0 !important;
        }
      }
    }
    .handle {
      display: flex;
      margin: 0;
      padding: 0.3rem 0;
      span {
        flex: 1;
        text-align: center;
        color: #fff;
        background-color: #fc7c44;
        padding: 9px 0;
        border-radius: 50px;
        margin: 0 0.2rem;
        &:last-child {
          background-color: #f4be41;
        }
      }
    }
  }
}
</style>

<think> 我们正在讨论uni-swipe-action组件的使用教程。根据提供的引用内容,我们可以总结出以下关键点: 引用[1]提到:点击右侧内容时不自动收缩的实现方法,以及根据滑动方向设置show属性(左滑设为right,右滑设为left)。 引用[2]提到:如何改造滑动内容的排版,特别是按钮文字换行的问题,通过CSS设置按钮宽度来实现换行。 用户当前的需求是获取uni-swipe-action的使用教程。因此,我们需要提供一个全面的使用指南,包括基本用法、事件处理、样式定制等,同时结合引用中的信息,补充一些特定场景的实现方法(如手动控制展开状态、按钮文字换行)。 以下是uni-swipe-action的使用教程: ### 基本使用 1. **引入组件**:在`pages.json`中配置easycom规则(默认已配置),或在页面中手动引入。 ```json "usingComponents": { "uni-swipe-action": "/uni_modules/uni-swipe-action/components/uni-swipe-action/uni-swipe-action", "uni-swipe-action-item": "/uni_modules/uni-swipe-action/components/uni-swipe-action-item/uni-swipe-action-item" } ``` 2. **基本结构**:在页面中使用组件,每个`uni-swipe-action-item`代表一个可滑动的项,内部包含主内容和操作按钮。 ```html <uni-swipe-action> <uni-swipe-action-item :options="options" @click="onClick"> <view>这里是主内容</view> </uni-swipe-action-item> </uni-swipe-action> ``` 3. **配置选项**:`options`为按钮配置数组,例如: ```javascript data() { return { options: [ { text: '删除', style: { backgroundColor: '#dd524d' } }, { text: '收藏', style: { backgroundColor: '#007aff' } } ] } } ``` ### 事件处理 - `@click`事件:点击按钮时触发,返回按钮信息。 ```javascript methods: { onClick(e) { console.log('点击按钮索引:', e.index) } } ``` ### 手动控制展开状态 引用[1]中提到,可以通过设置`show`属性来控制当前项的展开状态(left或right)。例如,点击其他区域关闭已展开的项: ```html <uni-swipe-action ref="swipeAction"> <uni-swipe-action-item :show="currentIndex === index ? showValue : 'none'" v-for="(item, index) in list" :key="item.id"> <!-- 内容 --> </uni-swipe-action-item> </uni-swipe-action> ``` 在点击事件中,记录当前展开项的索引和方向: ```javascript data() { return { currentIndex: null, showValue: 'none' // 可以是'left','right','none' } }, methods: { handleSwipeChange(e) { // e.direction 滑动方向,e.index 当前项索引 this.currentIndex = e.index; this.showValue = e.direction === 'left' ? 'right' : 'left'; }, // 点击其他地方关闭 closeSwipe() { this.currentIndex = null; } } ``` ### 按钮样式定制(文字换行) 引用[2]中提到,默认按钮文字不换行,在小屏设备上可能显示不全。可以通过CSS强制换行: ```css /* 修改按钮文字样式 */ .uni-swipe-button { white-space: normal; /* 允许换行 */ word-break: break-all; /* 强制换行 */ height: auto; /* 高度自适应 */ line-height: normal; /* 行高正常 */ padding: 10rpx; /* 调整内边距 */ } ``` ### 注意事项 - 如果列表项是动态生成的,务必为每个`uni-swipe-action-item`设置唯一的`key`。 - 避免在滑动区域内放置可点击元素,以免干扰滑动操作。 §§相关问题§§ 1. 如何实现uni-swipe-action与列表数据的联动? 2. uni-swipe-action在微信小程序中有什么特殊限制? 3. 如何动态修改uni-swipe-action的按钮选项? </think> 以下是`uni-swipe-action`组件的详细使用教程,结合官方文档和社区实践整理: ### 一、基础使用 1. **组件引入**(推荐用easycom自动引入): ```html <uni-swipe-action> <uni-swipe-action-item :options="options" @click="handleClick"> <!-- 主内容区域 --> <view class="content">列表项内容</view> </uni-swipe-action-item> </uni-swipe-action> ``` 2. **按钮配置**: ```javascript data() { return { options: [ { text: '删除', style: { backgroundColor: '#dd524d' } }, { text: '收藏', style: { backgroundColor: '#007aff' } } ] } }, methods: { handleClick(e) { console.log('点击按钮索引:', e.index) } } ``` ### 二、关键特性实现 1. **手动控制展开状态**(引用[1]方案): ```html <uni-swipe-action-item :show="currentShow" @change="onSwipeChange"> ``` ```javascript data() { return { currentShow: 'none' // 可选:left/right/none } }, methods: { onSwipeChange(e) { // 根据滑动方向设置状态 this.currentShow = e.direction === 'left' ? 'right' : 'left' } } ``` 2. **按钮文字换行**(引用[2]方案): ```css .uni-swipe_button { white-space: normal; word-break: break-all; width: 80rpx; /* 限制宽度触发换行 */ padding: 10rpx; } ``` ### 三、高级功能 1. **禁用自动关闭**: ```html <uni-swipe-action :auto-close="false"> ``` 2. **左滑右滑独立配置**: ```javascript options: [ { text: '左滑菜单', direction: 'left' }, { text: '右滑菜单', direction: 'right' } ] ``` 3. **动态修改按钮**: ```javascript this.options[0].text = '已修改' ``` ### 四、注意事项 1. 在`scroll-view`中使用时需要设置`@touchmove.stop` 2. iOS端需要开启惯性滚动:`:throttle="true"` 3. 嵌套在`uni-list`中时,建议设置`border="false"` ### 五、典型场景示例 **聊天列表删除功能**: ```html <uni-swipe-action> <uni-swipe-action-item v-for="(item,index) in list" :key="item.id" :options="[{text:'删除',style:{backgroundColor:'red'}}]" @click="deleteItem(index)"> <view class="chat-item"> <image :src="item.avatar"/> <text>{{ item.name }}</text> </view> </uni-swipe-action-item> </uni-swipe-action> ``` [^1]: 引用自uni-app社区滑动状态控制方案 [^2]: 引用自PDA设备按钮样式优化实践
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值