scroll-view.uvue 3.3 KB
Newer Older
Y
init  
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<template>
    <view>
        <page-head title="scroll-view,区域滚动视图"></page-head>
        <view class="uni-padding-wrap uni-common-mt">
            <view class="uni-title uni-common-mt">
                <text class="uni-title-text">Vertical Scroll</text>
                <text class="uni-subtitle-text">纵向滚动</text>
            </view>
            <view>
                <scroll-view :scrollTop="scrollTop" :scrollY="true" class="scroll-Y" @scrolltoupper="upper"
                    @scrolltolower="lower" @scroll="scroll">
                    <view id="demo1" class="scroll-view-item uni-bg-red"><text class="text">A</text></view>
                    <view id="demo2" class="scroll-view-item uni-bg-green"><text class="text">B</text></view>
                    <view id="demo3" class="scroll-view-item uni-bg-blue"><text class="text">C</text></view>
                </scroll-view>
            </view>
            <view @tap="goTop" class="uni-center uni-common-mt">
                <text class="uni-link">点击这里返回顶部</text>
            </view>

            <view class="uni-title uni-common-mt">
                <text class="uni-title-text">Horizontal Scroll</text>
                <text class="uni-subtitle-text">横向滚动</text>
            </view>
            <view>
                <scroll-view class="scroll-view_H" :scrollX="true" @scroll="scroll" :scrollLeft="120">
                    <view id="demo1" class="scroll-view-item_H uni-bg-red"><text class="text">A</text></view>
                    <view id="demo2" class="scroll-view-item_H uni-bg-green"><text class="text">B</text></view>
                    <view id="demo3" class="scroll-view-item_H uni-bg-blue"><text class="text">C</text></view>
                </scroll-view>
            </view>
            <view class="uni-common-pb"></view>
        </view>
    </view>
</template>
<script lang="ts">
    export default {
        data() {
            return {
                scrollTop: 0,
                oldScrollTop: 0
            }
        },
        methods: {
            upper: function (e : ScrollToUpperEvent) {
                console.log(e)
            },
            lower: function (e : ScrollToLowerEvent) {
                console.log(e)
            },
            scroll: function (e : ScrollEvent) {
                this.oldScrollTop = e.detail.scrollTop
            },
            goTop: function () {
                // 解决view层不同步的问题
                this.scrollTop = this.oldScrollTop
                this.$nextTick(function () {
                    this.scrollTop = 0
                });
                uni.showToast({
                    icon: "none",
                    title: "纵向滚动 scrollTop 值已被修改为 0"
                })
            }
        }
    }
</script>

<style>
    .scroll-Y {
        height: 300rpx;
    }

    .scroll-view_H {
        width: 100%;
        flex-direction: row;
    }

    .scroll-view-item {
        height: 300rpx;
        justify-content: center;
        align-items: center;
    }

    .scroll-view-item_H {
        width: 690rpx;
        height: 300rpx;
        justify-content: center;
        align-items: center;
    }

    .text {
        font-size: 36rpx;
        color: #ffffff;
    }
</style>