uniApp 和微信小程序 scroll-view 實(shí)現(xiàn)區(qū)域滾動(dòng) 及scroll-top不生效的問題

1.上下滾動(dòng)因?yàn)楦叨鹊膯栴},scrollView沒有填滿所剩下的空間,沒有實(shí)現(xiàn)區(qū)域滾動(dòng)

  1. scroll-top 不生效的問題 ,比如想滾動(dòng)到底部
    在用這個(gè)標(biāo)簽之前,很多地方需要閱讀官方文檔比如這句話 :用豎向滾動(dòng)時(shí),需要給 一個(gè)固定高度,通過 css 設(shè)置 height。
    我就是因?yàn)闆]有設(shè)置高度導(dǎo)致 scroll-top不生效,但是設(shè)置高度百分之百又滿足不了區(qū)域滾動(dòng),設(shè)置百分之分有時(shí)還會(huì)錯(cuò)位 ----好了不比比,直接上代碼,建議全部復(fù)制過去

2025-2-19 又重新看了這個(gè)問題,發(fā)現(xiàn)是最外層content 設(shè)置高度100%導(dǎo)致的,把100%改為100vh,然后
scroll-view 高度寫死100% 即可,mounted動(dòng)態(tài)計(jì)算高度可以直接忽略了

.content {
        height: 100%;
}
<template>
    <view class="content flex-column">
        <view class="top-view flex-center" @tap="clickEvnet(0)">
            點(diǎn)擊我實(shí)現(xiàn)滾動(dòng)到頂部
        </view>
        <view class="scroll-view flex-1">
            <scroll-view :scroll-y="true" :scroll-top="scrollTop" class="h-full" scroll-with-animation="true">
                <block v-for="(item,index) in list" :key="index">
                    <view class="item-view">
                        {{item}}
                    </view>
                </block>
            </scroll-view>
        </view>
        <view class="bottom-view flex-center" @tap="clickEvnet(1)">
            點(diǎn)擊我實(shí)現(xiàn)滾動(dòng)到底部
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                scrollViewH: "",
                scrollTop: 0,
                list: []
            }
        },
        onLoad(options) {
            let that = this;
            for (var i = 0; i < 100; i++) {
                that.list.push("滾動(dòng)列表內(nèi)容" + i)
            }
        },
        mounted() {
            let that = this;
            const query = uni.createSelectorQuery().in(this);
            query.select('.scroll-view').boundingClientRect();
            query.exec(res => {
                const scrollViewH = res[0].height;
                console.log("scrollViewH==" + scrollViewH)
                that.scrollViewH = scrollViewH + "px"
            });
        },
        methods: {
            clickEvnet(type) {
                let that = this;
                that.goToBottom(type == 0 ? 0 : 99999)
            },
            // 容器滾動(dòng)到底部
            goToBottom(scrollTop) {
                let that = this
                that.scrollTop = scrollTop + Math.random() * 10
            },
        }
    }
</script>

<style>
    .content {
        height: 100vh;
    }

    .flex-column {
        display: flex;
        flex-direction: column;
    }
    .flex-center{
        align-items:center;
        justify-content:center;
    }
    .flex-1 {
        flex: 1;
    }

    .scroll-view {
        background-color: red;
        overflow: hidden;
    }

    .top-view,
    .bottom-view {
        background-color: #0081FF;
        height: 50px;
        color: #fff;
        font-size: 18px;
    }

    .item-view {
        color: #333333;
        padding: 10px;
        border-bottom: 1px solid #888888;
    }
.h-full{
        height: 100%;
    }
</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容