-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
const useInViewport = () => { | ||
const inViewRef = useRef(null); | ||
const [inViewport, setInViewport] = useState(false); | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
setInViewport(true); | ||
} else { | ||
setInViewport(false); | ||
} | ||
}); | ||
}); | ||
|
||
if (inViewRef.current) { | ||
observer.observe(inViewRef.current); | ||
} | ||
|
||
return () => { | ||
if (inViewRef.current) { | ||
observer.unobserve(inViewRef.current); | ||
} | ||
}; | ||
}, []); | ||
|
||
return { | ||
inViewRef, | ||
inViewport | ||
}; | ||
}; | ||
|
||
export default useInViewport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
|
||
const useScrollIntoView = ({offset = 0}) => { | ||
const targetRef = useRef<T>(null); | ||
const [isScrolled, setIsScrolled] = useState(false); | ||
|
||
const scrollIntoView = () => { | ||
if (targetRef.current) { | ||
const rect = targetRef.current.getBoundingClientRect(); | ||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop; | ||
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft; | ||
const elementTop = rect.top + scrollTop; | ||
const elementLeft = rect.left + scrollLeft; | ||
|
||
const windowHeight = window.innerHeight; | ||
const windowWidth = window.innerWidth; | ||
|
||
// 根据偏移量调整滚动位置 | ||
const adjustedTop = elementTop - offset; | ||
const adjustedLeft = elementLeft - offset; | ||
|
||
// 判断是否需要滚动 | ||
const shouldScrollY = adjustedTop < 0 || adjustedTop > windowHeight; | ||
const shouldScrollX = adjustedLeft < 0 || adjustedLeft > windowWidth; | ||
|
||
if (shouldScrollY) { | ||
window.scrollTo({ | ||
top: shouldScrollY? adjustedTop : scrollTop, | ||
behavior: 'smooth' | ||
}); | ||
} | ||
|
||
if (shouldScrollX) { | ||
window.scrollTo({ | ||
left: shouldScrollX? adjustedLeft : scrollLeft, | ||
behavior: 'smooth' | ||
}); | ||
} | ||
|
||
setIsScrolled(true); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (isScrolled) { | ||
setIsScrolled(false); | ||
} | ||
}, [isScrolled]); | ||
|
||
return { | ||
scrollIntoView, | ||
targetRef | ||
}; | ||
}; | ||
|
||
export default useScrollIntoView; | ||
|
||
// const { scrollIntoView, targetRef } = useScrollIntoView({ | ||
// offset: 60 | ||
// }); | ||
|
||
// return ( | ||
// <div> | ||
// <div ref={targetRef}>这是要滚动到的目标元素</div> | ||
// <button onClick={scrollIntoView}>滚动到目标元素</button> | ||
// </div> | ||
// ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useWindowScroll = () => { | ||
// 用于存储窗口滚动位置的状态 | ||
const [scroll, setScroll] = useState({ | ||
x: window.pageXOffset, | ||
y: window.pageYOffset | ||
}); | ||
|
||
// 用于更新滚动位置状态的函数 | ||
const handleScroll = () => { | ||
setScroll({ | ||
x: window.pageXOffset, | ||
y: window.pageYOffset | ||
}); | ||
}; | ||
|
||
// 在组件挂载时添加滚动事件监听器,在组件卸载时移除监听器 | ||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
|
||
// 返回滚动位置对象以及设置滚动位置的方法 | ||
return { | ||
scroll, | ||
setScroll: (newScroll) => { | ||
window.scrollTo(newScroll.x, newScroll.y); | ||
setScroll(newScroll); | ||
} | ||
}; | ||
}; | ||
|
||
export default useWindowScroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
import styles from './index.module.less' | ||
const SlideLinear = ({children}) => { | ||
return ( | ||
<div className={styles.slider}> | ||
<div className={styles.content}> | ||
{children} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default SlideLinear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.slider { | ||
overflow: hidden; | ||
width: 100%; | ||
position: relative; | ||
} | ||
|
||
.content { | ||
display: flex; | ||
white-space: nowrap; | ||
animation: 90s linear 0s infinite normal none running slide-right; | ||
} | ||
|
||
@keyframes slide-left { | ||
0% { | ||
transform: translate(0); | ||
} | ||
|
||
50% { | ||
transform: translate(-100%); | ||
} | ||
100% { | ||
transform: translate(0); | ||
} | ||
} | ||
@keyframes slide-right { | ||
0% { | ||
transform: translate(0); | ||
} | ||
|
||
50% { | ||
transform: translate(100%); | ||
} | ||
100% { | ||
transform: translate(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters