Using Events and API Methods to Control Owl Carousel

Options shape how a carousel behaves when it starts, but many interfaces need to respond as it moves: updating a counter, synchronising a second slider or loading content on demand. Owl Carousel supports this through jQuery events that the library both emits and listens for.
Options Versus Events
Options describe how a carousel should behave from the moment it starts: how many items to show, whether to loop and how fast to move. Events describe what happens while it runs. The distinction matters because many interface requirements cannot be expressed as fixed settings. A slide counter, a caption that updates as slides change, a progress bar or a pair of synchronised sliders all depend on reacting to movement as it occurs.
Version 2 of Owl Carousel uses jQuery's event system for both directions of communication. The carousel announces changes by triggering events that your code can listen for, and your code issues commands by triggering events that the carousel listens for. Once that symmetry is clear, the whole API becomes predictable: every interaction is either an event you hear or an event you send.
Listening for Events
Every event carries the namespace owl.carousel. Attach handlers with jQuery's on method:
var owl = $(".owl-carousel");
owl.on("changed.owl.carousel", function (e) {
if (e.property.name !== "position") return;
console.log("Now showing item", e.item.index);
});
The changed event fires for several kinds of change, not only position, so checking e.property.name avoids reacting to unrelated updates.
Useful Events
| Event | When it fires |
|---|---|
| initialize / initialized | Before and after the carousel is built |
| change / changed | Before and after a property such as position changes |
| translate / translated | At the start and end of a slide movement |
| drag / dragged | When a drag or swipe begins and ends |
| resize / resized | Before and after the layout responds to a window resize |
| refresh / refreshed | Before and after the carousel recalculates its layout |
| load.owl.lazy / loaded.owl.lazy | Before and after a lazy image loads |
Handlers for initialize and initialized must be attached before calling owlCarousel(), because those events fire during initialisation. Alternatively, pass callbacks as options, such as onInitialized or onTranslated.
Reading the Event Object
e.item.indexgives the position of the current item, including clones when looping is enabled.e.item.countgives the number of original items, excluding clones.e.page.indexande.page.countdescribe pagination in terms of pages rather than items.e.property.nameande.property.valueidentify what changed duringchangeandchangedevents.
Controlling the Carousel
Version 2 controls the carousel by triggering events on its element rather than calling methods directly. Optional arguments are passed in an array:
owl.trigger("next.owl.carousel"); // advance one step
owl.trigger("prev.owl.carousel", [300]); // go back over 300 ms
owl.trigger("to.owl.carousel", [4, 500]); // jump to item 5
owl.trigger("refresh.owl.carousel"); // recalculate layout
Adding and Removing Items
Content can change after initialisation. Use add.owl.carousel with markup and an optional position, remove.owl.carousel with a position, then trigger a refresh so the layout updates.
owl.trigger("add.owl.carousel", ['<div class="item">New slide</div>', 0])
.trigger("refresh.owl.carousel");
owl.trigger("remove.owl.carousel", [2])
.trigger("refresh.owl.carousel");
Destroying and Rebuilding
To remove the library and restore the original markup, trigger destroy.owl.carousel. This is helpful when switching to a static grid on large screens or before reinitialising with different options.
Example: A Slide Counter
owl.on("initialized.owl.carousel changed.owl.carousel", function (e) {
if (!e.namespace) return;
var current = e.relatedTarget.relative(e.item.index) + 1;
$(".counter").text(current + " / " + e.item.count);
}).owlCarousel({ items: 1, loop: true });
The relative method on the carousel instance converts an index that includes clones into the position among the original items, which keeps the counter accurate when looping.
Passing Callbacks as Options
Every event has a matching option whose name begins with on, which is convenient when the handler belongs with the rest of the configuration. Callbacks receive the same event object as listeners attached with on:
$(".owl-carousel").owlCarousel({
items: 1,
onInitialized: function (e) {
console.log("Ready with", e.item.count, "items");
},
onTranslated: function (e) {
$(".caption").text($(e.target).find(".owl-item.active img").attr("alt"));
}
});
Updating Content Outside the Carousel
Many layouts show a title, description or link that relates to the current slide but sits outside the carousel. Storing that information in data attributes on each slide keeps markup and behaviour together:
<div class="item" data-title="Northern Coast" data-link="/tours/north">…</div>
owl.on("translated.owl.carousel", function () {
var $slide = $(this).find(".owl-item.active .item").first();
$(".slide-title").text($slide.data("title"));
$(".slide-link").attr("href", $slide.data("link"));
});
Loading Slides on Demand
For long collections, it is wasteful to render every slide at once. Listening for movement and fetching further items as the visitor approaches the end keeps the initial page light:
var loading = false;
owl.on("changed.owl.carousel", function (e) {
if (e.property.name !== "position" || loading) return;
var remaining = e.item.count - e.item.index - e.page.size;
if (remaining > 2) return;
loading = true;
fetch("/api/slides?offset=" + e.item.count)
.then(function (r) { return r.text(); })
.then(function (html) {
$(html).each(function () { owl.trigger("add.owl.carousel", [this]); });
owl.trigger("refresh.owl.carousel");
loading = false;
});
});
Avoiding Feedback Loops
When two carousels are synchronised, moving one triggers a changed event that moves the other, which in turn fires its own changed event. Without a guard, the two sliders can keep commanding each other. A simple flag breaks the cycle:
var syncing = false;
function link(source, target) {
source.on("changed.owl.carousel", function (e) {
if (syncing || e.property.name !== "position") return;
syncing = true;
target.trigger("to.owl.carousel", [e.item.index, 300]);
syncing = false;
});
}
link(carouselA, carouselB);
link(carouselB, carouselA);
Debugging Events
When behaviour is unclear, logging every event the carousel emits shows exactly what happens and in which order:
owl.on([
"initialized", "resized", "refreshed", "changed",
"translate", "translated", "drag", "dragged"
].map(function (n) { return n + ".owl.carousel"; }).join(" "), function (e) {
console.log(e.type, e.property && e.property.name, e.item && e.item.index);
});
Key Takeaways
- Listen for namespaced events to react to movement.
- Trigger events to issue commands such as next, previous and go to.
- Attach
initializelisteners before callingowlCarousel(). - Refresh the carousel after adding or removing items.
Summary
Listen for namespaced events to react to movement, trigger events to issue commands and always refresh after changing content. Those patterns cover nearly every interactive requirement.


