微信小程序实现锚点效果 scroll-view的scroll-into-view属性

小程序中实现锚点效果,可以直接使用scroll-view的scroll-into-view属性就可以实现锚点效果,比较方便简单。那么需要用到scroll-view那些参数呢,下面具体讲讲:

  • scroll-x | scroll-y:设置滚动刚想
  • scroll-into-view:子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素。
  • scroll-with-animation:设置滚动条位置时使用动画过渡

效果展示:
在这里插入图片描述

wxml

<view class="fixed">
    <block wx:for="{{4}}" wx:key="index">
        <view class="bg{{index}}" data-index="{{index}}" bindtap="getViewId">内容</view>
    </block>
</view>
<scroll-view scroll-y class="scroll" scroll-into-view="{{viewid}}" scroll-with-animation>
    <block wx:for="{{4}}" wx:key="index">
        <view id="cont{{index}}" class="bg{{index}}">内容</view>
    </block>
</scroll-view>

注意: viewid值是动态的,通过setData设置即可。

js

Page({
    data: {
        viewid: ""
    },
    getViewId(e) {
        this.setData({
            viewid: "cont" + e.currentTarget.dataset.index
        })
    }
})

wxss

page {
    font-size: 28rpx;
    text-align: center;
}

.bg0 {
    background: #CCCC99;
}

.bg1 {
    background: #CC9966;
}

.bg2 {
    background: #666666;
}

.bg3 {
    background: #CC9999;
}

.fixed {
    border: 3rpx solid #eee;
    width: 100rpx;
    line-height: 100rpx;
    position: fixed;
    bottom: 10%;
    right: 0;
    z-index: 1;
}

.fixed view {
    width: 100rpx;
}

.scroll {
    height: 100vh;
}

.scroll view {
    width: 100%;
    height: 1000rpx;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FaxMiao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值