Open jjy9331 opened 1 year ago
GPT example
import React, { useRef, useEffect, useState } from "react";
import Typewriter from "typewriter-effect";
export default function ScrollTypewriter() {
const [scrollY, setScrollY] = useState(0);
const typewriterRef = useRef(null);
useEffect(() => {
const handleScroll = () => {
setScrollY(window.scrollY);
};
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
useEffect(() => {
const { current: typewriterElement } = typewriterRef;
if (scrollY > 500 && scrollY <= 1000) {
// Start the typewriter animation when scrollY is within the specified range
const typewriter = new Typewriter(typewriterElement, {
strings: ["Hello", "World"],
autoStart: true,
delay: 50,
});
}
}, [scrollY]);
return (
<div>
<div style={{ height: "1500px" }}></div> {/* Space for scrolling */}
<div ref={typewriterRef}></div> {/* Element where the typewriter effect will be applied */}
</div>
);
}
GPT example
import React, { useRef, useEffect, useState } from "react"; import Typewriter from "typewriter-effect"; export default function ScrollTypewriter() { const [scrollY, setScrollY] = useState(0); const typewriterRef = useRef(null); useEffect(() => { const handleScroll = () => { setScrollY(window.scrollY); }; window.addEventListener("scroll", handleScroll); return () => { window.removeEventListener("scroll", handleScroll); }; }, []); useEffect(() => { const { current: typewriterElement } = typewriterRef; if (scrollY > 500 && scrollY <= 1000) { // Start the typewriter animation when scrollY is within the specified range const typewriter = new Typewriter(typewriterElement, { strings: ["Hello", "World"], autoStart: true, delay: 50, }); } }, [scrollY]); return ( <div> <div style={{ height: "1500px" }}></div> {/* Space for scrolling */} <div ref={typewriterRef}></div> {/* Element where the typewriter effect will be applied */} </div> ); }
not working, we have to change to html tag like this
Ref