vue外卖二十二:商家详情-商家列表

45 篇文章 2 订阅
该博客介绍了如何在Vue.js应用中利用better-scroll库实现商家信息页面的滚动效果,特别是在数据异步加载完成后再创建滚动实例。文章详细展示了如何监听数据变化,确保当商家图片数据加载完成后,才初始化横向滚动效果,并且动态计算并设置图片列表的宽度,以实现无缝滚动。同时,代码示例中还包括了对商家信息、配送信息和活动服务等不同板块的布局和样式处理。
摘要由CSDN通过智能技术生成

一、有异步获取数据时 滑动创建时机page/shop/info/info.vue

1)有异步数据时要监视数据变动之后再刷新



<script>
  import {mapState} from 'vuex'
  import BScrool from '@better-Scroll/core' //滑动库
	export default{
    mounted(){
      // 【2】如果数据还没有pics, 直接结束
      if(!this.info.pics) {
        return
      }
      // 创建滚动实例
      this._initScroll()
    },
    computed:{
      ...mapState(['info'])
    },
    watch:{
      // 【3】监视info数据如果更新了则再创建一次滚动实例
      info(){
        this.$nextTick(()=>{
          this._initScroll()
        })
      }
    },
    methods:{
      //【1】创建2个Bscroll滚动实例
      _initScroll(){
        //整体页面的纵向滚动
        new BScrool('.shop-info')

        // 动态计算ul的宽度
        const ul = this.$refs.picsUl
        const liWidth = 120
        const space = 6
        const count = this.info.pics.length

        /*把计算出的宽度加到ul样式里:这么做原因是
        better-scroll宽度必须大于父元素.pic-wrapper才能实现横向滚动*/
        ul.style.width = (liWidth + space) * count -space + 'px'

        // 商家图片展示滚动实例
        new BScrool('.pic-wrapper',{
          scrollX:true
        })
      }
    }
  }
</script>

附件:完整代码

<template>
  <div class="shop-info">
    <div class="info-content">
      <section class="section">
        <h3 class="section-title">配送信息</h3>
        <div class="delivery">
          <div>
            <span class="delivery-icon">{{info.description}}</span>
            <span>由商家配送提供配送,约 {{info.deliveryTime}} 分钟送达,距离 {{info.distance}}</span>
          </div>
          <div class="delivery-money">配送费¥{{info.deliveryPrice}}</div>
        </div>
      </section>

      <div class="split"></div>

      <section class="section">
        <h3 class="section-title">活动与服务</h3>
        <div class="activity">
          <!-- 【活动标签样式2】:根据数据的type显示对应的样式 -->
          <div class="activity-item"
           v-for="(support,index) in info.supports"
           :class="activeStyle[support.type]">
            <span class="content-tag">
              <span class="mini-tag">{{support.name}}</span>
            </span>
            <span class="activity-content">{{support.content}}</span>
          </div>
          
        </div>
      </section>

      <div class="split"></div>

      <section class="section">
        <h3 class="section-title">商家实景</h3>
        <div class="pic-wrapper">
          <ul class="pic-list" ref="picsUl">
            <li class="pic-item" v-for="(pic,index) in info.pics">
              <img width="120" height="90"
              :src="pic"/>
            </li>
          </ul>
        </div>
      </section>

      <div class="split"></div>

      <section class="section">
        <h3 class="section-title">商家信息</h3>
        <ul class="detail">
          <li><span class="bold">品类</span> <span>{{info.category}}</span></li>
          <li><span class="bold">商家电话</span> <span>{{info.phone}}</span></li>
          <li><span class="bold">地址</span> <span>{{info.address}}</span></li>
          <li><span class="bold">营业时间</span> <span>{{info.workTime}}</span></li>
        </ul>
      </section>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import BScrool from '@better-Scroll/core'
	export default{
    data(){
      return{
        //【活动标签样式1】
        activeStyle:['activity-green','activity-red','activity-orange']
      }
    },
    mounted(){
      // 【2】如果数据还没有pics, 直接结束
      if(!this.info.pics) {
        return
      }
      // 创建滚动实例
      this._initScroll()
    },
    computed:{
      ...mapState(['info'])
    },
    watch:{
      // 【3】监视info数据如果更新了则再创建一次滚动实例
      info(){
        this.$nextTick(()=>{
          this._initScroll()
        })
      }
    },
    methods:{
      //【1】创建2个Bscroll滚动实例
      _initScroll(){
        //整体页面的纵向滚动
        new BScrool('.shop-info')

        // 动态计算ul的宽度
        const ul = this.$refs.picsUl
        const liWidth = 120
        const space = 6
        const count = this.info.pics.length

        /*把计算出的宽度加到ul样式里:这么做原因是
        better-scroll宽度必须大于父元素.pic-wrapper才能实现横向滚动*/
        ul.style.width = (liWidth + space) * count -space + 'px'

        // 商家图片展示滚动实例
        new BScrool('.pic-wrapper',{
          scrollX:true
        })
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixins.styl"

  .shop-info
    position: absolute
    top: 195px
    bottom: 0
    left: 0
    width: 100%
    background: #fff;
    overflow: hidden
    .section
      padding 16px 14px 14px
      font-size 16px
      background-color #fff
      color #666
      border-bottom 1px solid #eee
      position relative
      .section-title
        color #000
        font-weight 700
        line-height 16px
        > .iconfont
          float right
          color #ccc
      .delivery
        margin-top 16px
        font-size 13px
        line-height 18px
        .delivery-icon
          width 55px
          font-size 11px
          margin-right 10px
          display inline-block
          text-align center
          color #fff
          background-color #0097ff
          padding 1px 0
          border-radius 4px
        .delivery-money
          margin-top 5px

      .activity
        margin-top 16px
        .activity-item
          margin-bottom 12px
          display flex
          font-size 13px
          align-items center
          &.activity-green
            .content-tag
              background-color rgb(112, 188, 70)
          &.activity-red
            .content-tag
              background-color rgb(240, 115, 115)
          &.activity-orange
            .content-tag
              background-color: rgb(241, 136, 79)
          .content-tag
            display inline-block
            border-radius 2px
            width 36px
            height: 18px
            margin-right 10px
            color #fff
            font-style normal
            position relative
            .mini-tag
              position absolute
              left 0
              top 0
              right -100%
              bottom -100%
              font-size 24px
              transform scale(.5)
              transform-origin 0 0
              display flex
              align-items center
              justify-content center
      .pic-wrapper
        width: 100%
        overflow: hidden
        margin-top 16px
        .pic-list
          white-space: nowrap
          font-size: 0
          width 100%
          .pic-item
            display: inline-block
            margin-right: 6px
            width: 120px
            height: 90px
            &:last-child
              margin: 0
      .detail
        margin-bottom -16px
        > li
          display flex
          justify-content space-between
          align-items center
          margin-right -10px
          padding 16px 12px 16px 0
          line-height 16px
          bottom-border-1px(#ddd)
          font-size 13px
          > .bold
            font-weight 700
            color #333
          &:last-child
            border-none()


    .split
      width: 100%
      height: 16px
      border-top: 1px solid rgba(7, 17, 27, 0.1)
      border-bottom: 1px solid rgba(7, 17, 27, 0.1)
      background: #f3f5f7
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值