npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pm-carousel

v0.1.5

Published

pm-carousel is an accessible and small carousel plugin written in JavaScript with no dependencies

Downloads

14

Readme

pm-carousel

Accessible carousel plugin written in JS with no dependencies.

Examples here: https://lordfpx.github.io/pm-carousel/

Features

  • Fully accessible (if you respect HTML order)
  • Keyboard navigation (keyboard arrows, Home and End keys)
  • Touch swipe
  • Responsive carousel settings
  • No dependencies
  • Vanilla JS
  • Pretty small (about 3.5Kb gzip)

HTML markup

The HTML order is very important to be fully accessible. You will notice strings like {text} or {nbr}. They are mandatory and will be replaced by the script.

Full HTML example to use pm-carousel:

<div data-pm-carousel>
	<button
		type="button"
		data-pm-carousel-playstop="Stop the carousel|Play the carousel"
		hidden
	>
		{text}
	</button>

	<ul data-pm-carousel-paging hidden>
		<li>
			<button type="button">Slide {nbr}</button>
		</li>
	</ul>

	<div data-pm-carousel-wrapper>
		<div data-pm-carousel-overflow>
			<div data-pm-carousel-item>...</div>
		</div>
	</div>

	<button
		data-pm-carousel-prev="Previous item|Go back to last item"
		type="button"
		hidden
	>
		{text}
	</button>
	<button
		data-pm-carousel-next="Next item|Go back to first item"
		type="button"
		hidden
	>
		{text}
	</button>
</div>

Play and Stop button (optional)

This is the most basic button example. You can add any HTML markup that will not break this base. {text} will be replaced by the right value defined inside the data-pm-carousel-playstop attribute.

<button
	type="button"
	data-pm-carousel-playstop="Stop the carousel|Play the carousel"
	hidden
>
	{text}
</button>

Paging (optional)

The HTML inside the button can be freely personalized. {nbr} is mandatory and will be replaced by the slide number.

<ul data-pm-carousel-paging hidden>
	<li>
		<button type="button">Slide {nbr}</button>
	</li>
</ul>

The slides

<div data-pm-carousel-wrapper>
	<div data-pm-carousel-overflow>
		<div data-pm-carousel-item>...</div>
		<div data-pm-carousel-item>...</div>
		<div data-pm-carousel-item>...</div>
	</div>
</div>

Previous and next buttons

This is the most basic button example. You can add any HTML markup that will not break this base.

Labels inside data-pm-carousel-prev and data-pm-carousel-next attributes are used to dynamize {text}.

<button
	data-pm-carousel-prev="Previous item|Go back to last item"
	type="button"
	hidden
>
	{text}
</button>

<button
	data-pm-carousel-next="Next item|Go back to first item"
	type="button"
	hidden
>
	{text}
</button>

Settings

Default settings:

{
  loop: true,          // Will loop
  group: 1,            // nbr of slides per page
  spaceAround: 0,      // centered mode with partial view of surrounding slides (express in %)
  noStartSpace: false, // in combinaison with "spaceAround" option, but align left the carousel
  autoplay: 0,         // speed of the autoplay (0 for disabled)
}

Usage

npm i pm-carousel --save

In your script:

import pmCarousel from "pm-carousel"

If you need to load the script directly on your page, or to import it the "old-fashion way" (see https://github.com/umdjs/umd), you can find the umd version inside the dist folder: pm-carousel.umd.js.

Initialize

2 ways to initialize pm-carousel:

  • For all carousels with default options:

    pmCarousel()
  • For specified carousels only (with default options):

    const myCarousels = document.querySelectorAll(".my-class")
    
    pmCarousel({}, myCarousels)

Both methods can be called again when new carousels are injected into the DOM.

Settings

2 methods:

  • When calling pmCarousel function:

    pmCarousel({
    	default: {
    		group: 1,
    	},
    	responsive: [
    		{
    			minWidth: "800px",
    			group: 4,
    		},
    		{
    			minWidth: "400px",
    			group: 2,
    		},
    		{
    			minWidth: "600px",
    			disable: true,
    		},
    	],
    })
  • Inside the data-pm-carousel (the JSON must be valid!):

    <div
    	data-pm-carousel='
        {
          "default": {
            "group": 1
          },
          "responsive": [
            {
              "minWidth": "800px",
              "group": 4
            },
            {
              "minWidth": "400px",
              "group": 2
            },
            {
              "minWidth": "600px",
              "disable": true
            }
          ]
        }'
    >
    	...
    </div>

Responsive settings

Have you noticed the reponsive key in the above example? That's the way to make your carousel fully responsive.

You can use whatever unit you want for the minWidth setting.

The disable setting will deactivate the carousel.

API

The instance of pm-carousel can be reached from nodes with data-pm-carousel attribute.

Play and Stop

Only available when Play/PAuse button is present.

const myCarousel = document.querySelector(".class-of-a-carousel");

// Start playing
myCarousels.pmCarousel.play()

// Stop playing
myCarousels.pmCarousel.stop()

// Toggle Play or Stop
myCarousels.pmCarousel.toggleAutoplay()

Change current page

const myCarousel = document.querySelector(".class-of-a-carousel");
myCarousels.pmCarousel.changeActive(2)

Disable and reinit

const myCarousel = document.querySelector(".class-of-a-carousel");

// Disable Carousel
myCarousels.pmCarousel.disable()

// Reinit the Carousel again
myCarousels.pmCarousel.reinit()