vue 点击左右滑动

<template>
  <div class="nav-bottom">
    <span class="prev">
      <i class="el-icon-arrow-left" @click="prenHandle"></i>
    </span>
    <div class="content">
      <ul :style="{ transform: 'translateX(' + left + 'px)' }">
        <li v-for="(item, index) in 20" :key="index">
          <p>{{ item }}</p>
        </li>
      </ul>
    </div>
    <span class="next">
      <i class="el-icon-arrow-right" @click="nextHandle"></i>
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 0,
      left: 0,
      index: 1
    };
  },
  watch: {},
  methods: {
    prenHandle() {
      if (this.index == 0) return;
      this.index++;
      this.left = this.index * 134;
    },
    nextHandle() {
      if (this.index == -15) return;
      this.index--;
      this.left = this.index * 134;
    }
  },
  mounted() {
    this.width = this.width * 20;
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.nav-bottom {
  width: 750px;
  height: 110px;
  display: flex;
  justify-content: space-around;
  position: relative;
  text-align: center;
  background: rgba(2, 27, 52, 0.7);
  border-radius: 10px;
  .content {
    width: 660px;
    height: 70px;
    margin-top: 20px;
    overflow: hidden;
    ul {
      display: flex;
      width: 320px;
      height: 70px;
      li {
        width: 124px;
        height: 70px;
        line-height: 70px;
        display: flex;
        margin-right: 10px;
        flex-direction: column;
        flex-shrink: 0;
        text-align: center;
        color: #fff;
        background: #090;
      }
    }
  }
  span {
    width: 45px;
    height: 110px;
    line-height: 110px;
    font-size: 28px;
    color: #0293d9;
    i {
      cursor: pointer;
    }
  }
  .prev {
    float: left;
  }
  .next {
    float: right;
  }
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现点横向导航栏实现导航左右滑动,在Vue中可以使用如下步骤: 1. 在template中定义横向导航栏和导航内容区域: ```html <template> <div class="nav-container"> <div class="nav-bar"> <ul> <li v-for="(nav, index) in navList" :key="index" @click="handleNavClick(index)" :class="{ active: activeIndex === index }">{{nav}}</li> </ul> </div> <div class="nav-content" ref="navContent"> <div class="nav-item" v-for="(nav, index) in navList" :key="index">{{nav}}</div> </div> </div> </template> ``` 2. 在script中定义data和方法: ```javascript <script> export default { data() { return { navList: ['Nav1', 'Nav2', 'Nav3', 'Nav4'], // 导航栏列表 activeIndex: 0 // 当前激活的导航栏索引 } }, methods: { // 点导航栏 handleNavClick(index) { this.activeIndex = index // 更新当前激活的导航栏索引 const navContent = this.$refs.navContent // 获取导航内容区域 const navItem = navContent.querySelectorAll('.nav-item')[index] // 获取对应的导航内容项 navContent.scrollLeft = navItem.offsetLeft - navContent.offsetLeft // 滚动到对应的导航内容项位置 } } } </script> ``` 3. 在style中定义样式: ```css <style scoped> .nav-container { display: flex; flex-direction: column; height: 100%; } .nav-bar { height: 40px; line-height: 40px; background-color: #f5f5f5; overflow-x: auto; white-space: nowrap; } .nav-bar ul { margin: 0; padding: 0; } .nav-bar li { display: inline-block; padding: 0 20px; font-size: 14px; cursor: pointer; } .nav-bar li.active { color: #1890ff; border-bottom: 2px solid #1890ff; } .nav-content { flex: 1; overflow-x: auto; white-space: nowrap; } .nav-item { display: inline-block; width: 100%; height: 100%; padding: 20px; box-sizing: border-box; } </style> ``` 这样就可以实现点横向导航栏实现导航左右滑动了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值