1、横向滚动基本写法
<template> <view> <!-- 横向滚动条 --> <view class="uni-padding-wrap uni-common-mt"> <scroll-view class="scroll-view_H" scroll-x="true" @scroll="scroll" scroll-left="100%" @scrolltolower="scrolltolower"> <view class="scroll-view-item_H uni-bg-red">A</view> <view class="scroll-view-item_H uni-bg-green">B</view> <view class="scroll-view-item_H uni-bg-blue">C</view> </scroll-view> </view> </view> </template> <script> export default { data() { return { } }, methods: { scroll(event) { //距离每个边界距离 console.log(event.detail) }, //滚动到底部/右边触发 scrolltolower() { console.log(1111); }, // 滚动到顶部/左边触发 scrolltoupper() { console.log(2222); } } } </script> <style lang="scss"> .scroll-view_H { white-space: nowrap; .scroll-view-item_H { display: inline-block; 100%; height: 100px; } .uni-bg-red { background: red; } .uni-bg-green { background: green; } .uni-bg-blue { background: blue; } } </style>
2、纵向滚动基本写法
<template> <view> <!-- 这层标签没有也可以 --> <view class="uni-padding-wrap uni-common-mt"> <view> <scroll-view class="scroll-view" scroll-y="true" :scroll-top="scrollTop" @scroll="scroll" @scrolltoupper="upper" @scrolltolower="lower"> <view class="scroll-view-item top">上</view> <view class="scroll-view-item center">中</view> <view class="scroll-view-item bottom">下</view> </scroll-view> </view> <view @tap="goTop" class="uni-link uni-center uni-common-mt"> 点击这里返回顶部 </view> </view> </view> </template> <script> export default { data() { return { scrollTop: 0, old: { scrollTop: 0, } } }, methods: { scroll(e) { this.old.scrollTop = e.detail.scrollTop }, goTop() { //这里必须将this.old.scrollTop值赋值给this.scrollTop this.scrollTop = this.old.scrollTop; this.$nextTick(function() { this.scrollTop = 0 }); }, upper(){ // 滚动到顶部/左边触发 }, lower(){ // 滚动到底部/右边触发 } } } </script> <style lang="scss"> .scroll-view { // white-space: nowrap; height: 200px; 100%; .top { height: 200px; 100%; background: red; } .center { height: 200px; 100%; background: green; } .bottom { height: 200px; 100%; background: blue; } } </style>