Git Product home page Git Product logo

scrollspy's Introduction

Scroll progress (dual-side-scroll) v1.2.3

npm npm npm bundle size GitHub license

Assignment

This tiny plugin is designed to show the progress of the page scrolling interactively. There are two types of actions: onScrolled and onChanged. They can be used together or separately.

  • onScrolled - returns to the progress for each scroll event. It is convenient to use when you need to display progress in real time.
  • onChanged - works only when one paragraph is replaced to another. This is useful for switching the active menu item in the navigation block.

LiveDemo

How does it work?

All you need is a navigation menu with links to the relevant paragraphs in the text. When initializing an object, you need to specify cursor and menu selectors, as well as a callback function.

When the onScrolled event is used, an object will be sent to the processing unit:

    Progress {
        // id of current paragraph
        Id: string;
        // % of paragraph being reviewed
        Percent: number;
    }

When the onChanged event is used, the id of the current paragraph will be sent to the processing unit.

When the window is changed in size, the script automatically will be adjuscted to the proportional value. This is sometimes needed in the mobile version. When the device screen is rotated, the scrolling will continue to work correctly.

Installation

npm i dual-side-scroll

Take the minified version of the script from dist and place it in your application directory. Make sure the plugin connection string is located above the connection string of your scripts.

<script src="./<your_js_directory>/scroll-progress.min.js"></script>

For debugging purposes, there is an unminified version with sourcemap.

Example

The source code of the page LiveDemo script.

document.addEventListener("DOMContentLoaded", function (event) {
    let currentParagraphName = document.getElementById('current-paragraph-name');
    let currentParagraphPercent = document.getElementById('current-paragraph-percent');

    new ScrollProgress.Init(
        "#cursor",
        "menu",
        progress => {
            currentParagraphName.innerText = document.getElementById(progress.Id).innerText;
            currentParagraphPercent.innerText = progress.Percent + '%';
        },
        id => {
            document.querySelectorAll('a[href*="link"]')
                .forEach(element => 
                    element.classList.remove('active-meny-item')
                );
            document.querySelector(`[href="#${id}"]`).classList
                .add('active-meny-item');
        }
    );
});

Planned updates

  • Interactive cursor to scroll the page
  • Bookmarks in paragraphs

scrollspy's People

Contributors

eabrega avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

spilbertina

scrollspy's Issues

Scroll based on top

Hello, the scroll is based on top of the window, instead of the bottom. Can you help me to configure that? Im having trouble finding it in the script.

Add onChanged event

Для "ступенчатого" переключения пунктов меню.

Children element not worked.

Hi.
I'm try do that:

<ul>
    <li><a href="#link1">wwww 1</a></li>
    <li><a href="#link2">wwww 2</a></li>
    <li><a href="#link3">wwww 3</a></li>
    <li><a href="#link4">wwww 4</a></li>
</ul>

And get this error:

Uncaught Error: Menu item 'wwww 1
wwww 2
wwww 3
wwww 4' has not link to paragraph.
at menu.ts:55
at Array.map ()
at Menu.MapToMenuItems (menu.ts:53)
at new Menu (menu.ts:21)
at new Page (page.ts:29)
at new Init (scroll-init.ts:27)
at HTMLDocument. (app.js:18)

Thank you.

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.