微信小程序 van-sticky + swiper + scroll-view + DropdownMenu + TreeSelect 实现顶部固定、可滑动/滚动/点击切换、选择地区、刷新、分页加载

本文介绍了如何在微信小程序中结合van-sticky、swiper、scroll-view、DropdownMenu和TreeSelect,实现顶部固定效果、滑动切换、点击选择地区以及刷新和分页加载功能。详细阐述了js、wxml和wxss的实现代码结构。

 一、如图:

  二、js:

import {
  getArea,
  getProductList
} from "../../../../../api/dsxapi";
const ui = require("../../../../../utils/ui");
Page({
  data: {
    navData: ['法律', '财税', '投融资', '知识产权', '企业产业链', '园区招商', '综合业务'],
    currentTab: 0, //标签下标
    navScrollLeft: 0,
    itemTitle: "服务地区",
    mainActiveIndex: 0,
    activeId: null,
    items: null,
    province: '', //省
    provinceId: null, //省份id
    marketId: null, //市id
    ProductList: [], //产品列表
    pages: 0,
    current: 1,
    top: 0
  },
  onLoad(options) {
    // 滚动
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowWidth: res.windowWidth
        })
      },
    })
  },
  onShow() {
    this.ObtainArea()
    this.ObtainProductList(this.data.current)
  },

  //获取地区
  ObtainArea() {
    getArea().then((res) => {
      let ress = res.data.map((item) => {
        return {
          text: item.areaName,
          id: item.areaId,
          children: [{
              text: '全部',
              id: -1
            },
            ...item.children.map((items) => {
              return {
                text: items.areaName,
                id: items.areaId
              }
            })
          ]
        }
      })
      ress.unshift({
        text: "全国"
      });
      this.setData({
        items: ress
      })
    }).catch(function (imError) {})
  },

  //获取产品列表
  ObtainProductList(current) {
    let params
    let category
    if (this.data.currentTab == 0) {
      category = 100621
    } else if (this.data.currentTab == 1) {
      category = 100622
    } else if (this.data.currentTab == 2) {
      category = 100623
    } else if (this.data.currentTab == 3) {
      category = 100624
    } else if (this.data.currentTab == 4) {
      category = 100625
    } else if (this.data.currentTab == 5) {
      category = 100626
    } else if (this.data.currentTab == 6) {
      category = 100627
    }
    if (this.data.marketId !== null) {
      params = {
        category: category, //标签id
        provinceId: this.data.provinceId, //省id
        cityId: this.data.marketId, //市id
        current: current,
        searchId: 11,
        size: 100,
      };
    } else {
      params = {
        category: category, //标签id
        current: current,
        size: 20,
      };
    }
    getProductList(params).then((res) => {
      let ProductList
      if (res.data.current == 1) {
        ProductList = res.data.records;
        ProductList.forEach((item, index) => {
     
微信小程序使用 Vant 的 `van-sticky` 组件时,如果发现吸顶效果不生效,通常是由于页面结构、滚动容器设置或组件配置不当所导致。以下是几种常见的解决方法和优化建议: ### 1. 确保页面滚动容器正确设置 如果页面中使用了 `scroll-view` 作为滚动区域,`van-sticky` 的吸顶效果可能会失效。这是因为 `van-sticky` 默认监听的是页面滚动事件,而 `scroll-view` 的滚动不会触发全局滚动事件。 **解决方法:** 通过手动将 `scroll-view` 的 `scroll-top` 和 `offset-top` 属性传递给 `van-sticky` 组件,实现吸顶效果[^4]。 ```html <scroll-view bind:scroll="onScroll" scroll-y id="scroller" style="height: 200px;"> <view style="height: 400px; padding-top: 50px;"> <van-sticky scroll-top="{{ scrollTop }}" offset-top="{{ offsetTop }}"> <van-button type="warning">嵌套在 scroll-view 内</van-button> </van-sticky> </view> </scroll-view> ``` ```javascript Page({ data: { scrollTop: 0, offsetTop: 0, }, onScroll(event) { wx.createSelectorQuery() .select('#scroller') .boundingClientRect((res) => { this.setData({ scrollTop: event.detail.scrollTop, offsetTop: res.top, }); }) .exec(); }, }); ``` ### 2. 检查页面滚动行为 如果页面中没有使用 `scroll-view`,而是依赖原生滚动行为,可以使用 `onPageScroll` 方法获取滚动位置,并确保 `van-sticky` 所在的容器没有设置 `overflow: hidden` 或 `transform` 等影响定位的样式。 ```javascript Page({ onPageScroll(e) { const scrollTop = e.detail.scrollTop; // 可选:更新数据或触发吸顶逻辑 }, }); ``` ### 3. 避免全局样式干扰 在某些项目中,为了统一滚动行为,可能会在全局样式中设置 `body` 的 `overflow` 属性为 `scroll`,这会导致 `van-sticky` 吸顶行为异常。建议将 `body` 的 `overflow` 设置为 `visible`,以确保吸顶组件能够正确监听滚动事件[^1]。 ```css /* app.wxss */ page { overflow: visible; } ``` ### 4. 使用 `offset-top` 控制吸顶位置 `van-sticky` 提供了 `offset-top` 属性,用于设置组件距离顶部的吸顶位置。如果吸顶位置不准确,可以显式设置该值,以适配不同设备和布局。 ```html <van-sticky offset-top="100"> <van-tabs> <van-tab title="标签1">内容1</van-tab> <van-tab title="标签2">内容2</van-tab> </van-tabs> </van-sticky> ``` ### 5. 避免多个吸顶组件冲突 如果页面中存在多个 `van-sticky` 组件,可能会导致吸顶效果互相干扰。建议确保每个 `van-sticky` 的作用范围独立,或通过条件渲染控制只显示一个吸顶组件。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D_evin_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值