UNPKG

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