1.1.5 • Published 1 year ago

@toon.rombaut/ez-smooth-scroll v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

EzSmoothScroll

Lightweight library to add smooth scrolling to your website. Fixes problems with appearing/disappearing browsers UI (ex. url section on Chrome/Safari).

Features

  • Lightweight
  • No dependencies
  • Works on mobile browsers. No more problems with appearing/disappearing browser UI!
  • Use your own external RAF loop if needed
  • Auto resize

Demo

EzSmoothScroll demo.

Installation

npm

npm i @toon.rombaut/EzSmoothScroll

yarn

yarn add @toon.rombaut/EzSmoothScroll

Usage

  1. Add the class scroll-content to the element that will contain your content.
  2. Add the class scroll-container to the parent element of the above
<body>
    <div class="scroll-container">
        <div class="scroll-content">
            <!--Your content here -->
        </div>
    </div>
</body>
  1. Import and initiaze in javascript
//import
import {EzSmoothScroll} from "@toon.rombaut/EzSmoothScroll";

//Get your html elements
const container = document.querySelector(".scroll-container");
const scrollContent = document.querySelector(".scroll-content");

//Initialize
const ezSmoothScroll = new EzSmoothScroll({
    container: container,
    scrollContent: scrollContent,
    ease: 0.75,
    disableRaf: false
});
  1. If you don't need the smooth scrolling anymore, or you want to re-initialize, don't forget to use the destroy method on the current instance
ezSmoothScroll.destroy();

Options

ParameterTypeRequiredDefaultDescription
containerHTMLElementYes/The scroll container HTML element. Parent of the scrollContent HTML element.
scrollContentHTMLElementYes/The HTML element that contains your HTML.
easeNumberNo0.75The ease amount for the transform lerp.
disableRafBooleanNofalseDisable the internal RAF loop to use your own external RAF loop. Can be used to increase performance.

Properties

Properties that can be read from the EzSmoothScroll instance. Setting these properties is possible but not recommended.

PropertyTypeDescription
containerHTMLElementThe current scroll container HTML element.
scrollContentHTMLElementThe current HTML element that contains your HTML.
easeNumberThe current ease amount for the transform lerp.
scrollToRenderNumberThe currently rendered scroll position in px.
scrollTargetNumberThe target scroll position in px.
speedNumberThe current scrollspeed. Can be used for animations.
speedTargetNumberThe speedtarget. Can be used for animations.

Methods

Methods that can be executed on the EzSmoothScroll instance.

MethodParametersDescription
setSize()/Manually resize the smooth scroll.
update()/Manually update the scroll render. Useful if you use your own external RAF.
scrollTo(target)target - NumberTriggers scroll to the passed target scroll position.
destroy/Destroy the eventlisteners inside the instance. Important to execute if you want to re-initialize or to increase performance when you don't need the instance anymore.

External RAF

You can use your own external RAF loop.

//import
import {EzSmoothScroll} from "@toon.rombaut/EzSmoothScroll";

//Get your html elements
const container = document.querySelector(".scroll-container");
const scrollContent = document.querySelector(".scroll-content");

//Initialize
const ezSmoothScroll = new EzSmoothScroll({
    container: container,
    scrollContent: scrollContent,
    ease: 0.75,
    disableRaf: true
});

//render function,
render = () => {
    ezSmoothScroll.update();
    //do you other stuff here

    requestAnimationFrame(render);
}
//kickstart your own external RAF loop
render();

Support

I appreciate all feedback and comments.

Socials

Instagram: @toon.rombaut Email: toon.rombaut@hotmail.com

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

0.0.1-beta.2

1 year ago

0.0.1-beta.1

1 year ago

0.0.1-beta.0

1 year ago