scroll-view-props.uvue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <!-- <view class="page-scroll-view"> -->
  3. <page-head title="非下拉刷新的scroll-view属性示例"></page-head>
  4. <!-- 暂时分成两个方向不同的滚动视图,原因为:scroll-view组件不支持动态改变direction。 -->
  5. <scroll-view v-if="scrollX" direction="horizontal" :scroll-top="scrollTop" :scroll-left="scrollLeft"
  6. :upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
  7. :enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation" style="flex-direction: row;"
  8. class="uni-margin-wrap" :show-scrollbar="showScrollbar" :bounces="bounces" @scrolltoupper="scrolltoupper"
  9. @scrolltolower="scrolltolower" @scroll="scroll" @scrollend="scrollend" ref="scrollViewX" id="scrollViewX">
  10. <!-- h-margin-right末尾元素添加margin-right 测试bug #3866-->
  11. <view class="item" :id="'horizontal_'+item.id" v-for="(item,index) in items"
  12. :class="index==9 ? 'h-margin-right': ''">
  13. <text class="uni-text">{{item.label}}</text>
  14. </view>
  15. </scroll-view>
  16. <scroll-view v-else direction="vertical" :scroll-top="scrollTop" :scroll-left="scrollLeft"
  17. :upper-threshold="upperThreshold" :lower-threshold="lowerThreshold" :scroll-into-view="scrollIntoView"
  18. :enable-back-to-top="enableBackToTop" :scroll-with-animation="scrollWithAnimation" :show-scrollbar="showScrollbar"
  19. :bounces="bounces" @scrolltoupper="scrolltoupper" @touchmove="onTouchMove" @scrolltolower="scrolltolower"
  20. @scroll="scroll" @scrollend="scrollend" ref="scrollViewY" id="scrollViewY" class="uni-margin-wrap">
  21. <!-- v-margin-bottom末尾元素添加margin-bottom 测试bug #3866-->
  22. <view class="item" :id="item.id" v-for="(item, index) in items" :class="index==9 ? 'v-margin-bottom': ''">
  23. <text class="uni-text">{{item.label}}</text>
  24. </view>
  25. </scroll-view>
  26. <scroll-view class="uni-list" :showScrollbar="true" :scroll-y="true">
  27. <view class="uni-list-cell uni-list-cell-padding">
  28. <text>点击状态栏回到顶部(仅iOS)</text>
  29. <switch :checked="enableBackToTop" @change="enableBackToTop=!enableBackToTop"></switch>
  30. </view>
  31. <view class="uni-list-cell uni-list-cell-padding">
  32. <text>是否显示滚动条</text>
  33. <switch :checked="showScrollbar" @change="showScrollbar=!showScrollbar"></switch>
  34. </view>
  35. <view class="uni-list-cell uni-list-cell-padding">
  36. <text>是否有反弹效果</text>
  37. <switch :checked="bounces" @change="bounces=!bounces"></switch>
  38. </view>
  39. <view class="uni-list-cell uni-list-cell-padding">
  40. <text>是否开启滚动时使用动画过渡</text>
  41. <switch :checked="scrollWithAnimation" @change="scrollWithAnimation=!scrollWithAnimation"></switch>
  42. </view>
  43. <view class="uni-list-cell uni-list-cell-padding">
  44. <text>是否横向滚动</text>
  45. <switch :checked="scrollX" @change="changeDirectionX"></switch>
  46. </view>
  47. <view class="uni-list-cell uni-list-cell-padding">
  48. <text>是否竖向滚动</text>
  49. <switch :checked="scrollY" @change="changeDirectionY"></switch>
  50. </view>
  51. <view class="uni-slider uni-list-cell-padding">
  52. <text>拖动设置scroll-top</text>
  53. </view>
  54. <view class="uni-slider uni-list-cell-padding">
  55. <slider :max="1000" :min="0" :step="10" :value="scrollTop" :show-value="true" @change="handleChangeScrollTop" />
  56. </view>
  57. <view class="uni-slider uni-list-cell-padding">
  58. <text>拖动设置scroll-left</text>
  59. </view>
  60. <view class="uni-slider uni-list-cell-padding">
  61. <slider :max="1000" :min="0" :step="10" :value="scrollLeft" :show-value="true"
  62. @change="handleChangeScrollLeft" />
  63. </view>
  64. <view class="uni-list-cell uni-list-cell-padding">
  65. <text>设置触发scrolltoupper的距离</text>
  66. <input class="uni-list-cell-input" type="number" :value="upperThreshold" @input="handleUpperThresholdInput" />
  67. </view>
  68. <view class="uni-list-cell uni-list-cell-padding">
  69. <text>设置触发scrolltolower的距离</text>
  70. <input class="uni-list-cell-input" type="number" :value="lowerThreshold" @input="handleLowerThresholdInput" />
  71. </view>
  72. <view class="uni-list-cell uni-list-cell-padding">
  73. <button type="primary" class="button" @click="handleScrollIntoView">
  74. 滚动到id为`item3`的子视图
  75. </button>
  76. </view>
  77. <view class="uni-list-cell uni-list-cell-padding">
  78. </view>
  79. </scroll-view>
  80. <!-- </view> -->
  81. </template>
  82. <script>
  83. type Item = {
  84. id : string,
  85. label : string,
  86. }
  87. export default {
  88. data() {
  89. return {
  90. items: [] as Item[],
  91. scrollX: false,
  92. scrollY: true,
  93. bounces: false,
  94. scrollTop: 0,
  95. scrollLeft: 0,
  96. scrollChangeTop: 0,
  97. scrollIntoView: "",
  98. enableBackToTop: false,
  99. scrollWithAnimation: false,
  100. showScrollbar: true,
  101. upperThreshold: 50,
  102. lowerThreshold: 50,
  103. }
  104. },
  105. onLoad() {
  106. for (let i = 0; i < 10; i++) {
  107. const item = {
  108. id: "item" + i,
  109. label: "item" + i,
  110. } as Item;
  111. this.items.push(item);
  112. }
  113. },
  114. methods: {
  115. handleChangeScrollLeft(e : UniSliderChangeEvent) {
  116. this.scrollLeft = e.detail.value;
  117. },
  118. handleChangeScrollTop(e : UniSliderChangeEvent) {
  119. this.scrollTop = e.detail.value;
  120. },
  121. changeDirectionX() {
  122. this.scrollX = !this.scrollX;
  123. if (this.scrollX) {
  124. this.scrollY = false
  125. }
  126. this.scrollTop = 0;
  127. this.scrollLeft = 0;
  128. },
  129. changeDirectionY() {
  130. this.scrollY = !this.scrollY;
  131. if (this.scrollY) {
  132. this.scrollX = false
  133. }
  134. this.scrollTop = 0;
  135. this.scrollLeft = 0;
  136. },
  137. handleScrollIntoView() {
  138. if (this.scrollX) {
  139. this.scrollIntoView = "horizontal_item3";
  140. } else {
  141. this.scrollIntoView = "item3";
  142. }
  143. //重置状态,由于响应式的原因,设置的值与当前值相同,会导致再次设置无效果。
  144. setTimeout(() => {
  145. this.scrollIntoView = "";
  146. }, 0);
  147. },
  148. handleUpperThresholdInput(e : InputEvent) {
  149. const value = e.detail.value;
  150. if (value == "") {
  151. this.upperThreshold = 50;
  152. } else {
  153. this.upperThreshold = parseInt(e.detail.value);
  154. }
  155. },
  156. handleLowerThresholdInput(e : InputEvent) {
  157. const value = e.detail.value;
  158. if (value == "") {
  159. this.lowerThreshold = 50;
  160. } else {
  161. this.lowerThreshold = parseInt(e.detail.value);
  162. }
  163. },
  164. //事件监听
  165. scrolltoupper() {
  166. console.log("滚动到顶部");
  167. },
  168. scrolltolower() {
  169. console.log("滚动到底部");
  170. },
  171. scroll(e : ScrollEvent) {
  172. this.scrollChangeTop = e.detail.scrollTop
  173. console.log("scroll-top : " + e.detail.scrollTop + " scroll-left : " + e.detail.scrollLeft);
  174. },
  175. scrollend() {
  176. console.log("滚动停止");
  177. },
  178. onTouchMove() {
  179. console.log("TouchMove");
  180. },
  181. //自动化测试专用
  182. checkScrollHeight() : Boolean {
  183. var element = this.$refs["scrollViewY"]
  184. if (element != null) {
  185. var scrollHeight = (element as UniElement).scrollHeight
  186. console.log("checkScrollHeight" + scrollHeight)
  187. if (scrollHeight > 1900) {
  188. return true
  189. }
  190. }
  191. return false
  192. },
  193. //自动化测试专用
  194. checkScrollWidth() : Boolean {
  195. var element = this.$refs["scrollViewX"]
  196. if (element != null) {
  197. var scrollWidth = (element as UniElement).scrollWidth
  198. console.log("checkScrollWidth---" + scrollWidth)
  199. if (scrollWidth > 1900) {
  200. return true
  201. }
  202. }
  203. return false
  204. }
  205. }
  206. }
  207. </script>
  208. <style>
  209. .uni-margin-wrap {
  210. height: 250px;
  211. margin: 0 25px 25px 25px;
  212. }
  213. .item {
  214. justify-content: center;
  215. align-items: center;
  216. height: 250px;
  217. width: 100%;
  218. background-color: azure;
  219. border-width: 1px;
  220. border-style: solid;
  221. border-color: chocolate;
  222. }
  223. .uni-list {
  224. flex: 1;
  225. }
  226. .uni-text {
  227. color: black;
  228. font-size: 50px;
  229. }
  230. .uni-slider {
  231. justify-content: center;
  232. }
  233. .uni-list-cell-input {
  234. width: 50px;
  235. border: 1px solid #ccc;
  236. text-align: center;
  237. }
  238. .button {
  239. flex: 1;
  240. }
  241. /*自动化测试专用*/
  242. .v-margin-bottom {
  243. margin-bottom: 50px;
  244. }
  245. /*自动化测试专用*/
  246. .h-margin-right {
  247. margin-right: 50px;
  248. }
  249. </style>