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

相关文章

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程