uniapp小程序使用scroll-view组件实现上下左右滚动触发事件

在做uniapp开发小程序的时候,有一个需求是在一个表格区域里面可以上下左右滑动元素,并实现表头和左侧的标签联动效果,就想趣运动里面选择场地的效果一样,这里就用到了scroll-view组件,scroll-view官网文档地址:scroll-view | uni-app官网

但是使用的时候,要注意:

使用竖向滚动时,需要给 <scroll-view> 一个固定高度,通过 css 设置 height;使用横向滚动时,需要给<scroll-view>添加white-space: nowrap;样式。

就因为这个配置,就会导致你的滚动事件不会触发,所以一定要配置相应的宽度和高度!

代码:

                    <!-- 场地信息 -->
                    <scroll-view
                        class="scrollField"
                        scroll-x="true"
                        scroll-y="true"
                        @scroll="touchMove"
                    >
                        <view class="placeInfo">
                            <view class="column" v-for="p in 15">
                                <view v-for="cell in timeLabel" class="cell">
                                    ¥300
                                </view>
                            </view>
                        </view>
                    </scroll-view>



const touchMove = (event) => {
    console.log('handleTouchmove', event)
}

 css代码:

        .scrollField {
            height: 500rpx;
        }

        .placeInfo {
            display: flex;
            flex-direction: row;

            .column {
                .cell {
                    width: 100rpx;
                    background-color: #c2a3f2;
                    margin: 4rpx;
                    border-radius: 4rpx;
                    text-align: center;
                    color: white;
                }
            }
        }

最后实现的效果就是不论左右还是上下都可以触发滚动事件

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-15 22:46:15       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-15 22:46:15       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-15 22:46:15       87 阅读
  4. Python语言-面向对象

    2024-05-15 22:46:15       96 阅读

热门阅读

  1. 拓展(华为优秀网站)

    2024-05-15 22:46:15       31 阅读
  2. Qt | QTimer 类(计时器)

    2024-05-15 22:46:15       39 阅读
  3. vld.ini配置文件说明

    2024-05-15 22:46:15       34 阅读
  4. 基础环境配置

    2024-05-15 22:46:15       34 阅读
  5. LabVIEW软件开发工程师需要具备哪些能力与素质?

    2024-05-15 22:46:15       37 阅读
  6. 总结_看门狗项目应用解析

    2024-05-15 22:46:15       29 阅读
  7. Docker 容器连接:构建安全高效的容器化网络生态

    2024-05-15 22:46:15       33 阅读
  8. mysql(二)

    2024-05-15 22:46:15       32 阅读
  9. mysql中exists和in的区别

    2024-05-15 22:46:15       35 阅读
  10. MySQL变量的定义与使用

    2024-05-15 22:46:15       29 阅读
  11. Leecode热题100---128:最长连续数列

    2024-05-15 22:46:15       28 阅读