GithubHelp home page GithubHelp logo

markcellus / carousel-js Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 12.0 1.5 MB

Easily implement a dynamic carousel using minimal javascript. Supports Meteor, AngularJS, React, Polymer and any CSS library, e.g. Bootstrap.

License: MIT License

JavaScript 100.00%
carousel-component carousel thumbnails carousel-view carousels javascript

carousel-js's Introduction

Build Status npm version

Carousel

A lightweight and flexible Carousel class that allows you to build fully functional, advanced Carousels with minimal javascript and markup. This library is built using native vanilla javascript (for performance) and adheres to latest ECMAScript specs. Supports IE10+, all major browsers and even mobile.

Inspiration

This is a module that I built originally to solve many of the headaches and complexities around building flexible and scalable carousels.

This library has been used and adopted on many projects, including:

Installation

You can install as an npm package if using a build system like Browserify.

npm install carousel-js --save-dev

Usage

Carousel

You can create a carousel based off of a set of predetermined markup. Assuming you have the appropriate elements already in the DOM and have your CSS set up correctly to show and hide the styles. You can setup Carousel and navigate to panels programmatically.

var carousel = new Carousel({
    panels: document.getElementsByClassName('carousel-panel')
});

carousel.goTo(1); // go to second carousel panel

Carousel with Clickable Thumbnails

Create a carousel with thumbnails based off of a set of predetermined markup. Assuming, you have your html in the DOM and CSS set up correctly. You can use the Carousel class to add interactivity:

var thumbnails = document.getElementsByClassName('carousel-thumbnail');

var carousel = new Carousel({
    panels: document.getElementsByClassName('carousel-panel'),
    thumbnails: thumbnails
});

// click on second thumbnail to show second panel
thumbnails[1].click();

More details and example can be found here.

Carousel Image Lazy Loading

The carousel class also allows lazy loading images so that you can ensure that large image assets only load when transitioning to the panel they reside in. This saves us from hogging the user's bandwidth and downloading all image assets before a user navigates to it. To use the lazy loading functionality, let's assume you have the following in the DOM already:

<div class="carousel-panel">
    <img data-lazy-src="http://www.gstatic.com/webp/gallery/1.jpg" src="" />
</div>
<div class="carousel-panel">
    <img data-lazy-src="http://www.gstatic.com/webp/gallery/2.jpg" src="" />
</div>

Then you can do this:

var carousel = new Carousel({
    panels: document.getElementsByClassName('carousel-panel'),
    panelActiveClass: 'carousel-panel-active',
    lazyLoadAttr: 'data-lazy-src',
    assetLoadingClass: 'image-loading'
});

// go to second panel and lazy load the image it contains
carousel.goTo(1);

A more in-depth, working example of Carousel's lazy loading can be found here.

Carousel with Arrows

You can easily create a carousel with the traditional left and right arrows. Assuming you have the following markup and styles in the DOM, you can do something like this:

var leftArrowElement = document.getElementsByClassName('carousel-left-arrow')[0];
var rightArrowElement = document.getElementsByClassName('carousel-right-arrow')[0];

var carousel = new Carousel({
    panels: document.getElementsByClassName('carousel-panel'),
    leftArrow: leftArrowElement,
    rightArrow: rightArrowElement,
    arrowDisabledClass: 'arrow-disabled'
});

// go to first panel which will add a css class on the left arrow to disable it
carousel.goTo(0);

// click right arrow to navigate to next panel
// which will remove the disabled css class from the left arrow
rightArrowElement.click();

carousel-js's People

Stargazers

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

Watchers

 avatar  avatar

carousel-js's Issues

Keyboard interaction

I think it would be a good idea to add the ability to navigate through the carousel with keyboard arrows (left and right) and maybe include the spacebar for advancing ahead.

Getting errors when trying to use the provided examples

When trying to use your examples, they fail to load and throw the following errors:

Error: Module name "carousel-panels" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
TypeError: Carousel is not a constructor

I set the path to carousel.js, and don't see anywhere else that I could have made a mistake. It's hosted at http://52.11.83.207/nerf/carousel-with-thumbnails.html

I understand if you don't care to maintain this anymore, but would still appreciate it if you could take a look and let me know what's going on!

Thanks!

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.