scroll-view-props.uvue 7.6 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2 3 4 5
<template>
	<view style="flex:1;">
		<page-head title="非下拉刷新的scroll-view属性示例"></page-head>
		<view class="uni-margin-wrap">
			<!-- 暂时分成两个方向不同的滚动视图,原因为:scroll-x或scroll-y属性一经设置不能动态改变。 -->
shutao-dc's avatar
shutao-dc 已提交
6
			<scroll-view v-if="scrollX" :scroll-x="true" :scroll-y="false" :scroll-top="scrollTop" :scroll-left="scrollLeft"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
7
				:upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
雪洛's avatar
雪洛 已提交
8
				:enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation" style="flex-direction: row;width: 100%;height: 100%;"
9
				:show-scrollbar="showScrollbar" :rebound="rebound" @scrolltoupper="scrolltoupper"
A
Anne_LXM 已提交
10
				@scrolltolower="scrolltolower" @scroll="scroll" @scrollend="scrollend" ref="scrollViewX" id="scrollViewX">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
11 12 13 14
				<view class="item" :id="'horizontal_'+item.id" v-for="(item,_) in items">
					<text class="uni-text">{{item.label}}</text>
				</view>
			</scroll-view>
15
			<scroll-view v-else :scroll-y="scrollY"  :scroll-top="scrollTop" :scroll-left="scrollLeft"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
16 17
				:upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
				:enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation"
雪洛's avatar
雪洛 已提交
18
				:show-scrollbar="showScrollbar" :rebound="rebound" @scrolltoupper="scrolltoupper"
19
				@touchmove="onTouchMove"
雪洛's avatar
雪洛 已提交
20
				@scrolltolower="scrolltolower" @scroll="scroll" @scrollend="scrollend" ref="scrollViewY" id="scrollViewY" style="width: 100%;height: 100%;">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
				<view class="item" :id="item.id" v-for="(item,_) in items">
					<text class="uni-text">{{item.label}}</text>
				</view>
			</scroll-view>
		</view>

		<scroll-view class="uni-list" showScrollbar="true">
			<view class="uni-option">
				<text>是否显示滚动条</text>
				<switch :checked="showScrollbar" @change="showScrollbar=!showScrollbar"></switch>
			</view>
			<view class="uni-option">
				<text>是否有反弹效果</text>
34
				<switch :checked="rebound" @change="rebound=!rebound"></switch>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
35 36 37 38 39 40 41
			</view>
			<view class="uni-option">
				<text>是否开启滚动时使用动画过渡</text>
				<switch :checked="scrollWithAnimation" @change="scrollWithAnimation=!scrollWithAnimation"></switch>
			</view>
			<view class="uni-option">
				<text>是否横向滚动</text>
42
				<switch :checked="scrollX" @change="changeDirectionX"></switch>
雪洛's avatar
雪洛 已提交
43 44 45 46
			</view>
			<view class="uni-option">
				<text>是否竖向滚动</text>
				<switch :checked="scrollY" @change="changeDirectionY"></switch>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
			</view>
			<view class="uni-common-pb"></view>
			<view class="uni-slider">
				<text>拖动设置scroll-top</text>
				<slider :max="1000" :min="0" :step="10" :value="scrollTop" :show-value="true"
					@change="handleChangeScrollTop" style="margin-top: 10rpx;" />
			</view>
			<view class="uni-common-pb"></view>
			<view class="uni-slider">
				<text>拖动设置scroll-left</text>
				<slider :max="1000" :min="0" :step="10" :value="scrollLeft" :show-value="true"
					@change="handleChangeScrollLeft" style="margin-top: 10rpx;" />
			</view>

			<view class="uni-option">
				<text>设置触发scrolltoupper的距离</text>
xuty73419315's avatar
xuty73419315 已提交
63
				<input style="width: 100rpx;border-width: 2rpx;text-align: center;border-style: solid; " :value="upperThreshold"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
64 65 66 67 68
					type="number" @input="handleUpperThresholdInput" />
			</view>

			<view class="uni-option">
				<text>设置触发scrolltolower的距离</text>
xuty73419315's avatar
xuty73419315 已提交
69
				<input style="width: 100rpx;border-width: 2rpx;text-align: center;border-style: solid; " :value="lowerThreshold"
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
70 71 72 73 74
					type="number" @input="handleLowerThresholdInput" />
			</view>

			<view class="uni-common-pb"></view>
			<view class="uni-slider">
A
Anne_LXM 已提交
75
				<button type="primary" class="button" @click="handleScrollIntoView">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
					滚动到id为`item3`的子视图
				</button>
			</view>
			<view class="uni-common-pb"></view>
		</scroll-view>

	</view>
</template>

