// src/components/Services.js import React, { useState, useEffect } from 'react'; import…
- User
// src/components/Services.js import React, { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { useScroll, useTransform } from 'framer-motion'; import OnScreen from '../hooks/OnScreen';
/section#services .services-section { position: relative; width: 100%; overflow: hidden; } section#services .services-section .overlay { z-index: 2; } section#services .services-section .underlay { position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: -1; display: flex; justify-content: center; align-items: center; } section#services .services-section .overlay img, section#services .services-section .underlay img { width: 100%; height: 100%; object-fit: cover; }/
const imagesUnderlay = [
${process.env.PUBLIC_URL}/img/agency-05a1.png,${process.env.PUBLIC_URL}/img/agency-05a2.png,${process.env.PUBLIC_URL}/img/agency-05a3.png, ];const Services = () => {
codeconst servicesImage = `${process.env.PUBLIC_URL}/img/agency-05a.png`; const [currentImg, setCurrentImg] = useState(0); const [ref, isVisible] = OnScreen(); const { scrollY } = useScroll(); // Transform scroll Y to change the index of the image to display const imageIndex = useTransform(scrollY, [0, window.innerHeight], [0, imagesUnderlay.length]); useEffect(() => { return imageIndex.onChange((latest) => { if (isVisible) { const newIndex = Math.round(latest); // Prevent going beyond the last index setCurrentImg(Math.min(newIndex, imagesUnderlay.length - 1)); } }); }, [imageIndex, isVisible]);// add isVisible to dependencies to ensure the effect runs when visibility changes return ( <div className="container-fluid services-section" ref={ref}> <div class="overlay"> <img src={servicesImage} alt="services" class="img-fluid" /> </div> <div class="underlay"> <motion.img src={imagesUnderlay[currentImg]} alt={`image ${currentImg + 1}`} initial={{ opacity: 1 }} animate={{ opacity: 1 }} transition={{ duration: 0.8, ease: "easeInOut" }} /> </div> </div> ); - Assistant
- User
using your code:
import React, { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { useScroll, useTransform } from 'framer-motion'; import OnScreen from '../hooks/OnScreen';
- Assistant
- User
style={{ position: 'absolute', top: 0, left: 0, right: 0, bottom: 0 }}
- Assistant