beginner here, developing a website with a friend who cannot help me at the moment, and, I tried to add a function for background to fade-in on scrolling the site. I tried a lot by reading on web, but can’t figure out a way that would work for me, this is his/the code:
const TopbarContainer = ({ children }: { children: ReactNode }) => (
<div className="sticky top-0 bg-orange backdrop-blur-md">
{children}
</div>
);
coding in typescript, tailwind, React.
tried quite a lot, please suggest me with the best function that would work with it using the following effect;
window.addEventListener(‘scroll’,
2
Answers
this code helps, but, I want it to change the opacity of the background only. Could it be possible?
To add a function for background opacity to change, you can use the
useRef
hook and theuseState
hook.First, use the
useRef
hook to create a reference to the element whose background opacity you want to change. Then, use theuseState
hook to store the current opacity value.To change the opacity value, you can use the
setState
function to set the new opacity value. Then, you can use theref
object to access the element and set theopacity
property to the new value.Here is an example of how to do this:
Now, you can use the
handleOpacityChange
function to change the opacity of the top bar container. For example, you could create a button that calls the function with a new opacity value:You can also use the
useState
hook to create a slider that allows the user to adjust the opacity value.