Vue开发前端导航栏设计【常见问题+示例代码】

书接上回

上一次在文章中,我们讲解了Vue前端的容器搭建,在本文中,我们将继续细化设计,逐步完成一个较为完善的导航栏。

组件引用

组件引用是为了在较大规模开发时避免代码塞在一个文件里边,增加可读性。

在引用的主文件里的 js部分import,然后直接在 tmplate部分调用即可,代码如下

<template>
  <div>
    <navigator></navigator>
  </div>
</template>

<style scoped>

</style>

<script setup>
// 导入组件
import navigator from "@/page/navigator/navigator.vue"
</script>

在需要被调用的组件中,记得一定要有<script setup>部分

<template>
  <div class="navigator">

  </div>
</template>

<style scoped>

</style>

<script setup>

</script>

 调试浏览器,发现调用成功

导航栏框架搭建

这里提供一个简单的导航栏框架设计,本文将详细讲解微调过程。

先展示初版效果

提几个要点:

  1. 导航栏可以做个下端阴影,这样就算全白也会有区分度,看起来比较舒服
  2. 搜索框的border可以粗一点弄成圆的,用的element-plus组件所以需要使用::deep贯穿

<template>
  <div class="navigator">
    <div class="logo">
      <img src="../../asset/board/logo.png" style="height:400%;margin-top: -21%;margin-left: 15%;user-select: none;">
    </div>

    <div class="search">
      <el-input
          v-model="keyword"
          placeholder="请输入搜索内容"
          suffix-icon="Search"
          class="round-input"
          style="width:100%; height:60%; margin-top:3%">
      </el-input>
    </div>

    <div class="selectionSet">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" style="margin-top: 3%;margin-left: 10%">
        <el-tab-pane label="首页" name="first">    </el-tab-pane>
        <el-tab-pane label="资料库" name="second">  </el-tab-pane>
        <el-tab-pane label="讨论区" name="third">   </el-tab-pane>
        <el-tab-pane label="收藏夹" name="fourth">  </el-tab-pane>
      </el-tabs>

      <img src="../../asset/board/碳治郎1.png"
           style="height:90%;margin-left: 10%;margin-top: 0.5%">
    </div>
  </div>
</template>

