src/index.js
function scrollTo({ container, element, key, options }) {
  // if duration is ero then set it to very small so that we do not divide by zero
  if (options.duration <= 0) options.duration = 0.1;

  // width or height
  const sizeKey = SIZE_KEYS[key];
  // destination measurement
  let to = Math.min(
    element[`offset${key}`] + options[`offset${key}`],
    (container[`scroll${sizeKey}`] - container[`offset${sizeKey}`])
  );
  // if destination is greater than avaialable space then limit to avaialable space
  if (to > container[`scroll${sizeKey}`]) to = container[`scroll${sizeKey}`];
  // if destination is less than zero then make it zero
  if (to < 0) to = 0;
  // amount that needs to be scrolled
  const difference = to - container[`scroll${key}`];
  // amount thats needs to be scrolled every tick
  const perTick = difference / options.duration * options.tick;
  // if we are already scrolled to that point then exit
  if (perTick === 0) return;

  // scroll the amount for one tick
  const doScroll = () => {
    setTimeout(() => {
      // scroll container
      container[`scroll${key}`] = container[`scroll${key}`] + perTick;
      // if we have reached desired position then exit
      if (
        (container[`scroll${key}`] >= to && perTick > 0) ||
        (container[`scroll${key}`] <= to && perTick < 0)
      ) return;
      // else repeat after `TICK` interval
      doScroll();
    }, options.tick);
  };

  doScroll();
}
function scrollTo({ container, element, key, options }) {
  // if duration is ero then set it to very small so that we do not divide by zero
  if (options.duration <= 0) options.duration = 0.1;

  // width or height
  const sizeKey = SIZE_KEYS[key];
  // destination measurement
  let to = Math.min(
    element[`offset${key}`] + options[`offset${key}`],
    (container[`scroll${sizeKey}`] - container[`offset${sizeKey}`])
  );
  // if destination is greater than avaialable space then limit to avaialable space
  if (to > container[`scroll${sizeKey}`]) to = container[`scroll${sizeKey}`];
  // if destination is less than zero then make it zero
  if (to < 0) to = 0;
  // amount that needs to be scrolled
  const difference = to - container[`scroll${key}`];
  // amount thats needs to be scrolled every tick
  const perTick = difference / options.duration * options.tick;
  // if we are already scrolled to that point then exit
  if (perTick === 0) return;

  // scroll the amount for one tick
  const doScroll = () => {
    setTimeout(() => {
      // scroll container
      container[`scroll${key}`] = container[`scroll${key}`] + perTick;
      // if we have reached desired position then exit
      if (
        (container[`scroll${key}`] >= to && perTick > 0) ||
        (container[`scroll${key}`] <= to && perTick < 0)
      ) return;
      // else repeat after `TICK` interval
      doScroll();
    }, options.tick);
  };

  doScroll();
}
function scrollTo ( { container , element , key , options } ) { // if duration is ero then set it to very small so that we do not divide by zero if ( options . duration <= 0 ) options . duration = 0.1 ; // width or height const sizeKey = SIZE_KEYS [ key ] ; // destination measurement let to = Math . min ( element [ ` ${ key } ` ] + options [ ` ${ key } ` ] , ( container [ ` ${ sizeKey } ` ] - container [ ` ${ sizeKey } ` ] ) ) ; // if destination is greater than avaialable space then limit to avaialable space if ( to > container [ ` ${ sizeKey } ` ] ) to = container [ ` ${ sizeKey } ` ] ; // if destination is less than zero then make it zero if ( to < 0 ) to = 0 ; // amount that needs to be scrolled const difference = to - container [ ` ${ key } ` ] ; // amount thats needs to be scrolled every tick const perTick = difference / options . duration * options . tick ; // if we are already scrolled to that point then exit if ( perTick === 0 ) return ; // scroll the amount for one tick const doScroll = ( ) => { setTimeout ( ( ) => { // scroll container container [ ` ${ key } ` ] = container [ ` ${ key } ` ] + perTick ; // if we have reached desired position then exit if ( ( container [ ` ${ key } ` ] >= to && perTick > 0 ) || ( container [ ` ${ key } ` ] <= to && perTick < 0 ) ) return ; // else repeat after `TICK` interval doScroll ( ) ; } , options . tick ) ; } ; doScroll ( ) ; }
function that does the scrolling
function that does the scrolling