美文网首页
微信小程序点击标签滑动到相应内容,滚动内容切换相应标签实现

微信小程序点击标签滑动到相应内容,滚动内容切换相应标签实现

作者: ChinaGoodStaff | 来源:发表于2019-08-29 17:48 被阅读0次

这个是需要实现的效果(点击上面的标签自动滚动到相应内容,滚动内容也会自动切换上面的标签)


34382146037140_.pic.jpg

先来看一下代码:

js文件中Page里定义数据(上面需要显示的标签内容):
data: {
    current: {
      menu: 1
    },
    menus: [{
        id: 1,
        text: '房间',
        active: true,
        scrollSubviewId: "roomList",
        hidden: false,
      },
      {
        id: 2,
        text: '美宿介绍',
        active: false,
        scrollSubviewId: "introduce",
        hidden: false,

      },
      {
        id: 3,
        text: '独一份',
        active: false,
        scrollSubviewId: "mall",
        hidden: true,
      },
      {
        id: 4,
        text: '推荐',
        active: false,
        scrollSubviewId: "recommend",
        hidden: true,
      },
      {
        id: 5,
        text: '热评',
        active: false,
        scrollSubviewId: "comment",
        hidden: true,
      }
    ],
    top: 0,
  },

wxml文件中代码

<view class="top-tab">
  <block wx:for="{{menus}}" wx:key="menu.list">
    <view class="top-tab-item{{item.active ? ' active' : ''}}" bindtap="scrollToView" data-item="{{item}}" hidden='{{item.hidden}}'>{{item.text}}</view>
  </block>
</view>
  scrollToView: function(e) {
    var item = e.currentTarget.dataset.item || {};
    var menus = this.data.menus;
    menus.forEach((f) => {
      if (f.id == item.id)
        f.active = true;
      else
        f.active = false;
    });
    this.setData({
      menus: menus,
      'current.menu': item.id,
      toView: item.scrollSubviewId,
      tapMenu: true
    });
  },
  scrollDidScroll: function(e) {
    var top = e.detail.scrollTop;
    if (top > 20) {
      if (!this.data.showBackTopView) {
        this.setData({
          showBackTopView: true,
        });
      }
    } else {
      if (this.data.showBackTopView) {
        this.setData({
          showBackTopView: false,
        });
      }
    }
    if (this.data.tapMenu) {
      this.setData({
        tapMenu: false,
      });
      return;
    }
    var introduceTop = this.data.introduceTop;
    var mallTop = this.data.mallTop || (introduceTop + this.data.introduceHeight);
    var recommendTop = this.data.recommendTop || (mallTop + this.data.mallHeight);
    var commentTop = this.data.commentTop || (recommendTop + this.data.recommendHeight);
    var id = 1;
    if (top > commentTop) {
      id = 5;
    }
    if (this.data.recommendHeight > 0) {
      if (top > recommendTop && top < (this.data.commentHeight > 0 ? commentTop : Number.POSITIVE_INFINITY)) {
        id = 4;
      }
    }
    if (this.data.mallHeight > 0) {
      if (top > mallTop && top < (this.data.recommendHeight > 0 ? recommendTop : (this.data.commentHeight > 0 ? commentTop : Number.POSITIVE_INFINITY))) {
        id = 3;
      }
    }
    if (top > introduceTop && top < (this.data.mallHeight > 0 ? mallTop : (this.data.recommendHeight > 0 ? recommendTop : (this.data.commentHeight > 0 ? commentTop : Number.POSITIVE_INFINITY)))) {
      id = 2;
    }
    if (top < introduceTop && top > 0) {
      id = 1;
    }
    if (this.data.current.menu != id) {
      var menus = this.data.menus;
      menus.forEach((f) => {
        if (f.id == id)
          f.active = true;
        else
          f.active = false;
      });
      this.setData({
        menus: menus,
        'current.menu': id
      });
    }
  },
  getTops: function() {
    var that = this;
    var menus = this.data.menus;
    for (let i = 0; i < menus.length; i++) {
      if (!menus[i].hidden) {
        var query = wx.createSelectorQuery() //创建节点查询器
        let floorID = '#' + menus[i].scrollSubviewId;
        query.select(floorID).boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function(res) {
          if (res[0]) {
            var result = res[0];
            if (result.id == 'introduce') {
              that.setData({
                introduceTop: result.top,
                introduceHeight: result.height || 0,
              });
            } else if (result.id == 'mall') {
              that.setData({
                mallTop: result.top,
                mallHeight: result.height || 0,
              });
            } else if (result.id == 'recommend') {
              that.setData({
                recommendTop: result.top,
                recommendHeight: result.height || 0,
              });
            } else if (result.id == 'comment') {
              that.setData({
                commentTop: result.top,
                commentHeight: result.height || 0,
              });
            }
          }
        })
      }
    }
  },

相关文章

  • 微信小程序点击标签滑动到相应内容,滚动内容切换相应标签实现

    这个是需要实现的效果(点击上面的标签自动滚动到相应内容,滚动内容也会自动切换上面的标签) 先来看一下代码: js文...

  • 微信小程序利用scroll-view和swiper实现标签页

    小程序实现一个联动的效果,具体效果看图。 上方是一个可滚动的标签栏。下面也是可以滚动的内容区。点击上方标签栏,下方...

  • 2.Axure动态面板之Tab标签页卡

    Tab标签页卡 当点击不同的页卡时,内容区域会发生对应的变化。 效果 点击不同的页卡,切换相应的内容,并且页卡状态...

  • 自定义ScrollView和TabLayout联动(一)

    最近在做新项目,需要实现一个淘宝商品详情页的效果,根据滚动的距离切换顶部的tab标签,点击tab标签可以滚动到指定...

  • 网易新闻与腾讯新闻直播模块分析

    一、主界面 1、腾讯直播模块的导航栏包含很多内容的标签,点击标签的分类或左右滑动界面可切换到相应的板块。 1、在导...

  • a标签锚点的问题

    a便签作为锚点定位页面的位置还是很常见的,基本实现如下: 但是,点击锚点,会使相应内容直接滚动到页面最顶部,如果有...

  • 微信小助手

    批量添加标签 微信聊天界面,点击内容,选择更多,选中同类标签内容,标注标签。 常用微信功能界面 聊天文件查找 设为...

  • target伪类实现标签切换不起作用的问题

    目的是希望实现点击标签切换相应的页,原始代码如下: 问题: 实际上无论我怎么切换都只显示了live页; 解决办法:...

  • 滚动标题条的创建

    首先,预览一下实现效果: 滚动标签条在应用中起到导航的功能,可将正文内容分成具体的内容模块,用户点击标签时相对...

  • 设置a标签的激活样式

    有时我们需要设置点击某个a标签来请求相应的数据,之后需要给这个a标签设置相应的激活样式。如果通过a标签的点击事件来...

网友评论

      本文标题:微信小程序点击标签滑动到相应内容,滚动内容切换相应标签实现

      本文链接:https://www.haomeiwen.com/subject/oeiljctx.html