Jump to content
Search Community

malfunction smooth scroll, doesn't work on mobile

Anderw test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi there -ย  you had a lot of errors here!

The code inside the smooth scroll function wasn't firing, you weren't including GSAP or scrollTo plugin or jQuery at all. You were using old GSAP syntax and there were closing tags missing in your links and for your scroll container.

I've adjusted it for you to give you a better starting point.

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



For future reference, we're more than happy to help with GSAP related questions, but please make sure the demo provided is thoroughly checked for other issues. console.log() is your friend when debugging!

Link to comment
Share on other sites

Hi,

ย 

Once again you provided a non-working example. Your jquery ready function is not working:

2 hours ago, Cassie said:

console.log() is your friend when debugging!

Just remove all of that and this works:

const links = gsap.utils.toArray(".header__menu a");

links.forEach((link) => {
  link.addEventListener("click", (e) => {
    e.preventDefault();
    const href = e.target.getAttribute("href");
    const target = href === "#" ? 0 : href;
    gsap.to(window, {
      duration: 1,
      ease: "power3.out",
      scrollTo: target,
    })
  });
});

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

  • Solution

Hi,

ย 

This is just a JS logic issue, not a GSAP related problem. This seems to solve it:

let hamburger = document.querySelector(".header__hamburger");
let isOpen = false;
const menuClickHandler = () => {
  document.body.classList.toggle("menu-open");
  isOpen = !isOpen;
};
hamburger.addEventListener("click", menuClickHandler);


const links = gsap.utils.toArray(".header__menu a");

links.forEach((link) => {
  link.addEventListener("click", (e) => {
    e.preventDefault();
    isOpen && menuClickHandler();
    const href = e.target.getAttribute("href");
    const target = href === "#" ? 0 : href;
    gsap.to(window, {
      duration: 1,
      ease: "power3.out",
      scrollTo: target,
    })
  });
});

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...