Skip to main content

Custom page anchor positioning, return to top, and sticky positioning

1. Usage scenarios

In a longer web page content, it is convenient for users to quickly locate the part to be consulted; In order to provide a better user experience, add the "back to the top" button, and use the anchor link to quickly return the user to the top of the page.

2. Implement functions

2.1. Create a custom page

2.2. Module configuration for anchor positioning

For each module that needs to be quickly redirected through the right Directory, turn on the "use unique identifier as DOM ID" option.

2.3. Copy the following code to Page JS and bind the action

// 目录点击
export function onSelect(selectedKeys, extra) {
const obj = {
'0-0-0': 'div_lonydol8', // 模版介绍
'0-0-1': 'div_lonydol9', // 办公地址管理页面
'0-0-2': 'div_lonydola', // 信息录入管理页面
'0-0-3': 'div_lonydolb', // 咨询业务登记
'0-0-4': 'div_lonydolc', // 落户申请...
'0-0-5': 'div_lonydold', // 证明类
}
if (selectedKeys.length && extra.selected) {
if (selectedKeys[0] === '0-0') {
// 返回顶部
this.backToTop();
} else {
// 跳转至指定锚点
this.scrollIntoPoint(obj[selectedKeys[0]]);
}
}
}

// 跳转至指定锚点
export function scrollIntoPoint(pointId) {
// api文档及兼容性:https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
document.querySelector(`#${pointId}`).scrollIntoView({
behavior: 'smooth',
});
}

// 返回顶部
export function backToTop() {
// api文档及兼容性:https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo
const scrollNode = document.querySelector('.next-shell-sub-main') || window;
scrollNode.scrollTo({
top: 0,
behavior: 'smooth',
});
}

2.4. Directory settings sticky positioning

When the scroll bar scrolls, the directory can be drawn at the top of the page to improve user experience.

Set the following style to the page, otherwise the sticky positioning will not take effect.

:root {
position: sticky;
position: -webkit-sticky;
top: 0;
border-radius: 8px;
max-height: 400px;
overflow-y: auto;
}

@media screen and (max-width: 480px){
:root {
margin-bottom: 16px;
}
}
.vc-page {
overflow: visible;
}

3. Effect

4. Try it online

This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
Copyright © 2024钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4