4641751420510_.pic.jpg
4651751420519_.pic.jpg
有导航的
image.png
- :target-offset="79"如果顶部有定位在顶部,有透明度的导航栏banner图片上,需要设置偏移
按需引入antd的锚点
<template>
<div class="main-container" ref="contentContainer" id="scrollContainer">
<div class="anchor-box" id="anchorBox">
<!-- :target-offset="79"如果顶部有定位在顶部,有透明度的导航栏banner图片上,需要设置偏移 -->
<a-anchor
:getContainer="getContainer"
:activeKey="activeKey"
@change="handleChange"
>
<a-anchor-link href="#section1" title="section1" />
<a-anchor-link href="#section2" title="section2" />
<a-anchor-link href="#section3" title="section3" />
<a-anchor-link href="#section4" title="section4" />
<a-anchor-link href="#section5" title="section5" />
</a-anchor>
</div>
<!-- 顶部图宽度撑满 高度保证比例 -->
<div
style="
width: 100%;
background: green;
position: relative;
height: 298px;
overflow: hidden;
"
></div>
<div style="margin-top: -50px; position: relative">
<h2 id="section1"><a href="#section1" class="anchor"></a></h2>
<div
style="
width: 80%;
margin: 0 auto;
height: 350px;
display: flex;
justify-content: space-between;
"
>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
</div>
</div>
<h2 style="margin-top: 0px" id="section2">
<a href="#section2" class="anchor"></a>
</h2>
<h2 style="text-align: center">section2</h2>
<div style="margin-top: 20px; position: relative">
<div
style="
width: 80%;
margin: 0 auto;
height: 350px;
display: flex;
justify-content: space-between;
"
>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
</div>
</div>
<h2 id="section3"><a href="#section3" class="anchor"></a></h2>
<h2 style="text-align: center">section3</h2>
<div style="margin-top: 20px; position: relative">
<div
style="
width: 80%;
margin: 0 auto;
height: 350px;
display: flex;
justify-content: space-between;
"
>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
</div>
</div>
<h2 id="section4"><a href="#section4" class="anchor"></a></h2>
<h2 style="text-align: center">section4</h2>
<div style="margin-top: 20px; position: relative">
<div
style="
width: 80%;
margin: 0 auto;
height: 350px;
display: flex;
justify-content: space-between;
"
>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
</div>
</div>
<h2 id="section5"><a href="#section5" class="anchor"></a></h2>
<h2 style="text-align: center">section5</h2>
<div style="margin-top: 20px; position: relative">
<div
style="
width: 80%;
margin: 0 auto;
height: 350px;
display: flex;
justify-content: space-between;
"
>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
<div
style="
border-radius: 20px;
background: pink;
width: 30%;
height: 350px;
"
></div>
</div>
</div>
</div>
</template>
<script>
import "ant-design-vue/dist/antd.css";
import { Anchor } from "ant-design-vue";
export default {
components: {
"a-anchor": Anchor, // 在模板中使用 <a-button> 或 <AButton> 都可以。
"a-anchor-link": Anchor.Link,
},
data() {
return {
activeKey: null,
};
},
computed: {},
mounted() {
window.addEventListener("scroll", this.handleScroll, true);
},
beforeDestroy() {
window.removeEventListener("scroll", this.handleScroll);
},
methods: {
handleChange(newActiveKey) {
this.activeKey = newActiveKey;
},
handleScroll(event) {
if (event.target.scrollTop > 700) {
var element = document.getElementById("anchorBox");
element.classList.add("anchorFixed");
} else {
var element = document.getElementById("anchorBox");
element.classList.remove("anchorFixed");
}
},
getContainer() {
return document.getElementById("scrollContainer");
},
},
created() {},
};
</script>
<style scoped>
.anchor-box {
box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.1);
width: 200px;
z-index: 999999;
position: fixed;
left: 20px;
top: 412px;
border-radius: 10px;
overflow: hidden;
transition: top 0.5s ease;
}
.anchorFixed {
top: 120px !important;
}
.ant-affix {
position: relative;
border-radius: 20px;
padding: 20px;
width: 200px !important;
}
::v-deep .ant-anchor-wrapper {
margin-left: -4px;
padding-left: 4px;
overflow: auto;
}
::v-deep .ant-anchor {
margin: 30px 20px;
}
::v-deep .ant-anchor-wrapper::before {
position: absolute;
left: 16px;
content: "";
display: inline-block;
width: 11px;
height: 11px;
border-radius: 11px;
border: 3px solid #d7dce6;
background-color: #fff;
bottom: 20px;
}
::v-deep .ant-anchor-wrapper::after {
position: absolute;
left: 16px;
content: "";
display: inline-block;
width: 11px;
height: 11px;
border-radius: 11px;
border: 3px solid #d7dce6;
background-color: #fff;
top: 20px;
}
::v-deep .ant-anchor-link {
line-height: 1.143;
padding: 18px 0 18px 15px;
}
::v-deep .ant-anchor-link::before {
position: absolute;
left: -3px;
display: inline-block;
width: 9px;
height: 9px;
border-radius: 9px;
margin-top: 5px;
cursor: default;
content: "";
background-color: #d7dce6;
}
::v-deep .ant-anchor-link-active > .ant-anchor-link-title {
color: #333;
font-weight: 600;
}
::v-deep .ant-anchor-link-title {
font-size: 18px;
}
::v-deep .ant-anchor-ink-ball {
position: absolute;
left: 50%;
z-index: 99;
width: 10px;
height: 10px;
background-color: #3c85f3;
border: 0;
border-radius: 0;
transform: translateX(-50%);
}
::v-deep .ant-anchor-ink-ball::before {
position: absolute;
content: "";
display: block;
right: -10px;
width: 0;
height: 0;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 5px solid #3c85f3;
border-right: 5px solid transparent;
}
::v-deep .ant-anchor-ink-ball.visible {
display: inline-block;
}
</style>
vue2按需引入antd
image.png
image.png
- npm install ant-design-vue@1.7.8
- npm install babel-plugin-import --save-dev
- npm install less less-loader --save-dev