当前位置:首页 > VUE

vue实现天猫导航

2026-01-22 13:06:48VUE

Vue 实现天猫导航

项目结构

使用 Vue CLI 创建项目,安装必要依赖如 vue-routeraxios。目录结构建议如下:

src/
├── components/
│   ├── Header.vue          # 顶部导航
│   ├── MainNav.vue         # 主分类导航
│   ├── SubNav.vue          # 二级分类导航
│   └── SearchBar.vue       # 搜索框组件
├── views/
│   ├── Home.vue            # 首页
│   └── Category.vue        # 分类页
├── router/
│   └── index.js            # 路由配置
└── App.vue                 # 根组件

顶部导航实现

顶部导航包含用户登录、店铺入口等信息,使用 Flex 布局实现:

<template>
  <div class="top-header">
    <div class="left">
      <span>天猫首页</span>
      <span>喵,请登录</span>
      <span>免费注册</span>
    </div>
    <div class="right">
      <span>我的订单</span>
      <span>购物车</span>
    </div>
  </div>
</template>

<style scoped>
.top-header {
  display: flex;
  justify-content: space-between;
  background: #f2f2f2;
  padding: 5px 20px;
}
</style>

主分类导航

主分类采用横向滚动式设计,数据通过 v-for 动态渲染:

vue实现天猫导航

<template>
  <div class="main-nav">
    <div v-for="item in navList" :key="item.id" class="nav-item">
      <img :src="item.icon" />
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navList: [
        { id: 1, name: '女装', icon: 'icon-path' },
        { id: 2, name: '男装', icon: 'icon-path' }
      ]
    }
  }
}
</script>

<style scoped>
.main-nav {
  display: flex;
  overflow-x: auto;
  white-space: nowrap;
}
.nav-item {
  padding: 0 15px;
  text-align: center;
}
</style>

搜索框组件

集成搜索建议功能,使用 v-model 绑定输入值:

<template>
  <div class="search-box">
    <input 
      v-model="keyword" 
      @keyup.enter="search" 
      placeholder="搜索商品" 
    />
    <button @click="search">搜索</button>
    <ul v-if="suggestions.length" class="suggestions">
      <li v-for="(item, index) in suggestions" :key="index">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: '',
      suggestions: []
    }
  },
  methods: {
    async search() {
      if (this.keyword.trim()) {
        const res = await axios.get('/api/search?s=' + this.keyword)
        this.suggestions = res.data.suggestions
      }
    }
  }
}
</script>

路由配置

配置分类页面的动态路由:

vue实现天猫导航

// router/index.js
{
  path: '/category/:id',
  name: 'Category',
  component: () => import('../views/Category.vue')
}

交互优化

  1. 主分类导航添加鼠标悬停效果:

    .nav-item:hover {
    color: #FF0036;
    transform: translateY(-2px);
    }
  2. 搜索框防抖处理:

    import _ from 'lodash'
    export default {
    watch: {
     keyword: _.debounce(function(newVal) {
       this.fetchSuggestions(newVal)
     }, 300)
    }
    }

数据获取

通过 Axios 获取导航数据:

async created() {
  try {
    const res = await axios.get('/api/nav')
    this.navList = res.data
  } catch (error) {
    console.error(error)
  }
}

注意事项

  1. 移动端需添加 touch 事件支持
  2. 图标建议使用 SVG 雪碧图或 iconfont
  3. 二级分类建议使用 CSS 过渡动画
  4. 路由切换时保持导航状态可用 Vuex 管理

标签: vue
分享给朋友:

相关文章

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…