Ninja Slider
Features
- Responsive layout
- Tap or swipe navigation on touch-enabled devices
- Supports YouTube, Vimeo, and HTML5 media
- Lazy loading of nearby images for smooth, seamless transitions
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>
-
Any HTML content
Each <li> is a slide that can include any HTML content, even without an image. Examples: Demo 3, and Demo 8
-
Add Links
Click here to learn how to add a link to a slider image or slide elements.To add a link to a slider image, you can do it like this:
<li> <a href="/home/"> <img class="ns-img" src="img/3.jpg"> </a> </li>If you need to add a link to a caption in a slide, you can:
<li> <a class="ns-img" href="img/a.jpg"></a> <div class="caption" onclick="window.open('/zoom-slider/')"> Zoom </div> </li>If a slide doesn't need a hyperlink, you can:- <li><a class="ns-img" href="img/1.jpg"></a></li>
- or: <li><img class="ns-img" src="1.jpg"></li>
-
Navigation controls
By default, ninja-slider.js automatically adds navigation arrows and a pager (nav bullets). To customize the navigation controls with your own styles, refer to the Adding Nav Controls Manually section in Demo 2.
-
Fullscreen button
The fullscreen button—which you can see when hovering over the demo slider at the top—needs to be added manually if required:
<div class="fs-icon" title="Expand/Close"></div> -
Support Youtube, Vimeo, and HTML videos/audios
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);
- "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"
- 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>
- 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.
- If the slider images are 900px wide and 400px high, you can set the aspectRatio to "900:400", or "9:4".
- If you want the images to scale responsively based on window height, set this value to "?:100%". See Demo 10 for details.
-
If your slider images have different aspect ratios, you have several options:
-
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> -
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.
-
Set: aspectRation:"default"
The slider will take the first image (class="ns-img") as its reference aspectRatio and use it for all images.
-
-
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.
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.
Styles
-
Make the slider full-width (100%)
Remove the max-width setting in ninja-slider.css, for example:
#ninja-slider div.slider-inner {max-width:800px;} -
Make the slider full-height (100%)
If you want the slider to scale responsively based on window height, set value "?:100%" to the aspectRatio in ninja-slider.js. Example: Demo 10.
-
Caption transition effects
You can easily configure caption transitions using the transition or animation properties in your stylesheet.
See the stylesheets for Demos 1 and 2 in the download package for examples.
-
Display captions outside the slider
Refer to the last paragraph in Demo 8 for details.
-
Fullscreen button
See the stylesheets for demo 1, 4, and 8 in the download package for fullscreen button styling.
-
Pager (navigation bullets)
By default, the Ninja Slider script automatically generates a pager and places it directly below the slider. The generated code looks like this:
<div id="ninja-slider-pager"> <a rel="0">1</a> <a rel="1">2</a> ...... <a rel="n">n+1</a> </div>Note: The pager numbers is invisible because ninja-slider.css sets:#ninja-slider-pager a { font-size:0; }The numbers will be visible if setting a different size, i.e. {font-size:10px;}
Use case #1: Remove the pager
In ninja-slider.css, set:#ninja-slider-pager { display:none; }
Use case #2: Place the pager at your preferred position
You can add a blank div, <div id="ninja-slider-pager"></div>, wherever you want the pager to appear. The slider script will find it and add the bullets.
Use case #3: Customize your own pager
You can create your own pager. For example:
<div id="ninja-slider-pager"> <a rel="0"><img src="bullet0.gif" /></a> <a rel="1"><img src="bullet1.gif" /></a> ...... <a rel="4"><img src="bullet4.gif" /></a> </div> -
Customize navigation arrows
-
Hide navigation arrows on mobile devices
