Vue3導(dǎo)航欄組件封裝實(shí)現(xiàn)方法
在Vue3中封裝一個(gè)導(dǎo)航欄組件,并且實(shí)現(xiàn),隨著滾動(dòng)條滾動(dòng)實(shí)現(xiàn)一個(gè)吸頂效果,供大家參考
導(dǎo)航欄組件的效果圖:

滾動(dòng)條滾動(dòng)以后的吸頂效果示意圖:

具體代碼展示:
<template>
<header class="app-header">
<div class="container">
<!-- 頭部導(dǎo)航區(qū)域 -->
<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>
中間菜單部門單獨(dú)封裝一個(gè)組件,實(shí)現(xiàn)兩個(gè)組件的復(fù)用 (HeaderNavCommon組件)

<template>
<ul class="app-header-nav">
<li class="home"><RouterLink to="/">首頁(yè)</RouterLink></li>
<li><a href="#" >美食</a></li>
<li><a href="#" >餐廚</a></li>
<li><a href="#" >藝術(shù)</a></li>
<li><a href="#" >電器</a></li>
<li><a href="#" >居家</a></li>
<li><a href="#" >洗護(hù)</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 />
<!-- 右側(cè)按鈕 -->
<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() {
// 頁(yè)面滾動(dòng)距離
// const top = ref(0)
// window.onscroll = () => {
// top.value = document.documentElement.scrollTop
// }
// 頁(yè)面滾動(dòng)利用第三方包
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;
// 此處為關(guān)鍵樣式!!!
// 默認(rèn)情況下完全把自己移動(dòng)到上面
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案
今天小編就為大家分享一篇微信內(nèi)置開發(fā) iOS修改鍵盤換行為搜索的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue3+Element-plus項(xiàng)目自動(dòng)導(dǎo)入報(bào)錯(cuò)的解決方案
vue3出來一段時(shí)間了,element也更新了版本去兼容vue3,下面這篇文章主要給大家介紹了關(guān)于Vue3+Element-plus項(xiàng)目自動(dòng)導(dǎo)入報(bào)錯(cuò)的解決方案,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
ElementUI的this.$notify.close()調(diào)用不起作用的解決
本文主要介紹了ElementUI的this.$notify.close()調(diào)用不起作用的解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Vue3+ElementPlus封裝圖片空間組件的門面實(shí)例
圖片空間是用于管理上傳圖片的工具,可以讓用戶方便地存儲(chǔ)、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過圖片空間組件,用戶能直接在其他模塊(例如商品圖片)中選擇所需圖片2024-09-09
詳解Vue基于 Nuxt.js 實(shí)現(xiàn)服務(wù)端渲染(SSR)
直接使用 Vue 構(gòu)建前端單頁(yè)面應(yīng)用,頁(yè)面源碼時(shí)只有簡(jiǎn)單的幾行 html,這并不利于網(wǎng)站的 SEO,這時(shí)候就需要服務(wù)端渲染,本篇文章主要介紹了詳解Vue基于 Nuxt.js 實(shí)現(xiàn)服務(wù)端渲染(SSR),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
vue通過tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定
這篇文章主要介紹了vue通過tailwindcss實(shí)現(xiàn)class動(dòng)態(tài)綁定,文中給大家介紹了一些常用類名語(yǔ)法記錄,對(duì)vue動(dòng)態(tài)綁定class相關(guān)知識(shí)感興趣的朋友一起看看吧2023-07-07

