Giter Site home page Giter Site logo

onscroll's Introduction

Onscroll

Makes your web page dance on scroll

Dependency Status devDependency Status

Introduction

Many web pages these days produce the "wow effect" by triggering transitions and animations based on scroll position. Though it might look good, it may require massive coding, plus - naive implementation can affect performance and lead to a bad user experience.
With page performance in mind, Onscroll can help you achieve this behaviour with a single command.

Installation:

Bower: bower install onscroll --save

Usage:

  1. Add a reference to Onscroll library to your web page:
    <script src='bower_components/onscroll/dist/onscroll.min.js'></script>
  2. Create a new Onscroll instance:
let obj = new Onscroll({
 // {string} CSS3 selector or multiple comma separated selectors: 
 selector: '.first-selector, #second-selector',
   
 // {number} horizontal scroll (X axis) position in pixels from which to apply the actions
 // or
 // {from: {number}, to: {number}} horizontal scroll (X axis) min & max positions in pixels to apply the actions: 
 left: 20,
 //left: {
 //  from: 20,
 //  to: 100
 //},
 
 // {number} vertical scroll (Y axis) position in pixels from which to apply the actions
 // or
 // {from: {number}, to: {number}} vertical scroll (Y axis) min & max positions in pixels to apply the actions: 
 top: 20,
 //top: {
 //  from: 20,
 //  to: 100
 //},
 
 // {string} limit manipulation to specific scrolling direction (north, south, west, east)
 // or
 // [{string}] multiple directions with OR relation between
 direction: 'south',
 //direction: ['south-west', 'west'],

 // add one or more class names:
 addClass: 'gold',
 //addClass: ['yellow', 'bold'],
   
 // remove one or more class names:
 removeClass: 'center',
 //removeClass: ['opacity', 'double-padding'],
   
 // set inline style:
 setStyle: {
   transform: function(position/*, direction*/) {
     return `translateY(${20 - position.top}px)`;
   },
   paddingLeft: function(left/*, top*/) {
     return `${left}px`;
   }
 },
   
 // call a function:
 callFunction: function(/*position, direction*/) {
   this.revealPopup();
 }
});

Basic

Use addClass and removeClass when you want to set or unset CSS properties with fixed values. This is done by adding or removing class name(s) to element(s).

new Onscroll({
  selector: '.some-element',
  top: 20,
  // value can be either string in case of a single class name to add:
  addClass: 'gold'
  // or an array in case of multiple class names to add:
  //addClass: ['gold', 'double-spacing'],
  // to remove one or more class names, use the -removeClass- action:
  //removeClass: 'gold'
});

Moderate

Use setStyle when you want to set or unset CSS properties with dynamic values, based on the scroll position. This is done by setting inline style to elements. Each property in object is a CSS property and its value is a function with two arguments, position (object which contains current horizontal (left) and vertical (top) scroll position) and direction (object which contains scroll direction), which can be used for calculating the function's return value - the value of CSS property.

new Onscroll({
  selector: '.some-element',
  top: 20,
  setStyle: {
    transform: function(position/*, direction*/) {
      return `translateY(${20 - position.top}px)`;
    },
    paddingLeft: function(position/*, direction*/) {
      return `${position.left}px`;
    }
  }
});

Advance

Use callFunction when you not necessarily want to manipulate style, but rather call an existing function, execute JavaScript etc. The action's value is a function with two arguments, position (object which contains current horizontal (left) and vertical (top) scroll position) and direction (object which contains scroll direction). Function should not return any value.

new Onscroll({
  selector: '.some-element',
  top: 20,
  callFunction: function (position, direction) {
    if (left > 100 && direction.north === true) {
      submitForm();          
    }
  }
});

More Commands

  • obj.disable() - disables instance.
  • obj.enable() - enables instance.

onscroll's People

Contributors

nire0510 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.