From 8496cae1aa8fd345b31e98424587aa2fd71978f4 Mon Sep 17 00:00:00 2001 From: Ifraim Solomonov Date: Fri, 1 Mar 2024 20:58:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A5=D1=83=D0=BA=20useElementOnScreen,=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=D1=82=D0=B8=D0=BB=D0=B5=D0=B9=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B2=D0=B1=D0=B0=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Navbar/Navbar.jsx | 1 - src/components/Navbar/Navbar.styles.scss | 9 +++++-- src/lib/useElementOnScreen.js | 30 ++++++++++++++++++++++++ src/pages/Main/Main.jsx | 22 +++++++++++++++-- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/lib/useElementOnScreen.js diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index ebbadb5..d383a80 100755 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -1,5 +1,4 @@ import { scb as logo } from '../../assets/images'; -import { NavLink } from "react-router-dom"; import NavbarLink from './NavbarLink'; import './Navbar.styles.scss'; diff --git a/src/components/Navbar/Navbar.styles.scss b/src/components/Navbar/Navbar.styles.scss index 5c1bd01..e71c4cf 100755 --- a/src/components/Navbar/Navbar.styles.scss +++ b/src/components/Navbar/Navbar.styles.scss @@ -1,13 +1,13 @@ @import "../../assets/styles/theme.scss"; .navbar { - position: absolute; + position: fixed; top: 0; width: 80%; height: 60px; max-height: 80px; padding: 1em 10%; - background: transparent; + background: $bg-secondary; z-index: 1; display: flex; @@ -43,4 +43,9 @@ background: $text-secondary; } } +} + +.navbar.absolute { + position: absolute; + background: transparent; } \ No newline at end of file diff --git a/src/lib/useElementOnScreen.js b/src/lib/useElementOnScreen.js new file mode 100644 index 0000000..78084b5 --- /dev/null +++ b/src/lib/useElementOnScreen.js @@ -0,0 +1,30 @@ +import { useRef, useEffect, useState } from "react"; + +const useElementOnScreen = (options) => { + + const containerRef = useRef(null); + const [isVisible, setIsVisible] = useState(false); + + const observerCallback = (entries) => { + const [entry] = entries; + setIsVisible(entry.isIntersecting); + }; + + useEffect(() => { + const observer = new IntersectionObserver(observerCallback, options); + + if (containerRef.current) { + observer.observe(containerRef.current); + } + + return () => { + if (containerRef.current) { + observer.unobserve(containerRef.current); + } + } + }, [containerRef, options]) + + return [containerRef, isVisible]; +} + +export default useElementOnScreen; \ No newline at end of file diff --git a/src/pages/Main/Main.jsx b/src/pages/Main/Main.jsx index 82b017b..40d021b 100755 --- a/src/pages/Main/Main.jsx +++ b/src/pages/Main/Main.jsx @@ -1,11 +1,29 @@ +import { useEffect } from 'react'; + import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components'; +import useElementOnScreen from '../../lib/useElementOnScreen'; function Main() { + const [containerRef, isVisible] = useElementOnScreen({ + root: null, + rootMargin: '0px', + threshold: 0, + }); + + useEffect(() => { + const [ navbar ] = document.getElementsByClassName('navbar'); + if (isVisible) { + navbar.classList.add('absolute'); + } else { + navbar.classList.remove('absolute'); + } + }, [isVisible]); + return ( <> -
+

Думай иначе, будь креативным!

-

Студенческое конструкторское бюро
+

Студенческое конструкторское бюро
"Компьютерное инновационное творчество"

Связаться с нами