React如何实现具备吸顶和吸底功能组件

其他教程   发布日期:2023年06月30日   浏览次数:415

本篇内容介绍了“React如何实现具备吸顶和吸底功能组件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

具体要求:

  • 需要可以设置是

    1. 吸顶
    还是
    1. 吸底
    1. 吸顶
    可以设置距离视窗顶部的位置,
    1. 吸顶
    可以设置距离视窗底部的位置。
  • 可以对正常组件都生效,不影响组件自身的样式。

实现

组件主要是为了

  1. 吸顶
或者
  1. 吸底
功能,那么就命名为
  1. AutoFixed

主要实现逻辑:需要判断自身在视窗内的位置与设置的

  1. 吸顶
或者
  1. 吸底
位置是否匹配,匹配上了则可以进行
  1. 吸顶
或者
  1. 吸底

获取自身位置一般可以用

  1. 滚动的位置
  1. 自身距离页面顶部
的位置来判断,但实现起来会麻烦一些,
  1. IntersectionObserver
也很好用,而且性能会更好,因此这里将直接使用
  1. IntersectionObserver
来处理。

下面,我们先实现一个基于

  1. IntersectionObserver
实现的判断位置的
  1. hook

定义 props 类型:

  1. import { RefObject } from "react";
  2. type Props = {
  3. el: React.RefObject<Element>;
  4. options?: IntersectionObserverInit;
  5. };

可接受参数:

  1. el
: React 的
  1. ref
实例,被判断判断位置的 DOM 元素。
  1. options
: IntersectionObserver 构造函数的初始化参数。

具体实现:

  1. import React, { useEffect, useState } from "react";
  2. export function useIntersection(props: Props): boolean {
  3. const { el, options } = props;
  4. // 是否到了指定位置区域
  5. const [intersection, setIntersection] = useState(true);
  6. useEffect(() => {
  7. if (!el.current) return;
  8. // 初始化 IntersectionObserver 实例
  9. const intersectionObserver = new IntersectionObserver(
  10. function (entries) {
  11. setIntersection(entries[0].intersectionRatio === 1);
  12. },
  13. { ...options, threshold: [1] }
  14. );
  15. // 开始监听
  16. intersectionObserver.observe(el.current);
  17. return (): void => {
  18. // 销毁
  19. intersectionObserver.disconnect();
  20. };
  21. }, [el.current]);
  22. return intersection;
  23. }

现在实现了一个可以根据传入的参数来控制否到了指定位置区域的 hook :

  1. useIntersection

  1. useIntersection
只是对
  1. IntersectionObserver
的简单封装,并没有复杂实现,具体作用就是用于判断某个元素是否进入了
  1. 可视窗口
,想了解更多可以点击去查看它的MDN文档。

下面再来实现我们要实现的具备吸顶和吸底功能的组件:

  1. AutoFixed

参数定义:

  1. export type AutoFixedProps = React.ImgHTMLAttributes<HTMLDivElement> & {
  2. /** 吸顶距离 */
  3. top?: string;
  4. /** 吸底距离 */
  5. bottom?: string;
  6. /** 是否一直吸顶或者吸底 */
  7. alwaysFixed?: boolean;
  8. zIndex?: number;
  9. children: React.ReactNode;
  10. /** 元素框高度 */
  11. height: number | string;
  12. /** 相对的目标元素,因为是用的 fixed 定位,记得做相应处理。 */
  13. root?: Element | Document | null;
  14. /** 固定的时候才有的className */
  15. fixedClassName?: string;
  16. /** 固定的时候才有的样式 */
  17. fixedStyle?: React.CSSProperties;
  18. /** fixed状态改变时调用 */
  19. onFixedChange?: (isFixed: boolean) => void;
  20. };

可接受参数 基于

  1. React.HtmlHTMLAttributes<HTMLDivElement>
,也就是继承了
  1. div
的默认属性。

其他自定义参数说明:

    1. top
    吸顶距离,
    1. 元素顶部
    距离
    1. 视窗顶部
    小于等于
    1. top
    时,进行吸顶。
    1. bottom
    吸底部距离,
    1. 元素底部
    距离
    1. 视窗底部
    大于等于
    1. bottom
    时,进行吸底。注意逻辑是和
    1. 吸顶
    相反。
    1. alwaysFixed
    ,用于支持默认就要一直吸顶或者吸底的情况,需要配合
    1. top
    1. bottom
    来使用。
    1. zIndex
    控制吸顶或者吸底时的样式层级。
    1. children
    1. children
    元素是正常的 React 组件即可。
    1. height
    被包裹元素的高度.也就是
    1. children
    元素 的高度。
    1. root
    ,相对视窗的目标元素,也就是可以控制在某个区域内进行吸顶和吸底,但因为这里是用的
    1. fixed
    定位,如果需要设置
    1. root
    时,需要改变成
    1. absolute
    定位。
    1. fixedClassName
    吸顶和吸底的时候需要动态添加的
    1. className
    1. fixedStyle
    吸顶和吸底的时候需要动态添加的
    1. 样式
    1. onFixedChange
    吸顶和吸底的时候告诉外界。

