0.2.3 • Published 4 years ago

@timroussilhe/smooth v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Smooth

enter image description here

What's Smooth?

Custom library to handle scroll.

There is 3 main things that Smooth can take care of for you :

  • Wrapping a container ( usually the page ) and adding a smooth scroll ( with some lerping/momemtum to it )
    • This can be some by keeping the real scroll and scrollbar for a more native experience
    • Or this can be done using virtual-scroll for a more smooth experience
  • Handling Parallax
    • Smooth will act as a controller to handle parallax
  • Scroll Trigger, to reveal element when they reach a certain

The library works on mobile but I recommend to keep the real scroll on mobile.

Usage

HTML

<!DOCTYPE html>

<html lang="en">
  <head>
    <title>Title</title>
  </head>
  <body>
    <main id="content">
      <section class="page-wrapper">...</section>
    </main>
  </body>
</html>

Javascript

Create a smooth instance

import Smooth from '@timroussilhe/smooth';

this.smooth = new Smooth({
  scrollElement: document.querySelector('.scroll-wrapper'),
  scrollOptions: {
    useVirtualScroll: false,
    smoothContainer: true,
    easing: 0.12,
    friction: null,
  }
});

this.smooth.start();

Add Scroll Elements

const elementParallax = {
  el: document.querySelector('.scroll-wrapper'),

  parallax: [
    {
      start: 0,
      end: 500,
      properties: [['translateY', 0, 250]],
      easing: 'easeInOutCubic',
      viewFactorStart: 0,
      viewFactorEnd: 0,
    },
  ],
};

const $footer = document.querySelector('footer');

const elementTrigger = {
  el: $footer,

  trigger: {
    start: 'in-viewport',
    end: 'out-viewport',
    viewFactorStart: 0,
    viewFactorEnd: 0,
    callback: (before) => {
      if (before === 0) {
        $footer.classList.add('footer--in');
      } else if (before === -1) {
        $footer.classList.remove('footer--in');
      }
    },
  },
};
this.smooth.addElements([elementParallax, elementTrigger]);

Reflow/resize

this.smooth.reset(this.getScrollElement());
this.smooth.resize();

API

Smooth Properties/Options

propertytypedefaultdescription
scrollElementDOM elementnullrequired DOM element for the Scroll wrapper
scrollbarElementDOM elementnulloptional DOM element for the Scrollbar container
scrollOptionsObject{}required Option for the Scroll
scrollbarOptionsObject{}optional Option for the Scroll

Scroll Properties/Options

propertytypedefaultdescription
easingnumber0.5easing value for the scroll
frictionnumber0.2friction value for the scroll
wheelDeltaMultipliernumber0.45Multiplier to add to the wheel delta
maxWheelnumber400Max value that the wheel event delta can get to
autoUpdatebooleantrueLet Scroll update its own requestanimationframe loop
useVirtualScrollbooleantrueUse virtual-scroll or the native scroll
directionstringverticalDirection of the scroll, vertical or horizontal
infinitebooleanfalseInfinite scroll will not stop the Scroll when reaching the limit of the scroll Wrapper.

Scrollbar Properties/Options

propertytypedefaultdescription
orientationstringverticalOrientation and placement of the scrollbar
minSizenumber50Smallest size that the Scrollbar thumb can reach
fadebooleantrueIf the Scrollbar thumb need to fade or not
fadeTimenumber1200Fade Timer duration
classNamestringscrollbar-thumbclassName of the generated Scrollbar
updateScrollTargetfunctionnullFunction to called when the Scrollbar get updated

Smooth Element Option

propertytypedescription
elDOM ElementTarget element
triggerObjectTrigger Item values
parallaxArrayArray of Parallax Item values

Smooth Element Paralaxe

propertytypedescription
startstring | numberstart value for the scroll element. This can be a number or use 'in-viewport' 'out-viewport'
endstring | numberend value for the scroll element. This can be a number or use 'in-viewport' 'out-viewport'
propertiesarrayProperties to animate, each property should be an array following this structure property,valueStart,ValueEnd ex: ['translateY', 0, 200]
viewFactorStartstring | numberfactor to add to the start value, this can be a number that will be used as a factor of the element height or a fixed pixel value. Ex: 200px
viewFactorEndstring | numberfactor to add to the end value, this can be a number that will be used as a factor of the element height or a fixed pixel value. Ex: 200px

Smooth Element Trigger

propertytypedescription
startstring | numberstart value for the scroll element. This can be a number or use 'in-viewport' 'out-viewport'
endstring | numberend value for the scroll element. This can be a number or use 'in-viewport' 'out-viewport'
viewFactorStartstring | numberfactor to add to the start value, this can be a number that will be used as a factor of the element height or a fixed pixel value. Ex: 200px
viewFactorEndstring | numberfactor to add to the end value, this can be a number that will be used as a factor of the element height or a fixed pixel value. Ex: 200px
callbackfunction (scrollPosition) => {}callback function to be called when the trigger start or end scroll values are reached. scrollPosition is -1 for before, 0 for in, 1 for after.
repeatbooleanRepeat the callback function after the first trigger in.

Smooth Method

MethodDescription
startStart Smooth
addElements(elements)Add Scroll Elements. Arguments: elements
reset(elements)Reset Scroll Elements. Usually used on resize
disableDisable Smooth
enableEnable Smooth
scrollTo(target,direct)Set Scroll Position. Arguments: target: scroll position. direct scroll to the target with no animation
resize(target)Resize Smooth. Arguments: target: you can provide a fixed height but this is not necessary
destroy()Destroy Smooth instance and clear events.

Live Examples

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago