Vue3导航栏组件封装实现方法

 更新时间:2021年09月14日 14:26:29   作者:丹丹呀~~  
这篇文章主要为大家详细介绍了Vue3导航栏组件封装的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在Vue3中封装一个导航栏组件,并且实现,随着滚动条滚动实现一个吸顶效果,供大家参考

导航栏组件的效果图:

滚动条滚动以后的吸顶效果示意图:

具体代码展示:

<template>
  <header class="app-header">
    <div class="container">
      <!-- 头部导航区域 -->
      <HeaderNavCommon />
      <div class="search">
        <i class="iconfont icon-search"></i>
        <input type="text" placeholder="搜一搜" />
      </div>
      <div class="cart">
        <a href="#" class="curr">
          <i class="iconfont icon-cart"></i>
          <em>2</em>
        </a>
      </div>
    </div>
  </header>
</template>
 
<script>
import HeaderNavCommon from '@/components/header-nav-common.vue'
export default {
  name: 'AppHeader',
  components: {
    HeaderNavCommon
  }
}
</script>
 
<style scoped lang="less">
.app-header {
  background: #fff;
  .container {
    display: flex;
    align-items: center;
  }
  .navs {
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;
    li {
      margin-right: 40px;
      width: 38px;
      text-align: center;
      a {
        display: inline-block;
        font-size: 16px;
        line-height: 32px;
        height: 32px;
      }
      &:hover {
        a {
          color: @xtxColor;
          border-bottom: 1px solid @xtxColor;
        }
      }
    }
  }
  .search {
    width: 170px;
    height: 32px;
    position: relative;
    border-bottom: 1px solid #e7e7e7;
    line-height: 32px;
    .icon-search {
      font-size: 18px;
      margin-left: 5px;
    }
    input {
      width: 140px;
      padding-left: 5px;
      color: #666;
    }
  }
  .cart {
    width: 50px;
    .curr {
      height: 32px;
      line-height: 32px;
      text-align: center;
      position: relative;
      display: block;
      .icon-cart {
        font-size: 22px;
      }
      em {
        font-style: normal;
        position: absolute;
        right: 0;
        top: 0;
        padding: 1px 6px;
        line-height: 1;
        background: @helpColor;
        color: #fff;
        font-size: 12px;
        border-radius: 10px;
        font-family: Arial;
      }
    }
  }
}
</style>

中间菜单部门单独封装一个组件,实现两个组件的复用  (HeaderNavCommon组件)

<template>
  <ul class="app-header-nav">
    <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li><a href="#" >美食</a></li>
    <li><a href="#" >餐厨</a></li>
    <li><a href="#" >艺术</a></li>
    <li><a href="#" >电器</a></li>
    <li><a href="#" >居家</a></li>
    <li><a href="#" >洗护</a></li>
    <li><a href="#" >孕婴</a></li>
    <li><a href="#" >服装</a></li>
    <li><a href="#" >杂货</a></li>
  </ul>
</template>
 
<script>
 export default {
    name: 'AppHeaderNav'
   }
</script>
 
<style scoped lang='less'>
.app-header-nav {
    width: 820px;
    display: flex;
    padding-left: 40px;
    position: relative;
    z-index: 998;
  li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
  a {
    font-size: 16px;
    line-height: 32px;
    height: 32px;
    display: inline-block;
   }
  &:hover {
    a {
    color: @xtxColor;
    border-bottom: 1px solid @xtxColor;
        }
      }
  }
}
</style>

封装吸顶效果的组件

<template>
  <div class="app-header-sticky" :class="{ show: top >= 78 }">
    <div class="container" v-if="top >= 78">
      <!-- 中间 -->
      <HeaderNavCommon />
      <!-- 右侧按钮 -->
      <div class="right">
        <RouterLink to="/">品牌</RouterLink>
        <RouterLink to="/">专题</RouterLink>
      </div>
    </div>
  </div>
</template>
 
<script>
import HeaderNavCommon from '@/components/header-nav-common.vue'
// import { ref } from 'vue'
import { useWindowScroll } from '@vueuse/core'
export default {
  name: 'AppHeaderSticky',
  components: { HeaderNavCommon },
  setup() {
    // 页面滚动距离
    // const top = ref(0)
    // window.onscroll = () => {
    //   top.value = document.documentElement.scrollTop
    // }
    // 页面滚动利用第三方包
    const { y: top } = useWindowScroll()
    return { top }
  }
}
</script>
 
<style scoped lang="less">
.app-header-sticky {
  width: 100%;
  height: 80px;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;
  // 此处为关键样式!!!
  // 默认情况下完全把自己移动到上面
  transform: translateY(-100%);
  // 完全透明
  opacity: 0;
  // 显示出来的类名
  &.show {
    transition: all 0.3s linear;
    transform: none;
    opacity: 1;
  }
  .container {
    display: flex;
    align-items: center;
  }
  .right {
    width: 220px;
    display: flex;
    text-align: center;
    padding-left: 40px;
    border-left: 2px solid @xtxColor;
    a {
      width: 38px;
      margin-right: 40px;
      font-size: 16px;
      line-height: 1;
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue实现标签页切换/制作tab组件详细教程

    vue实现标签页切换/制作tab组件详细教程

    在项目开发中需要使用vue实现tab页签切换功能,所以这里总结下,这篇文章主要给大家介绍了关于vue实现标签页切换/制作tab组件的相关资料,需要的朋友可以参考下
    2023-11-11
  • Vue2递归组件实现树形菜单

    Vue2递归组件实现树形菜单

    这篇文章主要为大家详细介绍了Vue2递归组件实现树形菜单的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • vue cli3 调用百度翻译API翻译页面的实现示例

    vue cli3 调用百度翻译API翻译页面的实现示例

    这篇文章主要介绍了vue cli3 调用百度翻译API翻译页面的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • vue实现列表无缝滚动

    vue实现列表无缝滚动

    这篇文章主要为大家详细介绍了vue实现列表无缝滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • Vue.js点击切换按钮改变内容的实例讲解

    Vue.js点击切换按钮改变内容的实例讲解

    今天小编就为大家分享一篇Vue.js点击切换按钮改变内容的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue 中使用lodash对事件进行防抖和节流操作

    Vue 中使用lodash对事件进行防抖和节流操作

    这篇文章主要介绍了Vue 中使用lodash对事件进行防抖和节流操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vue实现输入框只允许输入数字

    vue实现输入框只允许输入数字

    在vue项目中,输入框只允许输入数字,现将自己使用的一种方法记录,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2023-11-11
  • vue-cli+webpack在生成的项目中使用bootstrap实例代码

    vue-cli+webpack在生成的项目中使用bootstrap实例代码

    本篇文章主要介绍了vue-cli+webpack在生成的项目中使用bootstrap实例代码,具有一定的参考价值,有兴趣的可以了解一下
    2017-05-05
  • vite+ts vite.config.ts使用path报错问题及解决

    vite+ts vite.config.ts使用path报错问题及解决

    这篇文章主要介绍了vite+ts vite.config.ts使用path报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • Vue2.0权限树组件实现代码

    Vue2.0权限树组件实现代码

    本文通过实例代码给大家介绍了Vue2.0权限树组件实现代码,需要的的朋友参考下吧
    2017-08-08

最新评论