| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const n = `<template>
- <a-anchor
- :affix="false"
- showInkInFixed
- :targetOffset="10"
- :getContainer="getContainer"
- @click="handleAnchorClick"
- :items="anchors"
- />
- </template>
- <script setup>
- import { nextTick, onMounted, ref } from 'vue'
- defineOptions({ name: 'SAnchor' })
- const props = defineProps({
- // 监听的滚动容器元素(不传则监听 window)
- el: {
- type: String,
- default: 'body'
- }
- })
- const anchors = ref([])
- /** 锚点 */
- function getItemAnchor () {
- const elms = document.querySelectorAll(
- '.s-collapses .s-collapses-item .s-collapses-item-title'
- )
- if (elms.length > 0) {
- elms.forEach((i) => {
- anchors.value.push({
- el: i,
- key: i.id,
- href: \`#\${i.id}\`,
- title: i.lastChild.innerText
- })
- })
- }
- }
- function initRightNav () {
- getItemAnchor()
- setTimeout(() => {
- anchors.value = []
- getItemAnchor()
- }, 500)
- }
- function handleAnchorClick (e, link) {
- e.preventDefault()
- const i = anchors.value.find((i) => i.href === link.href)
- i?.el?.scrollIntoView({ block: 'start', behavior: 'smooth' })
- }
- function getContainer () {
- if (props.el === 'el') {
- return document.getElementById('form-container')
- }
- return document.querySelector(\`.\${props.el}\`)
- }
- onMounted(() => {
- nextTick(() => {
- initRightNav()
- })
- })
- <\/script>
- `;
- export {
- n as default
- };
|