效果如下

01.gif

View部分

  1. <div id="app" class="tab-navbar">
  2. <nav>
  3. <a v-for= "(v, i) in list" :key= "v" :class="{ active: index === i }" @click="select(i)"> 标题{{i + 1}} </a>
  4. </nav>
  5. <div class="items">
  6. <div class="ul" :style="`--tab-count: ${list.length};--tab-index: ${index}`">
  7. <div class="li" v-for="(v, i) in list" :key="v" :style="`--bg-color: ${v}`"> 内容{{i + 1}} </div>
  8. </div>
  9. </div>
  10. </div>

JS部分

  1. var app = new Vue({
  2. el: '#app',
  3. data: function(){
  4. return {
  5. index: 0,
  6. list: [ "#f66", "#09f", "#3c9"]
  7. };
  8. },
  9. methods: {
  10. select(i) {
  11. this.index = i;
  12. }
  13. }
  14. });

css 部分

  1. .tab-navbar {
  2. display: flex;
  3. overflow: hidden;
  4. flex-direction: column-reverse;
  5. border-radius: 10px;
  6. width: 300px;
  7. height: 400px;
  8. }
  9. nav {
  10. width: 300px;
  11. display: flex;
  12. height: 40px;
  13. background-color: #f0f0f0;
  14. line-height: 40px;
  15. text-align: center;
  16. }
  17. a {
  18. flex: 1;
  19. cursor: pointer;
  20. transition: all 300ms;
  21. }
  22. .active {
  23. background-color: #66f;
  24. font-weight: bold;
  25. color: #fff;
  26. }
  27. .items {
  28. flex: 1;
  29. }
  30. .ul {
  31. --tab-index: 0;
  32. --tab-width: calc(var(--tab-count) * 100%);
  33. --tab-move: calc(var(--tab-index) / var(--tab-count) * -100%);
  34. display: flex;
  35. flex-wrap: nowrap;
  36. width: var(--tab-width);
  37. height: 100%;
  38. transform: translate3d(var(--tab-move), 0, 0);
  39. transition: all 300ms;
  40. }
  41. .li {
  42. display: flex;
  43. justify-content: center;
  44. align-items: center;
  45. flex: 1;
  46. background-color: var(--bg-color);
  47. font-weight: bold;
  48. font-size: 20px;
  49. color: #fff;
  50. }