当前位置:   article > 正文

uniapp中scroll-view组件scroll-into-view的使用_uniapp scroll-into-view

uniapp scroll-into-view

  参考文档:scroll-view | uni-app官网

功能需求:让 scroll-view组件自动定位到当前月份,比如11月。

效果图:

 代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x enable-flex :scroll-into-view="scrollIndex" class="scrollCon">
  4. <view class="monthItem" :class="item.value == month ? 'active':''" v-for="(item, index) in monthList"
  5. :key="index" :id="'scroll'+index" @click="monthClick(index)">
  6. <view class="monthNum">{{item.name}}</view>
  7. </view>
  8. </scroll-view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. // 月份列表
  16. monthList: [{
  17. value: 1,
  18. name: '1月',
  19. }, {
  20. value: 2,
  21. name: '2月',
  22. }, {
  23. value: 3,
  24. name: '3月',
  25. }, {
  26. value: 4,
  27. name: '4月',
  28. }, {
  29. value: 5,
  30. name: '5月',
  31. }, {
  32. value: 6,
  33. name: '6月',
  34. }, {
  35. value: 7,
  36. name: '7月',
  37. }, {
  38. value: 8,
  39. name: '8月',
  40. }, {
  41. value: 9,
  42. name: '9月',
  43. }, {
  44. value: 10,
  45. name: '10月',
  46. }, {
  47. value: 11,
  48. name: '11月',
  49. }, {
  50. value: 12,
  51. name: '12月',
  52. }],
  53. //
  54. year: '',
  55. //
  56. month: '',
  57. // 定位当前月份
  58. scrollIndex: '',
  59. }
  60. },
  61. onLoad(option) {
  62. },
  63. onShow() {
  64. },
  65. onReady() {
  66. var _this = this;
  67. _this.getYearMonth();
  68. },
  69. methods: {
  70. // 获取当前年月
  71. getYearMonth() {
  72. var _this = this;
  73. var timestamp = Date.parse(new Date());
  74. var date = new Date(timestamp);
  75. //
  76. var Y = date.getFullYear();
  77. //
  78. var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  79. _this.year = Y;
  80. _this.month = M;
  81. _this.scrollIndex = 'scroll' + date.getMonth(); //定位到当前月份
  82. },
  83. // 点击月份
  84. monthClick(index) {
  85. var _this = this;
  86. _this.month = _this.monthList[index].value;
  87. _this.scrollIndex = 'scroll' + index;
  88. },
  89. }
  90. }
  91. </script>
  92. <style scoped>
  93. /* 月模式 */
  94. .scrollCon {
  95. white-space: nowrap;
  96. }
  97. .monthItem {
  98. display: inline-block;
  99. width: 64rpx;
  100. height: 64rpx;
  101. margin-right: 50rpx;
  102. }
  103. .monthItem:last-child {
  104. margin-right: 0;
  105. }
  106. .monthNum {
  107. width: 64rpx;
  108. height: 64rpx;
  109. line-height: 64rpx;
  110. text-align: center;
  111. color: #3D3D3D;
  112. font-size: 26rpx;
  113. background: transparent;
  114. border-radius: 50%;
  115. overflow: hidden;
  116. }
  117. .monthItem.active .monthNum {
  118. color: #fff;
  119. background: #3662EC;
  120. }
  121. </style>

scroll-view横向滚动的核心css

scroll-view的内层view元素需要

display: inline-block;

scroll-view的外层元素需要

white-space: nowrap;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/386993
推荐阅读
相关标签
  

闽ICP备14008679号