Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - How to remove / reload locomotive scroll on window resize?

I have a website I'm building that uses a horizontal layout on desktop and switches back to a native vertical layout on smaller screens. I'm using locomotive scroll, which is working great, but I can't seem to get the window resizing down.

Here is the function for large screens

 const lscroll = new LocomotiveScroll({
    el: document.querySelector('[data-scroll-container]'),
    smooth: true,
    direction: 'horizontal'
});

and on window.resize events, if the width goes below the mobile threshold, I tried to just destroy it and call it again, but with a direction of "vertical" instead of "horizontal".

 const lscroll = new LocomotiveScroll({
   el: document.querySelector('[data-scroll-container]'),
   smooth: true,
   direction: 'vertical'
 });
 lscroll.destroy();
 lscroll.init();

Any ideas?

question from:https://stackoverflow.com/questions/65836059/how-to-remove-reload-locomotive-scroll-on-window-resize

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I never used LocomotiveScroll, but it's strange example code you provided.

You assign vertical LS to lscroll, then you destroy it (vertical LS) and then init again.

You might want to use something like:

let lscroll = new LocomotiveScroll(...);

const handleResize = () => {
  const desiredType = ... // 'horizontal' or 'vertical'
  if (lscroll.direction !=== desiredType) {
    lscroll.destroy()
    lscroll = new LocomotiveScroll(...);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...