Coder Social home page Coder Social logo

phucbm / jquery-scroll-direction-plugin Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 55 KB

↕️ A lightweight jQuery plugin to detect scroll direction.

Home Page: https://scroll-direction.netlify.app

License: MIT License

JavaScript 53.58% HTML 46.42%
jquery scroll scrolling scroll-direction locomotive-scroll

jquery-scroll-direction-plugin's Introduction

Scroll Direction

Scroll Direction

A lightweight Javascript plugin to detect scroll direction on your website.

Update: Scroll Direction now works with other libraries that hijack the native scrollbar (like Locomotive Scroll).

Getting started

Self-hosting

You can download the plugin directly from Github .

<script src="/jquery.scroll-direction.js"></script>

Using CDN

You can also browse for the latest version by visiting Scroll Direction on jsDelivr

<!-- Scroll Direction - v2.0.0 -->
<script src="https://cdn.jsdelivr.net/gh/phucbm/[email protected]/jquery.scroll-direction.js"></script>

or minified version

<!-- Scroll Direction - v2.0.0 -->
<script src="https://cdn.jsdelivr.net/gh/phucbm/[email protected]/jquery.scroll-direction.min.js"></script>

Initialize Scroll Direction

After init, you will have some classes on your body tag to indicate the scroll direction and position.

// jQuery
$.scrollDirection.init();

// Pure JS
window.scrollDirection.init();
<body class="scroll-top scroll-up"></body>

Integrate with Locomotive

Set the hijacking:true so the plugin will let you use custom event to calculate scrolling info.

// init Locomotive
let scroller = new LocomotiveScroll();

// init Scroll Direction
$.scrollDirection.init({
    hijacking: true
});

// update Scroll Direction on Locomotive scroll event
scroller.on('scroll', function(obj){
    $.scrollDirection.update({
        scrollAmount: () => obj.scroll.y,
        maxScrollAmount: () => obj.limit.y,
    });
});

Usage

Init

$.scrollDirection.init({
    // options
});
Option Type Default Description
topOffset function return number () => 0 Height of top zone in pixel.
bottomOffset function return number () => 0 Height of bottom zone in pixel.
atBottomIsAtMiddle boolean true Consider bottom zone is also middle zone.
indicator boolean true Turn indicator on/off.
indicatorElement jQuery element $("body") Add indicator classes to this element.
scrollUpClass string "scroll-up" Class when scrolling up.
scrollDownClass string "scroll-down" Class when scrolling down.
scrollAtTopClass string "scroll-top" Class when at top zone.
scrollAtMiddleClass string "scroll-middle" Class when at middle zone.
scrollAtBottomClass string "scroll-bottom" Class when at bottom zone.
extraIndicators object {"element": $("#element"),"class": "element-is-viewed",} Add a class to indicatorElement when scroll pass the element
scrollAmount function return number () => $(window).scrollTop() The instance scroll amount of window.
maxScrollAmount function return number () => $(document).height() - $(window).height() Maximum scroll amount.
hijacking boolean false Disable update on window scroll event to use custom event.

Update

// jQuery
$.scrollDirection.update({
    // update new options
});

// Pure JS
window.scrollDirection.update({
    // update new options
});

Events

// jQuery
// this event runs whenever you load, resize and scroll
$(window).on("scrollDirection", function(){
    // do your job here
});

// when you scroll up
$(window).on("scrollUp", function(){
});

// when you scroll down
$(window).on("scrollDown", function(){
});

// when you at the beginning of the page, you can increase the top zone using topOffset
$(window).on("scrollAtTop", function(){
});

// when you in the middle of the page 
// this means the top and bottom zone are not visible in view port
$(window).on("scrollAtMiddle", function(){
});

// when you touch the end of the page
$(window).on("scrollAtBottom", function(){
});

// Pure JS
document.addEventListener("scrollDirection", () => {
});

APIs

You can also check the current scroll direction/position using these provided APIs.

  • $.scrollDirection.isScrollUp
  • $.scrollDirection.isScrollDown
  • $.scrollDirection.isScrollAtTop
  • $.scrollDirection.isScrollAtMiddle
  • $.scrollDirection.isScrollAtBottom
// jQuery
if($.scrollDirection.isScrollUp){
    // do something
}

// Pure JS
if(window.scrollDirection.isScrollUp){
    // do something
}

Deployment

Install

npm install

Dev server

gulp serve

Release

gulp release

jquery-scroll-direction-plugin's People

Contributors

phucbm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

matt663

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.