Jump to content
Search Community

positioning img group on center

kasiuleckretulec test
Moderator Tag

Recommended Posts

Hello, I have a problem with component positioning: whenever hover is active, the images are displayed at the top of the page, not in the component area. How to deal with this? Should property "top", "left" be applied to a specific div with specific units?

 

 

const scaleAnimation = {
  initial: { scale: 0, x: "-50%", y: "-50%" },
  enter: {
    scale: 1,
    x: "-50%",
    y: "-50%",
    transition: { duration: 0.4, ease: [0.76, 0, 0.24, 1] },
  },
 
  closed: {
    scale: 0,
    x: "-50%",
    y: "-50%",
    transition: { duration: 0.4, ease: [0.32, 0, 0.67, 0] },
  },
};
 
const Modal = ({ modal, portfolioLinks }) => {
  const { active, index } = modal;
 
  const container = useRef(null);
 
  useEffect(() => {
    const moveContainerX = gsap.quickTo(container.current, "left", {
      duration: 0.8,
      ease: "power3",
    });
    const moveContainerY = gsap.quickTo(container.current, "top", {
      duration: 0.8,
      ease: "power3",
    });
 
    window.addEventListener("mousemove", (e) => {
      const { clientX, clientY } = e;
      moveContainerX(clientX);
      moveContainerY(clientY);
    });
  });
 
  return (
    <motion.div
      ref={container}
      variants={scaleAnimation}
      initial={"initial"}
      animate={active ? "enter" : "closed"}
      className="modalContainer"
    >
      <div style={{ top: index * -100 + "%" }} className="modalSlider">
        {portfolioLinks.map((project, index) => {
          const { src, color } = project;
          return (
            <div
              key={index}
              style={{ backgroundColor: color }}
              className="modal"
            >
              <img
                src={src}
                width={300}
                height={0}
                alt="image"
                className="modalImg"
              ></img>
            </div>
          );
        })}
      </div>
    </motion.div>
  );
};
 
export default Modal;
Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi,

 

Besides echoing the need of a minimal demo I can notice that you are using React but you are not using GSAP Context. When working with React/Next, always use GSAP Context:

https://gsap.com/resources/React

 

Also you are not passing a dependencies array to your effect hook, that means that the code inside of it will execute on every re-render of that component:

useLayoutEffect(() => {
  const ctx = gsap.context();
  
  return () => ctx.revert(); // <- Cleanup!!
}, []);// this runs only when the component mounts

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...