The JavaScript API
Your own buttons and scripts can change a project’s slides, start and stop its slideshow, replay its layer transitions, play its videos, open popups, and read which slide is showing.
On this page
To control a project, select it with jQuery and call LayerSlider’s layerSlider() function on it. Write jQuery, not $: WordPress loads jQuery without the $ shortcut, so code that uses $ stops with an error.
When the visitor clicks a button of yours, for example, this call moves the project with the ID 12 to its next slide.
jQuery( '[id^="layerslider_12_"]' ).layerSlider( 'next' );To run code when something happens in the project, such as a slide change, use the project’s events.
Finding a project on the page
Find the project by its ID. In the LayerSlider dashboard, each project’s card shows its ID after its name, for example #12. Select the project by the start of its element id, the ID with the underscore after it: [id^="layerslider_12_"], as the examples do. The rest of the element id is random and changes on every page load, so #layerslider_12 matches nothing.
If the page shows the project more than once, this selector matches every copy. A command then reaches all of them, and a call that reads the project’s state answers for the first.
A project that has a slug can also be selected by its slug, as [data-ls-slug="summer-sale"]. A slug is a name of your own for the project. It goes with the project when you export it to another site, where the project gets a new ID. Enter it in Project Settings › Publish, in the Project Slug & Keywords section, and click Publish. Use unaccented letters, digits, hyphens, and underscores, and give each project a slug of its own.
A command that reaches no project does nothing and shows no error. If a command has no effect, check first that its selector matches the project. Compare the ID in your selector with the one on the project’s card in the LayerSlider dashboard. Another cause is a call made before the project has loaded.
Commands
Each command is sent as jQuery( selector ).layerSlider( command ). A command that takes a value gets it as a second argument. Every command returns the jQuery set it was called on, so you can chain commands.
These are the commands. Where a command has 2 names, either one works.
| Command | What it does | What it takes |
|---|---|---|
A slide number, such as 3 |
Changes to the slide with that number, counted from 1, and the slideshow continues from there | None |
next or nextSlide |
Changes to the next slide, or from the last slide to the first | None |
prev or previousSlide |
Changes to the previous slide, or from the first slide to the last | None |
start or startSlideshow |
Starts the slideshow again | None |
stop or stopSlideshow |
Stops the slideshow on the current slide. The slide’s layer transitions continue to their end, unless the project’s Pause Layers switch is on | None |
pause or pauseSlider |
Freezes the current slide’s layer transitions where they are | None |
resume or resumeSlider |
Continues the layer transitions that pause froze |
None |
toggle or toggleSlider |
Runs pause and resume in turn |
None |
replay or replaySlide |
Plays the current slide’s layer transitions again from their start | None |
reverse or reverseSlide |
Plays the current slide’s layer transitions backward, or forward again if they already run backward | true, optional, to play them forward again after they’re back at their start |
resetSlide |
Puts the current slide’s layer transitions back to their start and holds them there, so the slide’s layers show as they do before they enter | None |
redraw or redrawSlider |
Calculates the project’s size and layout again | None |
playMedia |
Plays the current slide’s media layers | None |
pauseMedia |
Pauses the current slide’s media layers | None |
muteMedia |
Mutes the media layers | None |
unmuteMedia |
Unmutes the media layers. Call it from the visitor’s click or tap | None |
destroy or kill |
Stops the project completely: it stays on the page, but it changes no more slides and no longer answers calls | true, optional, to remove the project from the page as well |
Pass a slide number as a number, also when you read it from your page’s markup: a string such as '2' does nothing. A slide number, next, and start are ignored while the project is busy, for example while it changes slides.
A project follows a change in its container’s width by itself, so redraw isn’t needed after such a change.
If your code was written for LayerSlider 8, see which changes in LayerSlider 9 affect it.
Starting, stopping, and pausing the slideshow
Use stop to keep the project on its current slide, and pause to freeze the slide where it is.
After stop, start changes to the next slide at once if the slide’s layer transitions have ended by then. After pause, resume continues the slide from where it froze, and the project changes to the next slide when the rest of the slide’s duration has passed.
For a single play and pause button, use toggle.
Popups
A popup is a project too, and your script finds it by its ID like any other project. These commands open and close popups:
| Command | What it does | What it takes |
|---|---|---|
openPopup |
Opens the popup | A slide number, optional, to open the popup on that slide |
closePopup |
Closes the popup | None |
closeAllPopups |
Closes every open popup on the page. Send it to any one of the popups | None |
launchPopups |
Opens every popup on the page at once, one over the other. Send it to any one of the popups | None |
Your script can open a popup that the page includes: one that the popup’s Target Pages settings show on the page, or one embedded in the page like any project. A popup that only your script opens needs no trigger in its Launch Popup settings, although the Project Editor shows a warning about the missing trigger.
After a popup has closed, it doesn’t open again until the page is loaded again, because its Prevent Reopening switch is on by default. To let your script open the popup as often as it needs, turn off Prevent Reopening in Project Settings › Layout › Repeat Control.
A button of yours can open a popup without a script: enter the button’s selector, such as .open-offer, in the popup’s Open by Click field, in Project Settings › Layout › Launch Popup. The popup finds such buttons when the page loads, so for a button that your script adds later, call openPopup.
Reading the project’s state
The calls that read the project’s state are getters. A getter returns a value instead of the jQuery set. If the page shows the project more than once, a getter answers for the first copy.
These are the getters:
| Getter | What it returns |
|---|---|
eventData |
The project’s state, such as its slides and its slideshow, in the same object that event handlers receive |
data |
The project’s own object inside LayerSlider |
userInitOptions |
The options the project was started with |
defaultInitOptions |
LayerSlider’s default options |
sliderInitOptions |
The options in effect: the defaults merged with the options the project was started with |
originalMarkup |
The HTML of the project’s element before the project started, as a string |
The current slide’s number, counted from 1, and the number of slides are in eventData:
const $project = jQuery( '[id^="layerslider_12_"]' );
const state = $project.layerSlider( 'eventData' );
const current = state.slides.current.index;
const count = state.slides.count;Calls made before the project has loaded
A command sent before the project has loaded can be lost without a message, and a getter called that early throws an error. A call made in a script right after the project, in a jQuery ready handler, or in a sliderWillLoad handler comes before the project has loaded. A ready handler comes too early because LayerSlider starts its projects only after every ready handler of the page has run.
To act as soon as the project can take commands, bind a handler for its sliderDidLoad event. In that handler, commands run at once, and a slide number becomes the first slide the project shows. This script starts the project on its third slide.
jQuery( function() {
jQuery( '[id^="layerslider_12_"]' ).on( 'sliderDidLoad', function() {
jQuery( this ).layerSlider( 3 );
} );
} );With LayerSlider’s default settings, a handler bound in a jQuery ready handler, as here, receives the event wherever your script is on the page. A handler bound when the window’s load event fires comes too late for a project in or near the first screen.
If the project is far below the first screen, its sliderDidLoad event comes after the page has loaded, even when nobody scrolls. The article on events explains when such a project loads and starts.
Examples
Change 12 in the examples to your project’s ID, and 15 to your popup’s.
The first example adds buttons of your own: one for the previous slide, one for the next, and one for the third slide.
<button type="button" class="sale-prev">Previous</button>
<button type="button" class="sale-next">Next</button>
<button type="button" class="sale-third">Third slide</button>
<script>
jQuery( function() {
const $project = jQuery( '[id^="layerslider_12_"]' );
jQuery( '.sale-prev' ).on( 'click', function() {
$project.layerSlider( 'prev' );
} );
jQuery( '.sale-next' ).on( 'click', function() {
$project.layerSlider( 'next' );
} );
jQuery( '.sale-third' ).on( 'click', function() {
$project.layerSlider( 3 );
} );
} );
</script>The second example shows the current slide and the number of slides when the visitor clicks a button of yours.
<button type="button" class="sale-which">Which slide?</button>
<p class="sale-status"></p>
<script>
jQuery( function() {
const $project = jQuery( '[id^="layerslider_12_"]' );
const $status = jQuery( '.sale-status' );
jQuery( '.sale-which' ).on( 'click', function() {
const state = $project.layerSlider( 'eventData' );
const current = state.slides.current.index;
const count = state.slides.count;
$status.text( 'Slide ' + current + ' of ' + count );
} );
} );
</script>The third example opens a popup when the project has reached its last slide.
<script>
jQuery( function() {
const $project = jQuery( '[id^="layerslider_12_"]' );
const $popup = jQuery( '[id^="layerslider_15_"]' );
$project.on( 'slideChangeDidComplete', function( event, slider ) {
if ( slider.slides.current.index === slider.slides.count ) {
$popup.layerSlider( 'openPopup' );
}
} );
} );
</script>