经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue实现选项卡案例
来源:jb51  时间:2022/3/1 19:40:11  对本文有异议

本文实例为大家分享了vue实现选项卡案例的具体代码,供大家参考,具体内容如下

实现步骤

实现静态UI效果

  • 用传统的方式实现标签结构和样式

基于数据重构UI效果

  • 将静态的结构和样式重构为基于Vue模板语法的形式
  • 处理事件绑定和js控制逻辑

声明式编程

  • 模板的结构和最终显示的效果基本一致

我们先把每组数据作为对象存储在数组中

  1. list: [{
  2. ? ? ? id: 1,
  3. ? ? ? title: 'apple',
  4. ? ? ? path: 'images/苹果.jpg'
  5. ? ? ? }, {
  6. ? ? ? ? ?id: 2,
  7. ? ? ? ? ?title: 'orange',
  8. ? ? ? ? ?path: 'images/橘子.jpg'
  9. ? ? ? }, {
  10. ? ? ? ? ?id: 3,
  11. ? ? ? ? ?title: 'lemon',
  12. ? ? ? ? ?path: 'images/柠檬.jpg'
  13. ? ? ? }]

然后通过v-for对这个数组进行遍历,取到对应的title值

  1. <li :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>

对图片也进行遍历

  1. <div :key='item.id' v-for='(item,index) in list'>
  2. ? ? ? <img src="item.path">
  3. </div>

具体代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. ? ? <meta charset="UTF-8">
  6. ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. ? ? <title>Document</title>
  8. ? ? <style>
  9. ? ? ? ? .tab ul {
  10. ? ? ? ? ? ? overflow: hidden;
  11. ? ? ? ? ? ? padding: 0;
  12. ? ? ? ? ? ? margin: 0;
  13. ? ? ? ? }
  14. ? ? ? ??
  15. ? ? ? ? .tab ul li {
  16. ? ? ? ? ? ? box-sizing: border-box;
  17. ? ? ? ? ? ? padding: 0;
  18. ? ? ? ? ? ? float: left;
  19. ? ? ? ? ? ? width: 100px;
  20. ? ? ? ? ? ? height: 45px;
  21. ? ? ? ? ? ? line-height: 45px;
  22. ? ? ? ? ? ? list-style: none;
  23. ? ? ? ? ? ? text-align: center;
  24. ? ? ? ? ? ? border-top: 1px solid blue;
  25. ? ? ? ? ? ? border-right: 1px solid blue;
  26. ? ? ? ? ? ? border-bottom: 1px solid blue;
  27. ? ? ? ? ? ? cursor: pointer;
  28. ? ? ? ? }
  29. ? ? ? ??
  30. ? ? ? ? .tab ul li:first-child {
  31. ? ? ? ? ? ? border-left: 1px solid blue;
  32. ? ? ? ? }
  33. ? ? ? ??
  34. ? ? ? ? .tab ul li.active {
  35. ? ? ? ? ? ? background-color: orange;
  36. ? ? ? ? }
  37. ? ? ? ??
  38. ? ? ? ? .tab div {
  39. ? ? ? ? ? ? width: 500px;
  40. ? ? ? ? ? ? height: 300px;
  41. ? ? ? ? ? ? display: none;
  42. ? ? ? ? ? ? text-align: center;
  43. ? ? ? ? ? ? font-style: 30px;
  44. ? ? ? ? ? ? line-height: 300px;
  45. ? ? ? ? ? ? border: 1px solid blue;
  46. ? ? ? ? ? ? border-top: 0px;
  47. ? ? ? ? }
  48. ? ? ? ??
  49. ? ? ? ? .tab div.current {
  50. ? ? ? ? ? ? margin-top: 0px;
  51. ? ? ? ? ? ? width: 300px;
  52. ? ? ? ? ? ? height: 300px;
  53. ? ? ? ? ? ? display: block;
  54. ? ? ? ? }
  55. ? ? ? ??
  56. ? ? ? ? img {
  57. ? ? ? ? ? ? width: 300px;
  58. ? ? ? ? ? ? height: 300px;
  59. ? ? ? ? }
  60. ? ? </style>
  61. </head>
  62.  
  63. <body>
  64. ? ? <div id="app">
  65. ? ? ? ? <div class="tab">
  66. ? ? ? ? ? ? <ul>
  67. ? ? ? ? ? ? ? ? <li v-on:click='change(index)' :class='currentIndex==index?" active":""' :key='item.id' v-for='(item,index) in list'>{{item.title}}</li>
  68.  
  69. ? ? ? ? ? ? </ul>
  70. ? ? ? ? ? ? <div :class='currentIndex==index?"current":""' :key='item.id' v-for='(item,index) in list'>
  71. ? ? ? ? ? ? ? ? <img :src="item.path">
  72. ? ? ? ? ? ? </div>
  73. ? ? ? ? ? ? <div>
  74. ? ? ? ? ? ? ? ? <img src="images/柠檬.jpg">
  75. ? ? ? ? ? ? </div>
  76. ? ? ? ? </div>
  77. ? ? </div>
  78. ? ? <script type="text/javascript" src="js/vue.js"></script>
  79. ? ? <script type="text/javascript">
  80. ? ? ? ? var vm = new Vue({
  81. ? ? ? ? ? ? el: '#app',
  82. ? ? ? ? ? ? /*数据*/
  83. ? ? ? ? ? ? data: {
  84. ? ? ? ? ? ? ? ? currentIndex: 0,
  85. ? ? ? ? ? ? ? ? /*当前索引*/
  86. ? ? ? ? ? ? ? ? list: [{
  87. ? ? ? ? ? ? ? ? ? ? id: 1,
  88. ? ? ? ? ? ? ? ? ? ? title: 'apple',
  89. ? ? ? ? ? ? ? ? ? ? path: 'images/苹果.jpg'
  90. ? ? ? ? ? ? ? ? }, {
  91. ? ? ? ? ? ? ? ? ? ? id: 2,
  92. ? ? ? ? ? ? ? ? ? ? title: 'orange',
  93. ? ? ? ? ? ? ? ? ? ? path: 'images/橘子.jpg'
  94. ? ? ? ? ? ? ? ? }, {
  95. ? ? ? ? ? ? ? ? ? ? id: 3,
  96. ? ? ? ? ? ? ? ? ? ? title: 'lemon',
  97. ? ? ? ? ? ? ? ? ? ? path: 'images/柠檬.jpg'
  98. ? ? ? ? ? ? ? ? }]
  99. ? ? ? ? ? ? },
  100.  
  101. ? ? ? ? ? ? /*js控制逻辑*/
  102. ? ? ? ? ? ? methods: {
  103. ? ? ? ? ? ? ? ? // 在这里实现选项卡切换操作:本质就是操作类名
  104. ? ? ? ? ? ? ? ? // 如何操作类名?就是通过currentIndex
  105. ? ? ? ? ? ? ? ? change: function(index) {
  106. ? ? ? ? ? ? ? ? ? ? this.currentIndex = index
  107. ? ? ? ? ? ? ? ? }
  108. ? ? ? ? ? ? }
  109. ? ? ? ? })
  110. ? ? </script>
  111. </body>
  112.  
  113. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号