• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast, light-weight (4KB gzip), and dependency-free content scroller.

Scroller

Fast, light-weight (4KB gzip), and dependency-free content scroller.

example.mp4

For more examples of how it works with different settings and UI conditions, check out this link.

Installation

Install Scroller with npm:

npm install prokrutchik

Add the scripts and styles to your project:

import { Scroller } from "prokrutchik";
import "prokrutchik/styles.css";

Or add them directly to the HTML file:

<script src="/path/to/prokrutchik/browser.js"></script>
<link href="/path/to/prokrutchik/styles.css" rel="stylesheet" />

Initialization

By default, Scroller provides auto initialization for all the .scroller elements on the page:

<ul class="scroller">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ul>

You can also initialize the instance manually:

<ul class="foo">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ul>

<script type="module">
  import { Scroller } from "prokrutchik";

  const instance = new Scroller({
    element: document.querySelector(".foo"),
  });
</script>

Configuration

You can configure Scroller via data- attributes in HTML:

  • data-navigation, shows/hides the navigation buttons, "visible" | "hidden";
  • data-scrollbar, shows/hides the scrollbar under the content, "visible" | "hidden";
  • data-align, specifies how to align the content if it fits the screen, "start" | "center" | "end";
  • data-start-position, initial position of the content, number of px | "start" | "center" | "end";
  • data-start-duration, starting animation duration, number of milliseconds.

Scroller items can be configured with:

  • data-anchor, label of the item in the navigation, string;
  • data-focused, if defined, Scroller will scroll to this item at the start.

Using JavaScript

You can also configure Scroller using the config object:

const scroller = new Scroller({
  element: document.querySelector(".foo"),

  // Show/hide the scrollbar, "visible" | "hidden":
  scrollbar: "hidden",

  // Show/hide the navigation, "visible" | "hidden":
  navigation: "hidden",

  // How to align content if it fits the screen, "start" | "center" | "end":
  align: "center",

  // Initial scroller content position, "start" | "center" | "end" | number of px:
  startPosition: "center",

  // Starting animation duration, number of milliseconds:
  startDuration: 500,
});

Public API

Scroller provides API for changing current position, handling item clicks, and dynamically updating the config.

Position Change

For position change, use the scrollTo method:

// Scrolls to the beginning of the content
scroller.scrollTo("start");

// Scrolls to the center of the content:
scroller.scrollTo("center");

// Scrolls to the end of the content:
scroller.scrollTo("end");

// Scrolls to 100px from the start of the content:
scroller.scrollTo(100);

// Second optional parameter specifies
// the animation duration in milliseconds.
// Scrolls to center in 500 ms:
scroller.scrollTo("center", 500);

Item Click Callback

For handling clicks on items, specify the onItemClick handler in the config:

const scroller = new Scroller({
  element: document.querySelector(".foo"),
  onItemClick: (event) => {
    /* The `event` argument here is `TouchEvent` or `MouseEvent` depending on the user device. */
  },
});

Config Updates

For configuration updates, use the update method:

scroller.update({
  scrollbar: "hidden",
  navigation: "hidden",
  align: "center",
  onItemClick: someFunc,
});

Examples

Scroller with disabled scrollbar, active navigation, and start alignment, configured via HTML data-attributes:

<head>
  <link href="/path/to/prokrutchik/styles.css" rel="stylesheet" />
  <script src="/path/to/prokrutchik/browser.js" defer></script>
</head>

<body>
  <ul class="scroller" data-scrollbar="hidden" data-align="start">
    <img src="example.png" data-anchor="anchor1" />
    <div data-anchor="anchor2"></div>
    <table data-anchor="anchor3"></table>
    <!-- ... -->
  </div>
</body>

Scroller with the same settings configured via config object:

import { Scroller } from "prokrutchik";
import "prokrutchik/styles.css";

const myScroller = new Scroller({
  el: document.querySelector(".foo"),
  scrollbar: "hidden",
  align: "start",
});

Components

If you want to use Scroller with React or Vue, check out these packages:

More Repositories

1

frontend-clean-architecture

React + TypeScript app built using the clean architecture principles in a more functional way.
TypeScript
2,276
star
2

refactor-like-a-superhero

How to refactor code efficiently and without pain.
945
star
3

solidbook

Book about the SOLID principles and object-oriented software design.
MDX
599
star
4

tools

Список полезных сервисов для разработчиков.
131
star
5

front-not-pain

How to work with ease and joy. Uplifting guide for frontend developers.
HTML
118
star
6

explicit-design

Source code for the “Explicit Design” post series.
TypeScript
91
star
7

www

Source code for my blog.
Svelte
81
star
8

treees

Trees images generator based on L-Systems made with TypeScript using OOP principles.
TypeScript
52
star
9

ttt-tdd

Book about test-driven development with an example of making “Tic-Tac-Toe” by TDD.
HTML
31
star
10

refactor-like-a-superhero-talk

Source code and examples for “Refactor Like a Superhero” talk.
JavaScript
29
star
11

text-generator

A naive text generator built in JavaScript using Markov chains.
JavaScript
26
star
12

testing-workshop

Source code samples for the workshop about testing React applications.
JavaScript
23
star
13

you-really-dont-need-redux-now

Redux vs Context API + Hooks comparison article.
JavaScript
20
star
14

react-scroller

React component wrapper for Scroller.
JavaScript
20
star
15

di-ts-in-practice

A sample app for a post about dependency injection with TypeScript.
TypeScript
18
star
16

rule-based-data-validation

Declarative data validation made with rule-based approach and functional programming.
TypeScript
17
star
17

fsm-example

Finite state machine example written in vanilla Javascript.
JavaScript
15
star
18

tmstmp

Converts timestamps to dates and backwards.
TypeScript
13
star
19

vue-scroller

Vue component wrapper for Scroller.
Vue
7
star
20

web-worker-example

Using Web Workers to Boost Performance.
CSS
7
star
21

binary-full-adder-in-the-game-of-life

Binary adder implementation in the Game of Life written in JavaScript using canvas.
JavaScript
6
star
22

bespoyasov

Hello · Hallå · Привет!
5
star
23

utils

Javascript helper-functions
JavaScript
5
star
24

kursovik-redux-electron

App showing $/₽ exchange
JavaScript
3
star
25

traktor-html-css-workshop

Source code samples for the HTML & CSS workshop at Traktor School.
HTML
2
star
26

clickme

“Click me!“ implementation made with RxJS.
TypeScript
2
star
27

react-piano-next

A virtual piano keyboard built with React and deployed with Vercel (former Next).
TypeScript
2
star
28

next-app-deployment

The example Next app, that we're going to deploy on Vercel, Heroku and a custom static server.
TypeScript
2
star
29

shitty-advices

HTML
1
star
30

kursovik-rn-expo

Rebuilt the Kursovik app using React Native and Expo because why not.
JavaScript
1
star
31

loose-equals

JavaScript explicit non-strict equality comparator.
JavaScript
1
star
32

wesbos-advanced-react-rerecorded

Let's relearn this stuff.
JavaScript
1
star
33

wesbos-node-course

Node course
JavaScript
1
star
34

bookmark

Crossbrowser aside anchor-navigation
JavaScript
1
star
35

unv-proj

University graduation project
Python
1
star
36

teaching-as-a-negotiation-skill

Слайды к докладу
HTML
1
star
37

figma-tags-finder

A missing tags manager for Figma
TypeScript
1
star
38

morse

Text to Morse code translator.
JavaScript
1
star