具体实现:

  1. import React, { useRef, useEffect } from "react";
  2. import { useIntersection } from "../../components/hooks/use-intersection";
  3. export const AutoFixed = (props: AutoFixedProps) => {
  4. const {
  5. alwaysFixed,
  6. top,
  7. bottom,
  8. style,
  9. height,
  10. root,
  11. zIndex = 100,
  12. children,
  13. className,
  14. fixedClassName,
  15. fixedStyle,
  16. onFixedChange,
  17. ...rest
  18. } = props;
  19. // `bottom` 值存在时,表面要悬浮底部
  20. const isFiexdTop = !bottom;
  21. const wrapperRef = useRef<HTMLDivElement>(null);
  22. // 设置监听参数控制:top 为吸顶距离,bottom 为吸底距离
  23. const options = {
  24. rootMargin: isFiexdTop
  25. ? `-${top || "0px"} 0px 1000000px 0px`
  26. : `0px 0px -${bottom || "0px"} 0px`,
  27. // 设置root
  28. root,
  29. } as IntersectionObserverInit;
  30. // 是否悬浮
  31. const intersection = useIntersection({ el: wrapperRef, options });
  32. const shouldFixed = alwaysFixed ? true : !intersection;
  33. useEffect(() => {
  34. // 通知外部
  35. onFixedChange?.(shouldFixed);
  36. }, [shouldFixed, onFixedChange]);
  37. return (
  38. <div
  39. style={{ ...style, height }}
  40. {...rest}
  41. className={`${className}${shouldFixed ? " fixedClassName" : ""}`}
  42. ref={wrapperRef}
  43. >
  44. <div
  45. style={{
  46. height,
  47. position: shouldFixed ? "fixed" : "initial",
  48. top: isFiexdTop ? top || 0 : undefined,
  49. bottom: isFiexdTop ? undefined : bottom || 0,
  50. zIndex: zIndex,
  51. ...(shouldFixed ? fixedStyle : {}),
  52. }}
  53. >
  54. {children}
  55. </div>
  56. </div>
  57. );
  58. };

实现逻辑:

  • 使用了

    1. alwaysFixed
    判断是否一直悬浮。
  • 默认悬浮顶部,

    1. bottom
    值存在时,表明要悬浮底部。
    1. useIntersection
    传入监听位置控制参数。
  • 根据

    1. useIntersection
    的结果来判断是否应该
    1. 吸顶
    1. 吸底
  • 做了

    1. style
    样式和
    1. className
    传入处理的问题,以及 zIndex 层级问题。
  • 吸顶时,不进行设置

    1. bottom
    ,吸底时,不进行设置
    1. bottom

主要核心逻辑是第

  1. 3
点:
  1. const options = {
  2. rootMargin: `-${top || "0px"} 0px -${bottom || "0px"} 0px`,
  3. };

  1. rootMargin
中:
  1. -${top || "0px"}
为吸顶距离,
  1. -${bottom || "0px"}
为吸底距离。一定要是负的,正数表示延伸到了视窗外的距离,负数表示距离视窗顶部或者底部的距离。

使用方式:

  1. <AutoFixed
  2. // 距离顶部为 20px 吸顶
  3. top="20px"
  4. // 占位高度,也就是 children 的高度
  5. height="20px"
  6. // fixed状态改变时
  7. onFixedChange={(isFixed) => {
  8. console.log(`isFixed: ` + isFixed);
  9. }}
  10. // fixed状态需要添加的className
  11. fixedClassName="hello"
  12. // fixed状态需要添加的style
  13. fixedStyle={{ color: "red" }}
  14. >
  15. <div>
  16. 我是悬浮内容,高度 20px, 距离顶部为 20px 吸顶
  17. </div>
  18. </AutoFixed>

以上就是React如何实现具备吸顶和吸底功能组件的详细内容,更多关于React如何实现具备吸顶和吸底功能组件的资料请关注九品源码其它相关文章!