<script>
	type Item = {
		id : string,
		label : string,
	}
	export default {
		data() {
			return {
				items: [] as Item[],
雪洛's avatar
雪洛 已提交
94
				scrollX: false,
95
				scrollY: true,
96
				rebound: false,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
				scrollTop: 0,
				scrollLeft: 0,
				scrollIntoView: "",
				enableBackToTop: false,
				scrollWithAnimation: false,
				showScrollbar: false,
				upperThreshold: 50,
				lowerThreshold: 50,
			}
		},

		onLoad() {
			for (let i = 0; i < 10; i++) {
				const item = {
					id: "item" + i,
					label: "item" + i,
				} as Item;
				this.items.push(item);
			}
		},
		methods: {
			handleChangeScrollLeft(e : SliderChangeEvent) {
				this.scrollLeft = e.detail.value;
			},
			handleChangeScrollTop(e : SliderChangeEvent) {
				this.scrollTop = e.detail.value;
			},
124
			changeDirectionX() {
雪洛's avatar
雪洛 已提交
125 126 127 128 129 130 131 132 133 134 135
				this.scrollX = !this.scrollX;
				if(this.scrollX) {
					this.scrollY = false
				}
				this.scrollTop = 0;
				this.scrollLeft = 0;
			},
			changeDirectionY() {
				this.scrollY = !this.scrollY;
				if(this.scrollY) {
					this.scrollX = false
136
				}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
				this.scrollTop = 0;
				this.scrollLeft = 0;
			},
			handleScrollIntoView() {
				if (this.scrollX) {
					this.scrollIntoView = "horizontal_item3";
				} else {
					this.scrollIntoView = "item3";
				}
				//重置状态,由于响应式的原因,设置的值与当前值相同,会导致再次设置无效果。
				setTimeout(() => {
					this.scrollIntoView = "";
				}, 0);
			},
			handleUpperThresholdInput(e : InputEvent) {
				const value = e.detail.value;
				if (value == "") {
					this.upperThreshold = 50;
				} else {
					this.upperThreshold = parseInt(e.detail.value);
				}
			},
			handleLowerThresholdInput(e : InputEvent) {
				const value = e.detail.value;
				if (value == "") {
					this.lowerThreshold = 50;
				} else {
					this.lowerThreshold = parseInt(e.detail.value);
				}
			},
			//事件监听
			scrolltoupper() {
				console.log("滚动到顶部");
			},
			scrolltolower() {
				console.log("滚动到底部");
			},
			scroll(e : ScrollEvent) {
				console.log("scroll-top : " + e.detail.scrollTop + " scroll-left : " + e.detail.scrollLeft);
			},
			scrollend() {
				console.log("滚动停止");
雪洛's avatar
雪洛 已提交
179 180 181 182 183 184 185 186
			},
			onTouchMove() {
				console.log("TouchMove");
			},
      //自动化测试专用
      checkScrollHeight(): Boolean {
        var element = this.$refs["scrollViewY"]
        if(element != null) {
187
           var scrollHeight = (element as Element).scrollHeight
雪洛's avatar
雪洛 已提交
188 189 190 191 192 193 194 195 196 197 198
           console.log("checkScrollHeight"+scrollHeight)
           if(scrollHeight > 1900) {
             return true
           }
        }
        return false
      },
      //自动化测试专用
      checkScrollWidth(): Boolean {
        var element = this.$refs["scrollViewX"]
        if(element != null) {
199
           var scrollWidth = (element as Element).scrollWidth
雪洛's avatar
雪洛 已提交
200 201 202 203 204 205
           console.log("checkScrollWidth---"+scrollWidth)
           if(scrollWidth > 1900) {
             return true
           }
        }
        return false
shutao-dc's avatar
shutao-dc 已提交
206
      }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		}
	}
</script>

<style>
	.uni-margin-wrap {
		height: 400rpx;
		margin-left: 50rpx;
		margin-right: 50rpx;
	}

	.item {
		justify-content: center;
		align-items: center;
		height: 400rpx;
		width: 100%;
		background-color: azure;
雪洛's avatar
雪洛 已提交
224
		border-width: 2rpx;
xuty73419315's avatar
xuty73419315 已提交
225
    border-style: solid;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		border-color: chocolate;
	}

	.uni-text {
		color: black;
		font-size: 50px;
	}

	.uni-list {
		flex: 1;
		margin: 50rpx 50rpx 0rpx 50rpx;
	}

	.uni-option {
		height: 100rpx;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
	}

	.uni-slider {
W
wanganxp 已提交
248
		height: 60px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
249 250 251
		padding: 30rpx;
		justify-content: center;
	}
雪洛's avatar
雪洛 已提交
252
</style>