tab切换,左右加箭头,点击箭头实现tab切换

和正常tab切换一样原理,点击箭头多了步计算

<template>
  <div>
    <div class="tab-container">
      <p>{{projectName}}</p>
      <div class="banner">
        <div v-for="(tab, index) in tabs" :key="index" :class="{'active': activeTab === index,'line':true}">
        </div>
      </div>
      <div class="jt">
        <i class="el-icon-arrow-left" @click="previousTab()"></i>
        <i class="el-icon-arrow-right" @click="nextTab()"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tabs: ['学不会1', '学不会2', '学不会3'],
      activeTab: 0, // 索引
      projectName: '' // 名称
    };
  },
  mounted () {
    this.projectName = this.tabs[0]
  },
  methods: {
    // 上一步
    previousTab () {
      this.activeTab = (this.activeTab - 1 + this.tabs.length) % this.tabs.length;
      this.projectName = this.tabs[this.activeTab]
    },
    // 下一步
    nextTab () {
      this.activeTab = (this.activeTab + 1) % this.tabs.length;
      this.projectName = this.tabs[this.activeTab]
    }
  }
};
</script>

<style lang="less" scoped>
.tab-container {
  height: 300px;
  display: flex;
  position: relative;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: #999;
  p {
    color: skyblue;
  }
  .banner {
    display: flex;
    .line {
      width: 60px;
      height: 10px;
      background: #ccc;
      margin-left: 10px;
      margin-top: 16px;
    }
  }
  .jt {
    position: absolute;
    top: 50%;
    transform: translate(0, -50%); // 移动元素 x轴,y轴
    display: flex;
    width: 100%;
    justify-content: space-between;
    padding: 0 10px;
    cursor: pointer;
    font-size: 20px;
  }
}

.active {
  background: skyblue !important;
  color: skyblue;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供 jQuery 实现同时展示多个 tab 标签并且支持左右箭头滚动的代码。 首先,您需要一个包含 tab 标签的容器元素,如下所示: ```html <div class="tab-container"> <ul class="tabs"> <li class="active">Tab 1</li> <li>Tab 2</li> <li>Tab 3</li> <li>Tab 4</li> <li>Tab 5</li> <li>Tab 6</li> <li>Tab 7</li> <li>Tab 8</li> <li>Tab 9</li> <li>Tab 10</li> </ul> </div> ``` 接着,您可以使用以下 CSS 样式来设置容器元素及其子元素的样式: ```css .tab-container { position: relative; overflow: hidden; width: 500px; /* 设置容器宽度 */ } .tabs { display: flex; padding: 0; margin: 0; list-style: none; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; /* 在移动设备上启用平滑滚动效果 */ } .tabs li { flex-shrink: 0; margin-right: 10px; padding: 10px; background-color: #eee; cursor: pointer; } .tabs li.active { background-color: #ccc; } ``` 最后,您可以使用以下 jQuery 代码来实现左右箭头滚动功能: ```javascript $(document).ready(function() { var tabContainer = $('.tab-container'); var tabs = $('.tabs'); var tabWidth = tabs.outerWidth(); // 获取每个 tab 元素的宽度 var containerWidth = tabContainer.outerWidth(); // 获取容器元素的宽度 var maxScroll = tabs.outerWidth() - containerWidth; // 计算最大滚动距离 var scrollStep = tabWidth * 2; // 设置每次滚动的距离 // 左箭头点击事件 $('.prev').click(function() { var scrollLeft = tabs.scrollLeft(); var newScrollLeft = Math.max(scrollLeft - scrollStep, 0); tabs.animate({ scrollLeft: newScrollLeft }, 500); }); // 右箭头点击事件 $('.next').click(function() { var scrollLeft = tabs.scrollLeft(); var newScrollLeft = Math.min(scrollLeft + scrollStep, maxScroll); tabs.animate({ scrollLeft: newScrollLeft }, 500); }); }); ``` 上述代码使用 jQuery 的 `scrollLeft()` 方法来获取和设置滚动距离,使用 `animate()` 方法来平滑滚动到指定位置。同时,还需要计算出容器元素和每个 tab 元素的宽度以及最大滚动距离和每次滚动的距离。 注意,您需要在 HTML 中添左右箭头的元素,并为它们添对应的类名(如 `.prev` 和 `.next`),并在 CSS 中设置它们的样式。 希望这个代码对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学不会•

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值