uniapp微信小程序遇到scroll-into-view不生效的问题

简单做个记录

正常按照uni官方文档配置就好,之前项目也做过类似需求,但是前两天遇到了设置不生效的问题。

一、想到的几个可能情况:

1、scroll-view 没有高度。

        方式1:初始高度设置为0 通过flex: 1 1auto;填充满剩余高度

<scroll-view :scroll-y="true" style="height: 0" :scroll-into-view="defaultId" class="match-list border-box u-flex-fill u-flex-column">
    <view>something</view>
</scroll-view>

         方式2:本地项目使用的方式,页面创建完成获取父元素高度减去固定的比如输入框等元素高度来设置scroll-view的高度(第一版)

// template
<scroll-view class="im-message-list" id="refMsgList" scroll-y="true" :style="{ height: contentHeight - 90 + 'px' }" :scroll-into-view="scrollIntoIndex">
    <view>something</view>
</scroll-view>


// script
initHeight() {
            let screenHeight = uni.getSystemInfoSync().screenHeight;

            this.$nextTick(() => {
                const query = uni.createSelectorQuery().in(this);
                query
                    .select('.tab-content')
                    .boundingClientRect((data) => {
                        const contentHeight = screenHeight - data.top;
                        this.contentHeight = contentHeight - uni.getSystemInfoSync().statusBarHeight - 80;
                    })
                    .exec();
            });
        },

2、scroll-into-view和子元素的id设置有问题(经测试设置没问题)

二、最终处理方式:

调整scroll-view高度设置,在js中计算完成了再放到标签style属性内

// template
<scroll-view class="im-message-list" id="refMsgList" scroll-y="true" :style="{ height: messageHeight }" :scroll-into-view="scrollIntoIndex">
    <view>something</view>
</scroll-view>


// script
initHeight() {
            let screenHeight = uni.getSystemInfoSync().screenHeight;

            this.$nextTick(() => {
                const query = uni.createSelectorQuery().in(this);
                query
                    .select('.tab-content')
                    .boundingClientRect((data) => {
                        const contentHeight = screenHeight - data.top;
                        this.contentHeight = contentHeight - uni.getSystemInfoSync().statusBarHeight - 80;
                        this.messageHeight = `${this.contentHeight - 90}px`
                        if (this.messageList.length) {
                            const index = this.messageList[this.messageList.length - 1]?.id
                            this.scrollIntoIndex = `chat-${index}`
                        }
                    })
                    .exec();
            });
        },

 具体为什么初版不生效还不清楚,有可能模板内语法有问题,但是我看着没毛病哈哈哈。。。。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,实现微信小程序scroll-viewscroll-into-view 无效果可能是由以下几个原因导致的: 1. scroll-into-view 属性的值与要跳转到的锚点标记的 id 属性值一致。例如,如果要跳转到一个 id 为 "anchor1" 的锚点标记,那么 scroll-into-view 的值应该设置为 "anchor1"。 2. scroll-view 容器高度不足:如果 scroll-view 容器的高度不足以显示要跳转到的锚点标记,那么 scroll-into-view 将无法生效。确保 scroll-view 容器的高度足够大,以容纳所有的内容和要跳转到的锚点标记。 3. scroll-into-view 设置在子组件上:scroll-into-view 属性应该设置在 scroll-view 组件上,而不是其子组件上。确保 scroll-into-view 属性被正确地设置在 scroll-view 组件上。 4. scroll-into-view 设置在动态生成的内容上:如果要跳转到的锚点标记是在动态生成的内容中,那么需要在动态生成内容之后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容渲染完成后才能生效。 5. scroll-into-view 设置在隐藏的内容上:如果要跳转到的锚点标记是在初始状态下是隐藏的内容中,那么需要在显示该内容后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容显示后才能生效。 以下是一个示例代码,演示了如何在微信小程序中使用 scroll-viewscroll-into-view 实现锚点跳转效果: ```html <scroll-view scroll-into-view="{{toView}}" scroll-y="true" style="height: 300px;"> <view id="anchor1">锚点1</view> <view id="anchor2">锚点2</view> <view id="anchor3">锚点3</view> <view id="anchor4">锚点4</view> <view id="anchor5">锚点5</view> </scroll-view> <button bindtap="scrollToAnchor">跳转到锚点2</button> ``` ```javascript Page({ data: { toView: '' }, scrollToAnchor: function() { this.setData({ toView: 'anchor2' }) } }) ``` 在上述示例中,scroll-view 组件设置了 scroll-into-view 属性为 toView 变量的值,当点击按钮时,toView 变量的值被设置为 "anchor2",从而实现了跳转到锚点2的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值