GithubHelp home page GithubHelp logo

bespoyasov / scroller Goto Github PK

View Code? Open in Web Editor NEW
117.0 4.0 5.0 2.94 MB

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

License: MIT License

HTML 32.38% JavaScript 58.96% CSS 8.66%
scroller javascript dependency-free ui component drag-and-drop

scroller's Introduction

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:

scroller's People

Contributors

beraliv avatar bespoyasov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

scroller's Issues

Refresh the code

It would be great to refresh the Scroller's code. Main goals are:

  • Get rid of polyfills;
  • Update build target / remove build altogether;
  • Use CSS custom properties for updating styles (?);
  • Improve semantics, use list element (?);
  • Remove build directory from repo;
  • Cleanup dev directory, extract the code into src;
  • Upgrade gulp for development purposes.

As a followup, would be cool to upgrade the React component and the Vue component.

Add TS annotations

There is no need to re-write the whole script in TypeScript but for TypeScript support, it would be great to add .d.ts annotations.

package.json

without it this library is hard to implement in the wild

Update docs

  • Actualize Readme;
  • Add Changelog;
  • Add example GIF;
  • Add link to more Scroller tests in various UI conditions.

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.