Create a list of items to use as a responsive carousel slider
The slider is a responsive display of elements that can be scrolled through using the mouse or touch gestures.
Usage
To apply the Slider component, add the data-uk-slider
attribute to a container element around a .uk-slider-container
element. Add a list of items and assign the .uk-slider
class to the list. Use the .uk-width-*
and .uk-grid-width-*
classes to determine how many elements are visible at once.
Example
Markup
<div data-uk-slider>
<div class="uk-slider-container">
<ul class="uk-slider uk-grid-width-medium-1-4">
<li>...</li>
...
</ul>
</div>
</div>
Navigation
The slider itself can be scrolled with click and drag of the mouse or swiping on touch devices. It is a good idea to also add a clickable slidenav navigation. The slidenav adds arrows buttons that appear when you hover the slider.
Example
<div class="uk-slidenav-position" data-uk-slider>
<div class="uk-slider-container">
<ul class="uk-slider uk-grid-width-medium-1-4">
<li>...</li>
...
</ul>
</div>
<a href="" class="uk-slidenav uk-slidenav-contrast uk-slidenav-previous" data-uk-slider-item="previous"></a>
<a href="" class="uk-slidenav uk-slidenav-contrast uk-slidenav-next" data-uk-slider-item="next"></a>
</div>
Center Mode
By default, elements of the slider always align to the left edge of the slider container. If you want to center the elements instead, set the center
attribute to true
.
Note The center list element always has the class .uk-active
added. You can make use of that if you want to highlight the center element.
Example
Markup
<div data-uk-slider="{center:true}">
<div class="uk-slider-container">
<ul class="uk-slider uk-grid-width-medium-1-4">
<li>...</li>
...
</ul>
</div>
</div>
Disable Infinite Scrolling
By default, the slider loops through all items. To disable that behaviour, set the infinite
parameter to false
. This works both with center mode enabled and disabled.
Example
Markup
<div data-uk-slider="{infinite: false}">
<div class="uk-slider-container">
<ul class="uk-slider uk-grid-width-medium-1-4">
<li>...</li>
...
</ul>
</div>
</div>
Item gutter
If you want some spacing between your elements, add the .uk-grid
class to the slider container. The elements will then be spaced according to the grid gutter.
Note You can use the modifiers uk-grid-medium
and uk-grid-small
to change the gutter.
Example
Item width
To set the widths of your elements, use the width classes from the UIkit grid. Either use the uk-grid-width-*
classes on the slider container or use individual widths per list item using the uk-width-*
classes.
Example: Individual widths
Markup
<div data-uk-slider>
<div class="uk-slider-container">
<ul class="uk-slider">
<li class="uk-width-1-3">...</li>
<li class="uk-width-1-5">...</li>
<li class="uk-width-2-5">...</li>
...
</ul>
</div>
</div>
Responsive behaviour
To adapt to different viewports, you can use the responsive grid classes. In the following example, the slider displays four items on large viewports, three on medium and just one item on small ones.
Example: Responsive widths
Markup
<div data-uk-slider>
<div class="uk-slider-container">
<ul class="uk-slider uk-grid-width-medium-1-3 uk-grid-width-large-1-4">
<li>...</li>
...
</ul>
</div>
</div>
Fullscreen mode
The slider includes a fullscreen mode where each slide stretches to 100% the height of the viewport.
Markup
<div data-uk-slider>
<div class="uk-slider-container">
<ul class="uk-slider uk-slider-fullscreen">
<li>...</li>
...
</ul>
</div>
</div>
JavaScript options
Option | Possible value | Default | Description |
---|---|---|---|
center |
boolean | false | Center items mode |
threshold |
boolean | true | Mouse movement threshold in pixel until trigger element dragging |
infinite |
boolean | true | Infinite scrolling |
activecls |
string | uk-active | Class added on active item in center mode |
autoplay |
false | boolean | Defines whether or not the slider items should switch automatically |
pauseOnHover |
boolean | true | Pause autoplay when hovering a slider |
autoplayInterval |
integer | 7000 | Defines the timespan between switching slider items |
Init element manually
var slider = UIkit.slider(element, { /* options */ });
Events
Name | Parameter | Description |
---|---|---|
focusitem.uk.slider |
event, index, item | On item focus |