Giter VIP home page Giter VIP logo

dynamic-marquee's Introduction

npm version

Dynamic Marquee

A small library for creating marquees.

Features:

  • You can change the rate on the fly.
  • Direction can either be up/down or right/left.
  • Width/height of items and container is allowed to change.
  • Container width/height is updated correctly to match maximum size of current items.
  • You can add an item at any time when space is available, and it will start off screen.

A loop() helper function is also provided which makes creating a carousel with looping content simple.

Demo

View the demo here.

View the code at "demo.html".

Installation

npm install --save dynamic-marquee
import { Marquee } from 'dynamic-marquee';

or

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/dynamic-marquee@1"></script>
<script type="text/javascript">
  const Marquee = dynamicMarquee.Marquee;
</script>

thanks to jsDelivr.

Usage

Construct Marquee Instance

With Default Options

const marquee = new Marquee(document.getElementById('marquee'));

With Custom Options

const marquee = new Marquee(document.getElementById('marquee'), {
  rate: 20, // 20 pixels/s downwards
  upDown: true // downwards instead of to the right
});

Append Item

You can add DOM elements, or just a string (which will automatically be wrapped in a div).

Each DOM element is only allowed on the marquee at one time.

const $item = document.createElement('div');
$item.textContent = 'testing123';
marquee.appendItem($item);

You are only allowed to append an item when there is room. You can check this like so:

if (marquee.isWaitingForItem()) {
  marquee.appendItem($item);
}

You can be notified when an item is required with

marquee.onItemRequired(({ immediatelyFollowsPrevious }) => {
  // for convenience if you have an item ready to go you can just return it
  // in place of `marquee.appendItem($item);`

  // if `immediatelyFollowsPrevious` is `true`, this would be a good time to add
  // a seperator on the side that is entering the screen first. See loop.js
  // for an example.
  return $item;
});

Do not perform any long running tasks in this method as it will block rendering.

If you need to perform some work in this method consider wrapping it in a setTimeout with delay 0.

Change the scroll rate? (px/s)

You can change the rate at any time, and set to 0 to pause.

marquee.setRate(-20);

Reset

To remove all items call

marquee.clear();

When has an item been removed?

You can be notified when an item has been removed with:

marquee.onItemRemove(($el) => {
  // $el has just been removed
});

When have all items finished scrolling?

You can be notified when the scroller is empty with:

marquee.onAllItemsRemoved(() => {
  //
});

You can check at any time with:

marquee.getNumItems();

Loop

A loop() function is provided for making looping content simple.

You provide an array of functions which return a DOM element, or string for that item. You can update this on the fly by calling the provided update() method.

When returning DOM elements each function should build the element from scratch, as the same DOM element is not allowed to appear on the marquee multiple times.

const $marquee = document.getElementById('marquee');
const marquee = new Marquee($marquee);
const control = loop(marquee, [
  () => 'item 1',
  () => 'item 2'
]);

// later
control.update([
  () => 'new item 1',
  () => 'new item 2'
])

dynamic-marquee's People

Contributors

tjenkinson avatar

Stargazers

 avatar

Watchers

 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.