小程序中的吸顶效果(小程序操作DOM)

小程序中的吸顶效果

在这里插入图片描述

  1. wxml


<!-- 吸顶view -->
<view hidden="{{showTabControl}}" class="top">
<van-tabs active="a">
  <van-tab title="标签 1" name="a"></van-tab>
  <van-tab title="标签 2" name="b"></van-tab>
  <van-tab title="标签 3" name="c"></van-tab>
</van-tabs>
</view>
<!-- 滚动容器 -->
<scroll-view 
scroll-y 
lower-threshold="100" 
id="scroller"
bindscrolltolower="scrollToLower" 
bindscroll="scrollPosition"
scroll-with-animation="true"
scroll-top="{{topPosition}}"
style="height:100vh">

<view style='height:200px;border:1px solid black;background:red'></view>

<!-- 吸顶位置 -->
<van-tabs active="a" class='xiding'>
<van-tab title="标签 1" name="a"></van-tab>
  <van-tab title="标签 2" name="b">内容 2</van-tab>
  <van-tab title="标签 3" name="c">内容 3</van-tab>
</van-tabs>

<view wx:for="{{listData}}" wx:key="{{index}}" style="height: 100px;border-bottom: 1px solid #f4f4f4;">
  <van-card
  num="2"
  price="{{item.price}}"
  title="{{item.title}}"
  thumb="{{ item.show.img }}"
  bindtap='go'
  data-id='{{item.iid}}'
   />
    </view>
    <view style="text-align: center;margin: 10px;">
      <view wx:if="{{loading}}">加载中...</view>
      <view wx:if="{{noMore}}">没有更多了</view>
      <view wx:if="{{loadingFailed}}">数据加载失败,请重试</view>
    </view>
  
 </scroll-view>

  1. wxss
.top{
  display: fixed;
  left: 0;
  top:0;
}

  1. js
Page({
data: {
   showTabControl: true //默认隐藏吸顶
  },
scrollPosition(e) {
  wx.createSelectorQuery().select('.xiding').boundingClientRect((rect) => {
      // console.log(rect)
      const show = rect.top > 0
      this.setData({
        showTabControl: show
      })
    }).exec()
  },
})

小程序操作DOM

<scroll-view scroll-y='true' style="height:100vh;" bindscroll="bindscroll" scroll-into-view="{{toview}}">
	<view class="rol">
		<text class="title" id="normalServe">常用应用</text>
		<text class="title" id="normalServe1">常用应用2</text>
		<text class="title" id="normalServe2">常用应用3</text>
	</view>
</scroll-view>
<view class="dadasfa"  wx:if="{{showNav}}"> dsadasfaf
</view>
Page({
  //声明节点查询的方法
  queryMultipleNodes: function() {
    const query = wx.createSelectorQuery()                // 创建节点查询器 query
    query.select('#productServe').boundingClientRect()    // 这段代码的意思是选择Id=productServe的节点,获取节点位置信息的查询请求
    query.select('#enterpriseServe').boundingClientRect() // 这段代码的意思是选择Id=enterpriseServe的节点,获取节点位置信息的查询请求
    query.select('#normalServe').boundingClientRect()     // 这段代码的意思是选择Id=normalServe的节点,获取节点位置信息的查询请求
    query.selectViewport().scrollOffset()                 // 这段代码的意思是获取页面滑动位置的查询请求
    query.exec((res) => {
      res[0].top                                          // #productServe节点的到页面顶部的距离
      res[1].width                                        // #enterpriseServe节点的宽度
      res[2].height                                       // #normalServe节点的高度
    })
  }
})
console.log(res) 输出节点信息详情
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

star@星空

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

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

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

打赏作者

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

抵扣说明:

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

余额充值