Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different options for specific viewport sizes #70

Closed
quantumleap33 opened this issue Dec 23, 2020 · 4 comments
Closed

Different options for specific viewport sizes #70

quantumleap33 opened this issue Dec 23, 2020 · 4 comments

Comments

@quantumleap33
Copy link

There is an example in the documentation regarding different options for specific viewport sizes.

const scrollAnimations = sal();

// Provide new options
scrollAnimations.reset({
  selector: 'animated-element',
  once: true,
});

I am not sure I understand the logic. I want to disable animations on mobile. Is there any way to do it?

@mciastek
Copy link
Owner

mciastek commented Dec 23, 2020

Yes, you can use reset() method. Of course, you should remember about adding a debounce to "resize" event handler. For example:

const animation = sal();

const switchAnimations = () => {
  if (window.innerWidth < 768) {
    animation.reset({
      selector: 'animated-element',
      once: true,
    });
  } else {
    animation.reset({
      // some other options
    });
  }
};

switchAnimations();
window.addEventListener('resize', switchAnimations);

Remember that changing options on the fly can cause some flickering, as elements will receive new styles and animation will be repeated.

@quantumleap33
Copy link
Author

Hi, thanks for getting back, Mirek.

Ok, so this one works from your example (I am using default selector [data-sal])

const animation = sal();

const switchAnimations = () => {
  if (window.innerWidth < 768) {
    animation.reset({
      once: true,
    });
  } else {
    animation.reset({
      once: false,
    });
  }
};

switchAnimations();
window.addEventListener('resize', switchAnimations);

When I modify your code to disable animations for mobile, it doesn't work. I still get the animations when I resize the browser and refresh.

const animation = sal();

const switchAnimations = () => {
  if (window.innerWidth < 768) {
    animation.reset({
      disable: true,
    });
  } else {
    animation.reset({
      disable: false,
    });
  }
};

switchAnimations();
window.addEventListener('resize', switchAnimations);

@mciastek
Copy link
Owner

@quantumleap33 try disabled instead of disable. There's a typo docs, the d is missing 🤦 . Let me update the docs.

@quantumleap33
Copy link
Author

It doesn't work even with the correction. Only this code works for me:

`const animation = sal();

const switchAnimations = () => {
if (window.innerWidth < 768) {
animation.reset();
animation.disable();
} else {
animation.reset();
animation.enable();
}
};

switchAnimations();
window.addEventListener('resize', switchAnimations);`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants