React PC端悬浮锚点吸顶导航

2024-05-06 07:18

本文主要是介绍React PC端悬浮锚点吸顶导航,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求描述
  • 实现PC端吸顶导航

实现分析
  • 先采用absolute绝对定位
  • 滚动超过锚点导航时,修改该导航定位为fixed
效果预览

实现方法
/** 锚点导航*/
import React from "react";
import styles from "./index.less";
import classnames from "classnames";
import { ActiveType } from "@17zwd/fe-egg-factory-page/detail/type";interface PageState {// 目录激活项activeItem?: string;// 是否吸顶isSticky?: boolean;
}class AnchorNav extends React.Component<any, PageState> {private scrollTimer;constructor(props) {super(props);this.state = {activeItem: ActiveType.baseInfo,isSticky: false,};}componentDidMount(): void {// 绑定页面滚动事件window.addEventListener("scroll", this.onPageScroll);}componentWillUnmount(): void {// 解绑页面滚动事件window.removeEventListener("scroll", this.onPageScroll);}compare = (prop) => {return function (a, b) {const value1 = a[prop];const value2 = b[prop];return value1 - value2;};};// 页面滚动事件onPageScroll = (): void => {if (this.scrollTimer) {clearTimeout(this.scrollTimer);}this.scrollTimer = setTimeout(() => {// 吸顶const firstItemTop = document.getElementById(ActiveType.baseInfo)?.getBoundingClientRect().top;if (firstItemTop && firstItemTop < 0) {this.setState({isSticky: true,});} else {this.setState({isSticky: false,});}// 动态高亮const viewTops: Array<any> = [];for (const i in ActiveType) {const id = ActiveType[i];const ele = document.getElementById(id);if (!ele) return;const top = ele?.getBoundingClientRect().top;if (top < 0) continue;viewTops.push({id,top,});}viewTops.sort(this.compare("top"));this.setState({activeItem: viewTops[0]?.id,});}, 90);};//跳转到的锚点onScrollToAnchor = (activeItem: string): void => {if (activeItem) {// 找到锚点const anchorElement = document.getElementById(activeItem);// 如果对应id的锚点存在,就跳转到锚点if (anchorElement) {anchorElement.scrollIntoView({ block: "start", behavior: "smooth" });this.setState({activeItem,});}}};getItemName = (en: string): string => {let res = "";switch (en) {case ActiveType.baseInfo:res = "基础信息";break;case ActiveType.certify:res = "资质认证";break;case ActiveType.overview:res = "综合概览";break;case ActiveType.remark:res = "工厂简介";break;case ActiveType.pics:res = "工厂实拍";break;case ActiveType.product:res = "产品案例";break;default:break;}return res;};renderItem = (): JSX.Element => {const { activeItem } = this.state;const items: Array<any> = [];for (const i in ActiveType) {const element = ActiveType[i];if (element) {items.push(element);}}return (<React.Fragment>{items.map((item, index) => {return (<likey={index}onClick={() => this.onScrollToAnchor(item)}className={activeItem === item ? styles.active : ""}><spanclassName={classnames(styles.anchorItem, styles.anchorNoBorder)}>{this.getItemName(item)}</span></li>);})}</React.Fragment>);};render(): JSX.Element {const { isSticky } = this.state;return (<React.Fragment><divclassName={classnames(styles.mainAnchor,isSticky ? styles.positionSticky : styles.positionAbsolute)}><ul className={styles.anchorList}>{this.renderItem()}</ul></div></React.Fragment>);}
}export default AnchorNav;
@import "@/less/mixin.less";// 导航
.positionSticky {position: fixed;margin-left: -124px;z-index: 1;
}
.positionAbsolute {position: absolute;left: -124px;
}
.mainAnchor {top: 0;width: 124px;.anchorList {margin-bottom: 0;padding: 0 24px;width: 112px;box-sizing: border-box;background-color: @background-secondary-color;> li {position: relative;color: @text-thrid-color;transition: all 0.2s linear;&:hover {color: @main-primary-color;}}.anchorItem {display: block;padding: 16px 0;height: 53px;font-size: 16px;line-height: 21px;box-sizing: border-box;border-top: solid 1px @border-primary-color;cursor: pointer;}.anchorNoBorder {border-top: none;}.active {color: @main-primary-color;&::before {position: absolute;content: "";left: -14px;top: 24px;width: 6px;height: 6px;border-radius: 50%;background-color: @main-primary-color;}}}.navBox {visibility: hidden;}
}

这篇关于React PC端悬浮锚点吸顶导航的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/963738

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h