GithubHelp home page GithubHelp logo

Comments (9)

omonk avatar omonk commented on June 14, 2024 4

I can have a look at some point! I hacked this together quickly for a small personal project I'm working on but would be happy to contribute for others to use also

from scroll-js.

omonk avatar omonk commented on June 14, 2024 2

I've hacked together some changes for scrollTo to accept x and y scrolling. This just works for the scrollTo function, it can be translated to the other methods but would require a bit more consideration to update all methods etc.

async function scrollTo(el = window, options = {}) {
  const scroll = ({
    from,
    to,
    startTime,
    duration = 400,
    easeFunc,
    resolve,
  }) => {
    window.requestAnimationFrame(() => {
      const currentTime = Date.now();
      const time = Math.min(1, (currentTime - startTime) / duration);

      if (from.top === to.top && from.left === to.left) {
        return resolve ? resolve() : null;
      }

      setScrollPosition(el, {
        top: easeFunc(time) * (to.top - from.top) + from.top,
        left: easeFunc(time) * (to.left - from.left) + from.left,
      });

      /* prevent scrolling, if already there, or at end */
      if (time < 1) {
        scroll({ from, to, startTime, duration, easeFunc, resolve });
      } else if (resolve) {
        resolve();
      }
    });
  };

  const currentScrollPosition = getScrollPosition(el);

  return new Promise(resolve => {
    scroll({
      from: currentScrollPosition,
      to: { top: options.top, left: options.left },
      startTime: Date.now(),
      duration: options.duration,
      easeFunc: t => {
        return t * (2 - t);
      },
      resolve,
    });
  });
}

function getScrollPosition(el) {
  if (
    el === document.body ||
    el === document.documentElement ||
    el instanceof Window
  ) {
    return {
      top: document.body.scrollTop || document.documentElement.scrollTop,
      left: document.body.scrollLeft || document.documentElement.scrollLeft,
    };
  } else {
    return {
      top: el.scrollTop || el.scrollTop,
      left: el.scrollLeft || el.scrollLeft,
    };
  }
}

function setScrollPosition(el, { top, left }) {
  if (
    el === document.body ||
    el === document.documentElement ||
    el instanceof Window
  ) {
    document.body.scrollTop = top;
    document.documentElement.scrollTop = top;

    document.body.scrollLeft = left;
    document.documentElement.scrollLeft = left;
  } else {
    el.scrollTop = top;
    el.scrollLeft = left;
  }
}

from scroll-js.

svensson-david avatar svensson-david commented on June 14, 2024 1

No worries! Needed it for a Netflix type navigation with a lot of titles being shown horizontally. Trying to get it working with the the browser native scrolling behavior: smooth for now but I'll revisit this if we cannot get it to work reliably.

We're already using this library for some other scrolling and it's been really great this far 😃
Thanks!

from scroll-js.

jvgreenaway avatar jvgreenaway commented on June 14, 2024

@mkay581 isn't the first param the X scroll?

from scroll-js.

markcellus avatar markcellus commented on June 14, 2024

The first param to Scrolls .to() method is x, which is the horizontal scroll on the x axis, yes. But its not currently being used. I made the decision to include it so that future horizontal support will be easy. the API has since been changed. See updated README.

from scroll-js.

svensson-david avatar svensson-david commented on June 14, 2024

Is horizontal scroll still not supported?

from scroll-js.

markcellus avatar markcellus commented on June 14, 2024

No it isn't unfortunately. 😞 Just haven't had time to get to it. But do a lot of people need this? Horizontal scrolling seems rare when compared to vertical scrolling and haven't really seen this ticket gain as much popularity. I am open to accepting PRs though if you think its something you can tackle.

from scroll-js.

markcellus avatar markcellus commented on June 14, 2024

Awesome @omonk! You mind submitting a PR? So I can better evaluate, run tests, etc?

from scroll-js.

brandonmcconnell avatar brandonmcconnell commented on June 14, 2024

Would also be excited to see this added ☝🏼 @markcellus @omonk

from scroll-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.