weex实现滚动条和导航栏联动

本文介绍了如何修改Weex官方示例,实现在滚动条滑动时导航栏联动,提升用户体验。通过调整scroller和elevator组件的交互,确保视图同步更新。

 改写weex官方实例,使其可以实现滚动条和导航栏联动

入口

<template>
    <scroller @scroll="goTO" offset-accuracy="100" class="wrapper">
        <div class="floor" v-for="(name, index) in floors" :ref="'floor'+name">
            <text class="floor-title">- {{name}} -</text>
            <div class="goods"></div>
        </div>
        <div style="padding-bottom: 500wx;"></div>
        <scroller class="elevator" scroll-direction="horizontal" show-scrollbar="false">
            <text class="elevator-title">主要会场:</text>
            <div class="elevator-item" v-for="(name, index) in floors" :ref="'item'+index"
                @click="go2floor($event, 'floor'+name)">
                <text :style="{color:typeIndex===index?'red':''}" class="item-text" :ref="'text'+index">{{name}}</text>
            </div>
            <div style="padding-right: 400wx;"></div>
        </scroller>
    </scroller>
</template>

<script>
const dom = weex.requireModule('dom') || {}

export default {
  data () {
    return {
      typeIndex: 0,
      w: 0,
      floors: ['买手精选', '进口好货', '魅力美妆', '滋补保健', '进口母婴', '环球美食', '数码家电', '个护家清', '家居家纺', '名庄酒水']
    }
  },
  methods: {
    go2floor: function (event, refId) {
      const target = event.target
      const ref = this.$refs[refId]
      const el = ref ? ref[0] : null

      dom.scrollToElement(target, {
        offset: -300
      })
      if (el) {
        dom.scrollToElement(el, {
          offset: -60
        })
      }
    },
    goTO (e) {
      const s = e.contentOffset
      // 获取scroller高度除单个宽度取整
      this.typeIndex = -(Math.floor(s.y / this.w) + 1)
      const name = this.floors[this.typeIndex]
      const ref = this.$refs['item' + this.typeIndex]
      if (this.typeIndex >= 0) {
        dom.scrollToElement(ref, {
          offset: -300
        })
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      const name = 'floor买手精选'
      // 获取不同设备下商品信息的高度
      this.w = this.$refs[name][0].clientHeight
    })
  }
}
</script>

<style scoped>
    .wrapper {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-color: #f4f4f4;
        padding-top: 90wx;
    }

    .elevator {
        height: 100wx;
        padding-left: 15wx;
        padding-right: 15wx;
        background-color: #39f;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
    }

    .elevator-item {
        line-height: 90wx;
        margin: 0wx 15wx;
    }

    .elevator-title {
        font-size: 36wx;
        font-weight: 36wx;
        color: #fff;
        line-height: 90wx;
    }

    .item-text {
        font-size: 36wx;
        color: #fff;
        line-height: 90wx;
    }

    .floor {
        padding: 30wx;
    }

    .floor-title {
        font-size: 40wx;
        text-align: center;
        line-height: 70wx;
    }

    .goods {
        height: 600wx;
        border-width: 2wx;
        border-style: solid;
        border-color: rgb(162, 217, 192);
        background-color: rgba(162, 217, 192, 0.2);
    }

    .floor:after {
        padding-bottom: 500wx;
    }
</style>

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值