当前位置:   article > 正文

uniapp可滚动视图区域_uni滚动到视口位置出现

uni滚动到视口位置出现
<template>
    <view>
        <scroll-view :scroll-top="scrollTop" scroll-y="true" @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll" class="scroll-y" scroll-with-animation="true">
            <view class="scroll-y" style="background-color: red;">A</view>
            <view class="scroll-y" style="background-color: green;">B</view>
            <view class="scroll-y" style="background-color: blue;">C</view>
        </scroll-view>
        <view @click="goTop">点我回到顶部</view>
        <scroll-view @scroll="scroll1" :scroll-left="scrollLeft" class="scroll-x x" scroll-x="true" >
            <view class="scroll-x" style="background-color: red;">A</view>
            <view class="scroll-x" style="background-color: green;">B</view>
            <view class="scroll-x" style="background-color: blue;">C</view>
        </scroll-view>
        <view @click="goLeft">点我回到最左侧</view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                //当前距离顶部位置
                scrollTop:0,
                scrollLeft:0,
                old: {
                    //记录距离顶部位置
                    scrollTop: 0,
                    scrollLeft:0
                }
            }
        },
        onLoad(e) {
        },
        methods: {
            upper: function(e) {
                console.log('我到顶部了')
            },
            lower: function(e) {
                console.log('我到底部了')
            },
            scroll: function(e) {
                    // console.log(e)
                    this.old.scrollTop = e.detail.scrollTop
            },
            goTop: function(e) {
                    //给当前距离顶部位置赋值
                    this.scrollTop = this.old.scrollTop
                    //异步更新距离顶部位置
                    this.$nextTick(function() {
                            this.scrollTop = 0
                    });
            },
            scroll1(e){
                this.old.scrollLeft=e.detail.scrollLeft
            },
            goLeft(e){
                this.scrollLeft = this.old.scrollLeft
                //异步更新距离顶部位置
                this.$nextTick(function() {
                        this.scrollLeft = 0
                });
            }
        }
    }
</script>

<style>
    .scroll-y{
        width: 100%;
        height: 300rpx;
        text-align: center;
        line-height: 300rpx;
    }
    .scroll-x{
        width: 100%;
        height: 300rpx;
        text-align: center;
        line-height: 300rpx;
        display: inline-block;
    }
    .x{
        flex-wrap: nowrap;
        white-space: nowrap;
    }
</style>
  • 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
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/475135
推荐阅读
相关标签