• @colerise
  • @colerise
  • RESPONSIVE
    MOBILE·FRIENDLY
    @colerise
  • SLIDESHOW
    TOUCH·ENABLED
    @colerise


Demos & Templates: 12345678910

Thanks for using Ninja Slider

Ninja Slider

Features

HTML

<div id="ninja-slider">
    <div class="inner">
        <ul>
            <li><a class="ns-img" href="img/1.jpg"></a></li>
            <li><a class="ns-img" href="img/2.jpg"></a></li>
            <li><a href="/home/"><img class="ns-img" src="img/3.jpg"></a></li>
        </ul>
    </div>
</div>

JavaScript

Open ninja-slider.js in the download package and configure the options as needed:

var nsOptions = {
    sliderId: "ninja-slider", 
    transitionType: "slide", 
    autoAdvance: true, 
    rewind: true, 
    delay: "default", //or integer value such as: 3500
    transitionSpeed: "default", //eg. 500, 1200, or "default"
    aspectRatio: "9:6", 
    initSliderByCallingInitFunc: false, 
    shuffle: false, 
    startSlideIndex: 0, //0-based 
    navigateByTap: true, 
    keyboardNav: true, 
    before: null,
    (other options)
}; 
var nslider = new NinjaSlider(nsOptions);
Five options:
  • "fade": fade-in/out transition;
  • "slide": slide-in/out horizontally;
  • "slide vertical": slide-in/out vertically;
  • "zoom": zoom-out transition;
  • "kenburns 1.2": Ken Burns effect. You can update the 1.2 with another scale (greater than 1), such as "kenburns 1.1".
  • "none": no transition effect.

true or false: Automatically advances to the next slide

Allows the slider to go from the last slide back to the first.

Note: Only applicable when transitionType is "slide", or "slide vertical"

Delay between items in ms. Its value can be:
  • number of milliseconds, e.g.
    delay: 4500
  • delay: "default"

    "default" will be converted to 3500 for "fade", "slide" and "slide vertical" transition, and 6000 for "zoom" and "kenburns" transition.

To give a specific slide a different delay, use the data-delay attribute in the HTML markup, e.g.

<li data-delay="6500">...</li>
Speed of the transition. Its value can be:
  • number of milliseconds, e.g.
    transitionSpeed: 300
  • transitionSpeed: "default"

    "default" will be converted to 300 for "slide" and "slide vertical" transition, 2000 for "fade", and 1500 for "zoom" and "kenburns" transition.

  1. If the slider images are 900px wide and 400px high, you can set the aspectRatio to "900:400", or "9:4".
  2. If you want the images to scale responsively based on window height, set this value to "?:100%". See Demo 10 for details.
  3. If your slider images have different aspect ratios, you have several options:
    1. Choose a fixed aspectRatio("x:y") that fits most of the images.

      With this setting, the images that don't match will follow the style rule in ninja-slider.css:

      #ninja-slider .ns-img { background-size:contain; }

      You can change this to background-size:cover in the stylesheet, or override it for a specific slide with an inline style: <a class="ns-img" href="img/5.jpg" style="background-size:cover;"></a>

    2. Set: aspectRation:"auto"

      The slider will adjust its height for each slide image individually. This may cause the content below the slider to move up and down, which some users may find undesirable.

    3. Set: aspectRation:"default"

      The slider will take the first image (class="ns-img") as its reference aspectRatio and use it for all images.

  4. If you expect different aspect ratios for different devices, you can update the css file using @media queries. For example, if you want 2:1 for mobile 780px devices:
    @media (max-width:780px) {
        #ninja-slider ul {
            padding-top:50%!important;
        }
    }

In ninja-slider.js you will find the following line of code:

var nslider = new NinjaSlider(nsOptions);

This line initializes the slider when the page loads.

If the slider markup is not available, or it is (or its container is) display: none, the slider should not be initialized during page load. Instead, the slider should be initialized using one of the two methods below after the markup is ready or the container becomes visible.

  • Option 1: Set initSliderByCallingInitFunc: true

    When this option is enabled, the slider will not be instantiated automatically.
    You will need to explicitly call the initialization function when ready:

    nslider.init();

    You can also pass an index number to start from a specific slide. For example:

    nslider.init(2);

    This will start the slider from the 3rd slide. Check demo 8 for details.

  • Option 2: Manually instantiate the slider

    Remove the following line from the ninja-slider.js:

    var nslider = new NinjaSlider(nsOptions);

    Then, add the line to the location in your code where the slider should be initialized (i.e., when the markup is ready or the container becomes visible).

    Note: the option initSliderByCallingInitFunc must be set to false when using this approach.

true or false: Indicates if tapping on the screen (finger touch without swipe) will navigate to next slide when the device is touch-enabled.

Note: It will be overridden to false if pauseOnHover is true.

keyboardNav: true or false— allows navigation through slides using the left and right arrow keys.

A callback function executed at the beginning of each slide transition, if defined.

For example, in Demo 4, the before callback is used to ensure that manual slide navigation also triggers the thumbnail carousel to update accordingly.

The following less commonly used options are optional.

  • m: true or false (default: true): Even if transitionType may have not been set to "slide", manual navigation actions such as by clicking the arrow buttons or swiping on mobile device, will still use the "slide" transition by default. To disable this behavior and use the specified transition type, set m: false.
  • n: true or false (default: true): When true, the Prev/Next navigation buttons are disabled when the first or the last slide is active, preventing further navigation in that direction. Setting n: false keeps the buttons enabled, allowing continuous navigation (the slider will wrap to the previous or next slide).
  • pauseOnHover: true or false(default: false): Pauses automatic slide advancement when the mouse pointer is over the slider.
    Note: navigateByTap will be disabled if pauseOnHover is set to true.

Built-in functions

If you need to control the slider programmatically, click here for details.

  • nslider.displaySlide(slideIndexOrLi): Navigates to a slide. The parameter can be either the slide index (0-based) or the slide <li> element.
  • nslider.prev(): Navigates to the previous slide.
  • nslider.next(): Navigates to the next slide.
  • nslider.toggle(): Toggles between pause and play for the slider.
  • nslider.getPos(): Returns the current slide index.
  • nslider.getSlides(): Returns all slide elements (<li> elements).
  • nslider.playVideo(slideIndexOrLi): Navigates to the specified slide (by index or <li>) and plays the video inside that slide.
  • nslider.init(index): Starts the slider if it was not initialized during page load. See the initSliderByCallingInitFunc option for details.

Styles