<style scoped>
.navigator {
  display: flex;
  align-items: center;
  /* background-color: #d5e4f4;*/
  background-color: #ffffff;
  height: 9%;
  /* 添加底部阴影效果 */
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.logo {
  width: 30%;
  height: 100%;
  display: flex;
  padding-left: 3%;
}

.search {
  display: flex;
  width: 30%;
  height: 100%;
}

:deep(.round-input .el-input__wrapper) {
  border-radius: 20px !important;
  background-color: #FFFFFF;
  box-shadow: 0 0 0 1.5px rgb(210, 203, 241) !important;
}

.selectionSet {
  display: flex;
  width: 40%;
  height: 100%;
}

:deep(.el-tabs__item) {
  color: #000000;
  font-size: 25px;
  transition: color 0.3s ease;
}

::v-deep .el-tabs__active-bar {
  background-color: #064fe1 !important;
}

:deep(.el-tabs__nav-wrap::after) {
  position: static !important;
}
</style>

<script setup>
import { ref } from 'vue';

const keyword = ref('');
const activeName = ref('first');

const handleClick = (tab) => {
  console.log(' 当前选中标签:', tab.name);
};
</script>

图片文字取消点击光标闪烁

比如点击右上tab,发现光标是闪烁的,看的很不舒服,这里可以内联为元素设置一下

之后就不可选中了,图像貌似不可以这样解决,可以给图像设计一个点击跳转或点击放大,就不会出现光标闪烁了

user-select: none

输入框偏斜

如题所示,解决方案为在外边包的div上设置user-select:none ;这个偏斜实际上是选中了这个div

输入框悬停和聚焦流光动画

以下展示为全代码,方便CV

<template>
  <div class="navigator">
    <div class="logo">
      <img src="../../asset/board/logo.png" style="height:100%;margin-left: 20%;">
    </div>

    <div class="search">
      <el-input
          v-model="keyword"
          placeholder="                                     biuuuu~ 灵 光 乍 现"
          suffix-icon="Search"
          :class="{ 'round-input':true,'animated-placeholder': isSearchHover,'is-focused': isFocused  }"
          style="width:100%; height:60%; margin-top:3%"
          @focus="handleFocus"
          @blur="handleBlur"
      >
      </el-input>
    </div>

    <div class="selectionSet">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" style="margin-top: 3%;margin-left: 13%">
        <el-tab-pane label="首页" name="first">    </el-tab-pane>
        <el-tab-pane label="资料库" name="second">  </el-tab-pane>
        <el-tab-pane label="讨论区" name="third">   </el-tab-pane>
        <el-tab-pane label="收藏夹" name="fourth">  </el-tab-pane>
      </el-tabs>

      <img src="../../asset/board/碳治郎1.png"
           style="height:90%;margin-left: 10%;margin-top: 0.5%;user-select: none;">
    </div>
  </div>
</template>

<style scoped>
.navigator {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  height: 9%;
  /* 添加底部阴影效果 */
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.logo {
  width: 30%;
  height: 100%;
  display: flex;
  padding-left: 3%;
}

.search {
  display: flex;
  width: 30%;
  height: 100%;
  user-select: none;
}

:deep(.round-input .el-input__wrapper) {
  border-radius: 20px !important;
  background-color: #FFFFFF;
  box-shadow: 0 0 0 1.5px rgb(210, 203, 241) !important;
}

.selectionSet {
  display: flex;
  width: 40%;
  height: 100%;
}

:deep(.el-tabs__item) {
  color: #000000;
  font-size: 20px;
  transition: color 0.4s ease;
  user-select: none;
}

::v-deep .el-tabs__active-bar {
  background-color: rgb(2, 90, 255) !important;
}

:deep(.el-tabs__nav-wrap::after) {
  position: static !important;
}

@keyframes placeholderSlide {
  0% { transform: translateX(0); opacity: 0.6 }
  100% { transform: translateX(-10px); opacity: 0.2 }
}

:deep(.round-input .el-input__wrapper) {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); /* 平滑过渡 */
}

/* 悬停状态增强 */
:deep(.round-input:hover .el-input__wrapper) {
  box-shadow: 0 0 0 2px rgba(2, 90, 255, 0.3) !important;
  transform: scale(1.02);
}

/* 聚焦状态动画 */
:deep(.round-input.is-focused  .el-input__wrapper) {
  box-shadow: 0 0 0 3px rgba(2, 90, 255, 0.2) !important;
  background-color: #f8f9fa;
}

/* 动态placeholder动画 */
.animated-placeholder::placeholder {
  font-size: 14px;
  transition: all 0.3s ease;
  text-align: center;
}

/* 悬停时placeholder动画 */
:deep(.round-input:hover) .animated-placeholder::placeholder {
  animation: placeholderSlide 0.8s infinite alternate;
  text-align: left;
  padding-left: 28px;
}

/* 聚焦时placeholder消失动画 */
:deep(.round-input:focus-within) .animated-placeholder::placeholder {
  opacity: 0;
  transform: translateX(20px);
  transition: all 0.2s ease;
}
@keyframes streaming-light {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 流光容器 */
:deep(.round-input) .el-input__wrapper {
  position: relative;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 流光伪元素 */
:deep(.round-input) .el-input__wrapper::after {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(
      45deg,
      rgba(2,90,255,0.1) 0%,
      rgba(255,255,255,0.8) 25%,
      rgba(2,90,255,0.3) 50%,
      rgba(255,255,255,0.8) 75%,
      rgba(2,90,255,0.1) 100%
  );
  background-size: 400% 400%;
  opacity: 0;
  z-index: 1;
  transition: opacity 0.8s ease;
  pointer-events: none;
  border-radius: 22px;
}

/* 触发动画的条件 */
:deep(.round-input.is-focused)  .el-input__wrapper::after,
:deep(.round-input:hover) .el-input__wrapper::after {
  opacity: 1;
  animation: streaming-light 8s linear infinite;
}

/* 输入框内容层提升层级 */
:deep(.el-input__inner) {
  position: relative;
  z-index: 2;
  background: transparent !important;
}
</style>

<script setup>
import { ref } from 'vue';

let isSearchHover=ref(false);
let isFocus=ref(false);
const keyword = ref('');
const activeName = ref('first');

const isFocused = ref(false);

const handleFocus = () => {
  isFocused.value  = true;
};
const handleBlur = () =>
{
  isFocused.value  = false;
};
</script>

总结

本文中实现了一个前端代码比较完善的Nav开发,展示了各种可能遇到的问题,希望能对读者有所帮助

### 创建和配置移动端 Vue.js 项目的指南 #### 安装 Vue CLI 工具 为了简化开发流程并提供丰富的功能支持,建议使用官方提供的 Vue CLI 来初始化项目。通过命令行工具可以快速搭建起一个基于 Webpack 的现代前端工程架构。 ```bash npm install -g @vue/cli ``` 安装完成后即可利用 `vue create` 命令来启动新应用的构建过程[^1]。 #### 使用 Vue CLI 创建项目模板 执行如下指令将会引导开发者完成一系列选项设置,从而生成适合移动设备使用的初始代码结构: ```bash vue create my-mobile-app ``` 在此过程中可以选择预设好的 PWA (Progressive Web App) 插件以增强应用程序在不同平台上的兼容性和用户体验效果。 #### 配置 Meta Viewport 标签优化显示比例 为了让页面能够自适应各种屏幕尺寸,在 HTML 文件头部加入特定 meta viewport 属性是非常必要的操作之一: ```html <meta name="viewport" content="width=device-width, initial-scale=1"> ``` 这段声明确保了浏览器会按照实际物理像素宽度渲染网页内容,并保持默认缩放级别不变,使得界面元素不会因为视窗大小变化而失真变形[^4]。 #### 引入响应式框架提升布局灵活性 考虑到跨屏适配的需求,推荐引入像 Bootstrap 这样的 CSS 框架辅助设计工作。借助于专门针对 Vue 生态定制过的版本——Bootstrap-Vue,则可以在不牺牲性能的前提下获得更加便捷高效的组件库支持: ```javascript import { BootstrapVue, IconsPlugin } from 'bootstrap-vue' // Install BootstrapVue Vue.use(BootstrapVue) // Optionally install the BootstrapVue icon components plugin Vue.use(IconsPlugin) ``` 以上步骤涵盖了从环境准备到基础特性集成的主要环节,后续可根据具体业务逻辑继续扩展和完善整个系统的功能性[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值