定制开发小程序uniapp scroll-view基础用法

前言

        定制开发小程序在日常开发的过程中经定制开发小程序常会有局部滚动的需求,而scroll-view定制开发小程序组件正好可以满足这一需求。需注意在webview定制开发小程序渲染的页面中,区域滚动的性能不及页面滚动。

纵向滚动

        将组件中的属性scroll-y设定为true开启纵向滚动功能,给scroll-view设置一个高度,当内容高度大于scroll-view高度时即可开启滚动功能(内容高度小于scroll-view高度时无法体现滚动功能)

实现代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-y="true" style="height: 700rpx;">
  4. <view v-for="(item,index) in 3" style="height: 500rpx;" :style="{ backgroundColor: colorList[index]}">
  5. {{index}}
  6. </view>
  7. </scroll-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. colorList:["blue","red","yellow"]
  15. }
  16. },
  17. methods: {
  18. }
  19. }
  20. </script>
  21. <style>
  22. </style>

效果图:

横向滚动

        将scroll-view组件中的属性scroll-x设定为true开启横向滚动功能,给scroll-view设置一个宽度,当内容宽度大于scroll-view宽度时即可开启滚动功能(内容宽度小于scroll-view宽度时无法体现滚动功能)

        注意:scroll-view本身的display:flex不生效、如果想实现display:flex功能,则可以给scroll-view加上white-space: nowrap,给内容容器加上display:inline-block

实现代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;">
  4. <view v-for="(item,index) in 3" style="height: 500rpx;width: 100%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
  5. {{index}}
  6. </view>
  7. </scroll-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. colorList:["blue","red","yellow"]
  15. }
  16. },
  17. methods: {
  18. }
  19. }
  20. </script>
  21. <style>
  22. </style>

效果图:

 锚点定位

        当我们已进入页面就需要滚动到某一个元素的时候,锚点定位就可以很好的帮助我们定位并滚动到指定位置

        将scroll-with-animation设定为true开启动画效果、对scroll-into-view进行动态绑定

        注意:scroll-into-view绑定的值得是字符串,使用其他类型则会报错

实现代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" scroll-with-animation="true" :scroll-into-view="'scroll'+scrollId">
  4. <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}" :id="'scroll'+index">
  5. {{index}}
  6. </view>
  7. </scroll-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. colorList:["blue","red","yellow"],
  15. scrollId:1
  16. }
  17. },
  18. methods: {
  19. }
  20. }
  21. </script>
  22. <style>
  23. </style>

效果图:

触底事件       

        在滑动的数据需要懒加载的时候,我们就需要通过用户滑动到底部时触发懒加载方法,通过绑定scrolltolower方法即可实现纵/横触底时触发懒加载方法

实现代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" @scrolltolower="onReachScollBottom">
  4. <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
  5. {{index}}
  6. </view>
  7. </scroll-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. colorList:["blue","red","yellow"],
  15. }
  16. },
  17. methods: {
  18. onReachScollBottom(){
  19. uni.showToast({
  20. title:"触发了触底事件",
  21. duration:1500,
  22. icon:"none"
  23. })
  24. }
  25. }
  26. }
  27. </script>
  28. <style>
  29. </style>

 效果图:

 下拉刷新事件

        scroll-view组件也可以满足我们下拉刷新的需求、首先通过设置refresher-enabled为true开启下拉加载、动态绑定refresher-triggered对下拉加载的状态进行控制、绑定refresherrefresh触发下拉刷新事件

实现代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x="true" style="height: 500rpx;white-space: nowrap;" refresher-enabled="true" :refresher-triggered="refresh" @refresherrefresh="onRefresh">
  4. <view v-for="(item,index) in 3" style="height: 500rpx;width: 80%;display: inline-block;" :style="{ backgroundColor: colorList[index]}">
  5. {{index}}
  6. </view>
  7. </scroll-view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. colorList:["blue","red","yellow"],
  15. refresh: false
  16. }
  17. },
  18. methods: {
  19. onRefresh() {
  20. this.refresh= true;
  21. uni.showToast({
  22. title:"触发了下拉刷新",
  23. duration:1500,
  24. icon:"none"
  25. })
  26. // 这里不能直接让refresh直接为false,否则可能会发生下拉加载无法复位的情况
  27. setTimeout(() => {
  28. this.refresh = false;
  29. }, 500)
  30. }
  31. }
  32. }
  33. </script>
  34. <style>
  35. </style>

效果图:

 

 总结

        以上就是我整理的scroll-view的基础用法、想要了解更多的用法可以前往部分进行了解

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发