当前位置:   article > 正文

导航栏滑动自动居中(移动端)vue+原生js_vue 移动端导航栏

vue 移动端导航栏

vue加原生js实现移动端导航滑动自动居中,本人将它封装成了组件可供调用:

1.tabNav.vue组件:

  1. <template>
  2. <div
  3. class="scroll-nav"
  4. ref="scrollNav"
  5. :style="scrollWay == 'vertical' ? {'height': (navWH == 0 ? '100%' : navWH + 'px'),'background-color': colorBack,'color': colorText,'justify-content': scrollWH < navWH ? 'center' : ''} : {'width': (navWH == 0 ? '100%' : navWH + 'px'),'background-color': colorBack,'color': colorText,'justify-content': scrollWH < navWH ? 'center' : ''}"
  6. >
  7. <div
  8. class="scroll-box"
  9. ref="scrollBox"
  10. :style="scrollWay == 'vertical' ? {'transform': 'translate3d(0px,' + xyScroll + 'px,0px)','height': scrollWH + 'px','flex-direction': 'column'} : {'transform': 'translate3d(' + xyScroll + 'px,0px,0px)','width': scrollWH + 'px','flex-direction': 'row'}"
  11. >
  12. <div
  13. class="scroll-item"
  14. @click="chooseItem($event,index)"
  15. :class="index == activeIndex ? 'item-active' : ''"
  16. v-for="(item,index) in dataArr"
  17. :key="index"
  18. :style="scrollWay == 'vertical' ? {'margin': scrollMargin + 'px' + ' 0','padding': itemPadding,'color': index == activeIndex ? colorActiveText : '','background-color': index == activeIndex ? colorActiveBack : ''} : {'margin': '0 ' + scrollMargin + 'px','padding': itemPadding,'color': index == activeIndex ? colorActiveText : '','background-color': index == activeIndex ? colorActiveBack : ''}"
  19. >{
  20. {item[itemStr]}}</div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. name: "tabNav",
  27. data() {
  28. return {
  29. dataNum: [],
  30. deviceWH: 0,
  31. xyScroll: 0,
  32. oldxyScroll: 0,
  33. scrollWH: 0,
  34. scrollNav: {},
  35. scrollBox: {},
  36. scrollItemArr: [],
  37. startPointer: 0,
  38. movePointer: 0,
  39. endPointer: 0,
  40. useNavWH: 0,
  41. initinval: {},
  42. stepIntval: {},
  43. activeIndex: this.activeIndexParent
  44. };
  45. },
  46. props: {
  47. activeIndexParent: {
  48. type: Number,
  49. required: false,
  50. default: 0
  51. },
  52. colorText: {
  53. type: String,
  54. required: false,
  55. default: '#222'
  56. },
  57. colorBack: {
  58. type: String,
  59. required: false,
  60. default: 'whitesmoke'
  61. },
  62. colorActiveText: {
  63. type: String,
  64. required: false,
  65. default: '#fff'
  66. },
  67. colorActiveBack: {
  68. type: String,
  69. required: false,
  70. default: '#999'
  71. },
  72. dataArr: {
  73. type: Array,
  74. required: true
  75. },
  76. navWH: {
  77. type: Number,
  78. required: false,
  79. default: 0
  80. },
  81. scrollMargin: {
  82. type: Number,
  83. required: false,
  84. default: 0
  85. },
  86. scrollWay: {
  87. type: String,
  88. required: false,
  89. default: "horizontal"
  90. },
  91. itemPadding: {
  92. type: String,
  93. required: false,
  94. default: "5px 10px"
  95. },
  96. itemStr: {
  97. type: String,
  98. required: false,
  99. default: ""
  100. }
  101. },
  102. watch: {
  103. dataArr: function(newValue, oldValue) {
  104. if (newValue != oldValue) {
  105. t
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/76898
推荐阅读
相关标签
  

闽ICP备14008679号