UNPKG

9.97 kBTypeScriptView Raw
1import { ComponentType } from 'react'
2import { StandardProps, CommonEventFunction, BaseEventOrigFunction } from './common'
3
4interface ScrollViewProps extends StandardProps {
5 /** 允许横向滚动
6 * @default false
7 * @supported weapp, swan, alipay, tt, h5, rn
8 * @rn 二选一
9 */
10 scrollX?: boolean
11
12 /** 允许纵向滚动
13 * @default false
14 * @supported weapp, swan, alipay, tt, h5, rn
15 * @rn 二选一
16 */
17 scrollY?: boolean
18
19 /** 距顶部/左边多远时(单位px),触发 scrolltoupper 事件
20 * @default 50
21 * @supported weapp, swan, alipay, tt, h5, rn
22 */
23 upperThreshold?: number
24
25 /** 距底部/右边多远时(单位px),触发 scrolltolower 事件
26 * @default 50
27 * @supported weapp, swan, alipay, tt, h5, rn
28 */
29 lowerThreshold?: number
30
31 /** 设置竖向滚动条位置
32 * @supported weapp, swan, alipay, tt, h5, rn
33 */
34 scrollTop?: number
35
36 /** 设置横向滚动条位置
37 * @supported weapp, swan, alipay, tt, h5, rn
38 */
39 scrollLeft?: number
40
41 /** 值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
42 * @supported weapp, swan, alipay, tt, h5
43 */
44 scrollIntoView?: string
45
46 /** 在设置滚动条位置时使用动画过渡
47 * @supported weapp, swan, alipay, tt, h5, rn
48 * @default false
49 */
50 scrollWithAnimation?: boolean
51
52 /** iOS 点击顶部状态栏、安卓双击标题栏时,滚动条返回顶部,只支持竖向
53 * @supported weapp, alipay, rn
54 * @default false
55 */
56 enableBackToTop?: boolean
57
58 /** 启用 flexbox 布局。开启后,当前节点声明了 `display: flex` 就会成为 flex container,并作用于其孩子节点。
59 * @supported weapp
60 * @default false
61 */
62 enableFlex?: boolean
63
64 /** 开启 scroll anchoring 特性,即控制滚动位置不随内容变化而抖动,仅在 iOS 下生效,安卓下可参考 CSS `overflow-anchor` 属性。
65 * @supported weapp
66 * @default false
67 */
68 scrollAnchoring?: boolean
69
70 /** 开启自定义下拉刷新
71 * @supported weapp
72 * @default false
73 */
74 refresherEnabled?: boolean
75
76 /** 设置自定义下拉刷新阈值
77 * @supported weapp
78 * @default 45
79 */
80 refresherThreshold?: number
81
82 /** 设置自定义下拉刷新默认样式,支持设置 `black | white | none`, none 表示不使用默认样式
83 * @supported weapp
84 * @default 'black'
85 */
86 refresherDefaultStyle?: string
87
88 /** 设置自定义下拉刷新区域背景颜色
89 * @supported weapp
90 * @default '#FFF'
91 */
92 refresherBackground?: string
93
94 /** 设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发
95 * @supported weapp
96 * @default false
97 */
98 refresherTriggered?: boolean
99
100 /** 启用 scroll-view 增强特性
101 * @supported weapp
102 * @default false
103 */
104 enhanced?: boolean
105
106 /** iOS 下 scroll-view 边界弹性控制 (同时开启 enhanced 属性后生效)
107 * @supported weapp
108 * @default true
109 */
110 bounces?: boolean
111
112 /** 滚动条显隐控制 (同时开启 enhanced 属性后生效)
113 * @supported weapp
114 * @default true
115 */
116 showScrollbar?: boolean
117
118 /** 分页滑动效果 (同时开启 enhanced 属性后生效)
119 * @supported weapp
120 * @default false
121 */
122 pagingEnabled?: boolean
123
124 /** boolean false 滑动减速速率控制 (同时开启 enhanced 属性后生效)
125 * @supported weapp
126 * @default false
127 */
128 fastDeceleration?: boolean
129
130 /** 滚动到顶部/左边,会触发 scrolltoupper 事件
131 * @supported weapp, swan, alipay, tt, h5, rn
132 */
133 onScrollToUpper?: CommonEventFunction
134
135 /** 滚动到底部/右边,会触发 scrolltolower 事件
136 * @supported weapp, swan, alipay, tt, h5, rn
137 */
138 onScrollToLower?: CommonEventFunction
139
140 /** 滚动时触发
141 * `event.detail = {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY}`
142 * @supported weapp, swan, alipay, tt, h5, rn
143 */
144 onScroll?: BaseEventOrigFunction<ScrollViewProps.onScrollDetail>
145
146 /** 自定义下拉刷新控件被下拉
147 * @supported weapp
148 */
149 onRefresherPulling?: CommonEventFunction
150
151 /** 自定义下拉刷新被触发
152 * @supported weapp
153 */
154 onRefresherRefresh?: CommonEventFunction
155
156 /** 自定义下拉刷新被复位
157 * @supported weapp
158 */
159 onRefresherRestore?: CommonEventFunction
160
161 /** 自定义下拉刷新被中止
162 * @supported weapp
163 */
164 onRefresherAbort?: CommonEventFunction
165
166 /** 滑动开始事件 (同时开启 enhanced 属性后生效)
167 * @supported weapp
168 */
169 onDragStart?: CommonEventFunction
170
171 /** 滑动事件 (同时开启 enhanced 属性后生效)
172 * @supported weapp
173 */
174 onDragging?: CommonEventFunction
175
176 /** 滑动结束事件 (同时开启 enhanced 属性后生效)
177 * @supported weapp
178 */
179 onDragEnd?: CommonEventFunction
180}
181
182declare namespace ScrollViewProps {
183 interface onScrollDetail {
184 /** 横向滚动条位置 */
185 scrollLeft: number
186 /** 竖向滚动条位置 */
187 scrollTop: number
188 /** 滚动条高度 */
189 scrollHeight: number
190 /** 滚动条宽度 */
191 scrollWidth: number
192 deltaX: number
193 deltaY: number
194 }
195}
196
197/** 可滚动视图区域。使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 height。组件属性的长度单位默认为 px
198 *
199 * Tips:
200 * H5 中 ScrollView 组件是通过一个高度(或宽度)固定的容器内部滚动来实现的,因此务必正确的设置容器的高度。例如: 如果 ScrollView 的高度将 body 撑开,就会同时存在两个滚动条(body 下的滚动条,以及 ScrollView 的滚动条)。
201 * 微信小程序 中 ScrollView 组件如果设置 scrollX 横向滚动时,并且子元素为多个时(单个子元素时设置固定宽度则可以正常横向滚动),需要通过 WXSS 设置 `white-space: nowrap` 来保证元素不换行,并对 ScrollView 内部元素设置 `display: inline-block` 来使其能够横向滚动。
202 * @classification viewContainer
203 * @supported weapp, swan, alipay, tt, h5, rn
204 * @example_react
205 * ```tsx
206 * export default class PageView extends Component {
207 * constructor() {
208 * super(...arguments)
209 * }
210 *
211 * onScrollToUpper() {}
212 *
213 * // or 使用箭头函数
214 * // onScrollToUpper = () => {}
215 *
216 * onScroll(e){
217 * console.log(e.detail)
218 * }
219 *
220 * render() {
221 * const scrollStyle = {
222 * height: '150px'
223 * }
224 * const scrollTop = 0
225 * const Threshold = 20
226 * const vStyleA = {
227 * height: '150px',
228 * 'background-color': 'rgb(26, 173, 25)'
229 * }
230 * const vStyleB = {
231 * height: '150px',
232 * 'background-color': 'rgb(39,130,215)'
233 * }
234 * const vStyleC = {
235 * height: '150px',
236 * 'background-color': 'rgb(241,241,241)',
237 * color: '#333'
238 * }
239 * return (
240 * <ScrollView
241 * className='scrollview'
242 * scrollY
243 * scrollWithAnimation
244 * scrollTop={scrollTop}
245 * style={scrollStyle}
246 * lowerThreshold={Threshold}
247 * upperThreshold={Threshold}
248 * onScrollToUpper={this.onScrollToUpper.bind(this)} // 使用箭头函数的时候 可以这样写 `onScrollToUpper={this.onScrollToUpper}`
249 * onScroll={this.onScroll}
250 * >
251 * <View style={vStyleA}>A</View>
252 * <View style={vStyleB}>B</View>
253 * <View style={vStyleC}>C</View>
254 * </ScrollView>
255 * )
256 * }
257 * }
258 * ```
259 * @example_vue
260 * ```html
261 * <template>
262 * <view class="container">
263 * <view class="page-body">
264 * <view class="page-section">
265 * <view class="page-section-title">
266 * <text>Vertical Scroll - 纵向滚动</text>
267 * </view>
268 * <view class="page-section-spacing">
269 * <scroll-view :scroll-y="true" style="height: 300rpx;" `@scrolltoupper="upper" `@scrolltolower="lower" `@scroll="scroll" :scroll-into-view="toView" :scroll-top="scrollTop">
270 * <view id="demo1" class="scroll-view-item demo-text-1">1</view>
271 * <view id="demo2" class="scroll-view-item demo-text-2">2</view>
272 * <view id="demo3" class="scroll-view-item demo-text-3">3</view>
273 * </scroll-view>
274 * </view>
275 * </view>
276 * <view class="page-section">
277 * <view class="page-section-title">
278 * <text>Horizontal Scroll - 横向滚动</text>
279 * </view>
280 * <view class="page-section-spacing">
281 * <scroll-view class="scroll-view_H" :scroll-x="true" `@scroll="scroll" style="width: 100%">
282 * <view id="demo21" class="scroll-view-item_H demo-text-1">a</view>
283 * <view id="demo22" class="scroll-view-item_H demo-text-2">b</view>
284 * <view id="demo23" class="scroll-view-item_H demo-text-3">c</view>
285 * </scroll-view>
286 * </view>
287 * </view>
288 * </view>
289 * </view>
290 * </template>
291 *
292 * <script>
293 * const order = ['demo1', 'demo2', 'demo3']
294 * export default {
295 * name: 'Index',
296 * data() {
297 * return {
298 * scrollTop: 0,
299 * toView: 'demo2'
300 * }
301 * },
302 *
303 * methods: {
304 * upper(e) {
305 * console.log('upper:', e)
306 * },
307 *
308 * lower(e) {
309 * console.log('lower:', e)
310 * },
311 *
312 * scroll(e) {
313 * console.log('scroll:', e)
314 * }
315 * }
316 * }
317 * </script>
318 *
319 * <style>
320 * .page-section-spacing{
321 * margin-top: 60rpx;
322 * }
323 * .scroll-view_H{
324 * white-space: nowrap;
325 * }
326 * .scroll-view-item{
327 * height: 300rpx;
328 * }
329 * .scroll-view-item_H{
330 * display: inline-block;
331 * width: 100%;
332 * height: 300rpx;
333 * }
334 *
335 * .demo-text-1 { background: #ccc; }
336 * .demo-text-2 { background: #999; }
337 * .demo-text-3 { background: #666; }
338 * </style>
339 * ```
340 * @see https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html
341 */
342declare const ScrollView: ComponentType<ScrollViewProps>
343
344export { ScrollView, ScrollViewProps }