Git Product home page Git Product logo

dynamic-marquee's Introduction

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.

Using react?

Checkout "dynamic-marquee-react" instead!

Demo

Open in StackBlitz

or view a deployed demo.

View the code in "demo".

Installation

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

or

<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/dynamic-marquee@2"
></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
  startOnScreen: false, // start on screen
});

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);
}

appendItem also takes an optional second param config object, which can contain:

  • metadata: The value of this will be provided back to you in onItemRequired.
  • snapToNeighbour: If true the item will snap to the end of the neighbouring item instead of starting off screen.

You can be notified when an item is required with

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

  // If the new item would be touching another then `touching`
  // will be set to an object that contains `$el` and `metadata` of
  // the item it will be touching.
  // This can be used to determine if a separator should be added.
  // 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);

Note if you change the direction, isWaitingForItem() will change to false, and onItemRequired() will be called again when needed.

Reset

To remove all items call

marquee.clear();

You should also call this before removing the marquee from the DOM if you no longer need it to ensure that all timers are cleaned up and garbage collection can occur.

When has an item been removed?

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

marquee.onItemRemoved(($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

dependabot-preview[bot] avatar dependabot[bot] avatar github-actions[bot] avatar maltekiessling avatar tjenkinson 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

dynamic-marquee's Issues

Item already exists

In loop function, i am using result of
const marqueeItemElements = document.querySelectorAll('.item') as NodeListOf<HTMLElement>; const items = Array.from(marqueeItemElements).map((el) => { return ()=> el ; }); loop(marquee, items)
Please check for me!!

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Question: loop same element with long activity, DOM slowdown?

Hey,

in the loop section, theres this sentence:

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.

As far as I understand, it can cause issues for long activity apps, because creating DOM elements 1 million times over a perioud of X hours will slowdown the DOM renderer (browser), no?
Is there a way to have only the maximum needed elements, or set a maximum and if overseeded, re-use older elements already in the DOM?

Or does the engine take care of it themselves?

Thanks!

Javascript error on safari with loop function

Hi,
Thanks for your work,
I have a javascript error on my devtool, only on safari with the loop function :

TypeError: undefined is not an object (evaluating 'e.borderBoxSize[0]')

They are the same error on your demo page.

In Angular (above 2 version) getting error 'Cannot find name 'dynamicMarquee'

I am implementing this plugin in the angular project.
This is how I implemented it. I am getting an error on key 'dynamicMarquee' as 'Cannot find name 'dynamicMarquee'.

Code:
import { Marquee } from "dynamic-marquee";

const $marquee = document.getElementById("marquee");
if (!$marquee) {
throw new Error("The element #marquee wasn't found");
}

const marquee = (window.m = new dynamicMarquee.Marquee($marquee, {
  rate: -100,
}));

window.l = dynamicMarquee.loop(
  marquee,
  [
    function () {
      return "Contrary to popular belief, Lorem Ipsum is not simply random text";
    },
    function () {
      return "It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old";
    },
    function () {
      return "Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source";
    },
    function () {
      return 'RLorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC';
    },
  ],
  function () {
    var $separator = document.createElement("div");
    $separator.innerHTML = "&nbsp|&nbsp";
    return $separator;
  }
);

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Left to Right

hi.
thanks for your useful library.
how can i change that direction to Left to Right?

Word Wrap?

How does one word wrap, when doing up/down scrolling? 🙄

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Marquee is a little jerky

Hello, I'm using your perfect code, and everything is ok, except one thing, that render of marquee is a little jerky in vertical axis. Is it possible to do something? We are using 1080i50 output on caspar.

[Question] Stop on touch? (loop?)

Hello,

is it possible to implement something like "stop on touch / mouseover"?

Its 'harder' because the elements are created on the fly.
I've tried something like this, but it wouldnt catch it:

jQuery(document).on('touchstart mouseover mousedown', '.my_element_class', function () {
                if (!marquee) {
                    return;
                }
                marquee.setRate(0); // pause
            }.on('mouseleave', '.my_element_class', function () {
                if (!marquee) {
                    return;
                }
                marquee.setRate(marquee_speed); // resume
            }));

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Marquee wont work in Angular after giving build

Steps to regenerate:

  1. Create a hello world in Angular (Latest version)
  2. Apply dynamic marquee
import { Component } from '@angular/core';
import { Marquee } from 'dynamic-marquee';

@Component({
  selector: 'home',
  template: `
    <div>
      <div id="marquee"></div>
    </div>
  `,
})
export class HomeViewComponent {
  $marquee: HTMLElement;
  marquee: Marquee;

  ngAfterViewInit() {
    this.$marquee = document.getElementById('marquee')!;
    this.marquee = new Marquee(this.$marquee, {
      rate: -100,
    });

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

    this.marquee.onItemRequired(({ immediatelyFollowsPrevious }) => {
      setTimeout(() => {
        this.marquee.appendItem("new");
      },0);
    });
  }
}

  1. Give build using ng build --base-href "/applicationName/"
  2. Run the dist file

Dymaic marquee doesnt work properly. Texts are overlapping with each other. There is no error logged.

[Question] HTML Content

Hello,

is it possible to loop items which contain not only text but for example a <a href... wrap?

Support stop on mouseover?

Hi,

Does it support stop marquee on mouseover and resume marquee when mouse moves away from it?

Thank you

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet.
We recommend using:

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

No room for item.

I am getting a console.error No room for item.

I am not sure what is wrong.

clear() is broken

Checking the demo page and running marquee.clear():

Uncaught TypeError: Cannot read properties of undefined (reading 'remove')
    at dynamic-marquee@2:7
    at dynamic-marquee@2:7

I can reproduce this with a local use of dynamic-marquee.

Infinite loop when using images in upDown mode

Hi, I noticed that using elements containing images in the items of the loop builders array leads to an infinite loop where the library never stops calling onItemRequired as the items height is evaluated as 0.

Here is an example:

const marquee = new Marquee($target, { upDown: true, rate: 20 })

const builders = items.map(item => {
  return () => {
    const el = document.createElement('div')
    el.innerHTML = item // item is HTML code like '<div><img ... /></div>'
    // height is needed to prevent infinite loop
    el.style.minHeight = $target.clientWidth * 232 / 213 + 'px'
    return el
  }
})

loop(marquee, builders)

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.