当前位置:首页 > VUE

vue实现新闻导航栏

2026-01-21 20:49:08VUE

Vue实现新闻导航栏

使用Vue实现新闻导航栏可以通过组件化开发方式,结合路由和动态数据绑定完成。以下是具体实现方法:

基础结构搭建

创建Vue组件NewsNav.vue,包含导航栏HTML结构和基本样式:

<template>
  <div class="news-nav">
    <ul>
      <li v-for="(item, index) in navItems" :key="index">
        <router-link :to="item.path">{{ item.title }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navItems: [
        { title: '首页', path: '/' },
        { title: '国内', path: '/domestic' },
        { title: '国际', path: '/international' },
        { title: '科技', path: '/technology' },
        { title: '娱乐', path: '/entertainment' }
      ]
    }
  }
}
</script>

<style scoped>
.news-nav {
  background: #333;
  color: white;
}
.news-nav ul {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}
.news-nav li {
  padding: 15px 20px;
}
.news-nav a {
  color: white;
  text-decoration: none;
}
.news-nav a:hover {
  color: #42b983;
}
</style>

动态高亮当前路由

添加路由激活状态样式,修改router-link为:

<router-link 
  :to="item.path" 
  active-class="active"
  exact-active-class="exact-active"
>
  {{ item.title }}
</router-link>

在样式中添加:

.news-nav .active {
  color: #42b983;
  border-bottom: 2px solid #42b983;
}

响应式设计

vue实现新闻导航栏

添加媒体查询实现移动端适配:

@media (max-width: 768px) {
  .news-nav ul {
    flex-direction: column;
  }
  .news-nav li {
    padding: 10px;
    text-align: center;
  }
}

数据动态加载

从API获取导航数据:

export default {
  data() {
    return {
      navItems: []
    }
  },
  async created() {
    try {
      const response = await fetch('/api/nav-items');
      this.navItems = await response.json();
    } catch (error) {
      console.error('加载导航数据失败:', error);
    }
  }
}

添加交互效果

vue实现新闻导航栏

实现下拉菜单功能:

<template>
  <div class="news-nav">
    <ul>
      <li v-for="(item, index) in navItems" :key="index" @mouseenter="showSubNav(index)" @mouseleave="hideSubNav(index)">
        <router-link :to="item.path">{{ item.title }}</router-link>
        <ul v-if="item.subItems && isSubNavVisible[index]" class="sub-nav">
          <li v-for="(subItem, subIndex) in item.subItems" :key="subIndex">
            <router-link :to="subItem.path">{{ subItem.title }}</router-link>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isSubNavVisible: []
    }
  },
  methods: {
    showSubNav(index) {
      this.$set(this.isSubNavVisible, index, true);
    },
    hideSubNav(index) {
      this.$set(this.isSubNavVisible, index, false);
    }
  }
}
</script>

<style>
.sub-nav {
  position: absolute;
  background: #444;
  padding: 0;
  margin-top: 15px;
}
.sub-nav li {
  padding: 10px 20px;
  white-space: nowrap;
}
</style>

与Vuex集成

使用Vuex管理导航状态:

// store/modules/nav.js
export default {
  state: {
    navItems: []
  },
  mutations: {
    SET_NAV_ITEMS(state, items) {
      state.navItems = items;
    }
  },
  actions: {
    async fetchNavItems({ commit }) {
      const response = await fetch('/api/nav-items');
      commit('SET_NAV_ITEMS', await response.json());
    }
  }
}

在组件中使用:

import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState('nav', ['navItems'])
  },
  created() {
    this.$store.dispatch('nav/fetchNavItems');
  }
}

标签: 新闻vue
分享给朋友:

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…