1.功能实现背景
电商网站的首页内容会比较多,会有很多频,为了能让用户在滚动浏览内容的过程中都能够快速的切换到其它模块,需要导航一直可见,所以需要一个吸顶导航的效果
2.交互要求
1.滚动距离大于等于78个px的时候,组件固定在视口顶部跟随页面移动 2.滚动距离小于78个px的时候,组件消失
3.实现思路
1.准备一个吸顶组件,准备一个类名,控制样式让其固定在顶部 2.监听页面滚动,判断滚动距离,距离大于78px添加类名
4.具体实现
4.1新建吸顶导航组件src/layout/components/header-sticky.vue
<template>
<div class="app-header-sticky">
<div class="container">
<!-- 左侧图标 -->
<RouterLink class="logo" to="/" />
<!-- 中间的分类导航菜单 -->
<TopNavCommon />
<!-- 右侧按钮 -->
<div class="right">
<RouterLink to="/">品牌</RouterLink>
<RouterLink to="/">专题</RouterLink>
</div>
</div>
</div>
</template>
<script>
import TopNavCommon from '@/components/top-nav-common.vue'
export default {
name: 'AppHeaderSticky',
components: { TopNavCommon }
}
</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;
}
.logo {
width: 200px;
height: 80px;
background: url('~@/assets/images/logo.png') no-repeat right 2px;
background-size: 160px auto;
}
.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>
4.2Layout首页引入吸顶导航组件 src/views/layout/index.vue
<template>
<!-- 顶部通栏组件 -->
<TopNav />
<!-- header区域 -->
<Header />
<!-- 吸顶组件 -->
<HeaderSticky/>
<main>
<!-- 二级路由出口 -->
<router-view></router-view>
</main>
<!-- 底部footer -->
<Footer/>
</template>
<script>
import TopNav from './components/top-nav'
import Header from './components/header'
import Footer from './components/footer'
import HeaderSticky from '@/components/header-sticky'
export default {
name: 'XtxLayout',
components: { AppTopnav, AppHeader, AppFooter, HeaderSticky }
}
</script>
4.3在滚动到78px完成显示效果(添加类名)
.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;
+ }
4.4 通过控制滚动距离,动态添加类名 src/views/layout/components/header-sticky.vue
import TopNavCommon from '@/components/top-nav-common'
export default {
name: 'AppHeaderSticky',
components: { TopNavCommon },
+ setup () {
+ const top = ref(0)
+ window.onscroll = () => {
+ const scrollTop = document.documentElement.scrollTop
+ top.value = scrollTop
+ }
+ return { top }
+ }
}
<template>
<div class="app-header-sticky" :class='{show: top>=78}'>
<div class="container" v-if='top>=78'>
5.基于第三方实现
5.1安装@vueuse/core 包,它封装了常见的一些交互逻辑
npm i @vueuse/core@5.3.0
5.2在吸顶导航中使用
<template>
<div class="app-header-sticky" :class="{show:top >= 78}">
<div class="container" v-if='top>=78'>
<RouterLink class="logo" to="/" />
<HeaderNav />
<div class="left">
<RouterLink to="/" >品牌</RouterLink>
<RouterLink to="/" >专题</RouterLink>
</div>
</div>
</div>
</template>
<script>
import HeaderNav from './header-nav'
import { useWindowScroll } from '@vueuse/core'
export default {
name: 'AppHeaderSticky',
components: { HeaderNav },
setup () {
const { y: top } = useWindowScroll()
return { top }
}
}
</script>
总结:
- 安装npm i @vueuse/core@5.3.0版本号要锁定
- 熟悉useWindowScroll方法的基本用法
|