Front-End & Daily

[slick.js] How to add a slide reordering feature

[slick.js] How to add a slide reordering feature

This article explains how to add a slide reordering feature to the jQuery slider plugin "slick.js".

Example

Click "Move this slide left/right" to move the slide.

How to

Here's the HTML.

<div class="slider">
	<div class="slide">
		<p><img src="img01.jpg"></p>
		<button data-move="l">Move this slide left</button>
		<button data-move="r">Move this slide right</button>
	</div>
	<div class="slide">
		<p><img src="img02.jpg"></p>
		<button data-move="l">Move this slide left</button>
		<button data-move="r">Move this slide right</button>
	</div>
	<div class="slide">
		<p><img src="img03.jpg"></p>
		<button data-move="l">Move this slide left</button>
		<button data-move="r">Move this slide right</button>
	</div>
	<div class="slide">
		<p><img src="img04.jpg"></p>
		<button data-move="l">Move this slide left</button>
		<button data-move="r">Move this slide right</button>
	</div>
</div>

And here's the JS.

$(function(){
	// slick definition
	function slick_start(){
		$(".slider").slick({
			autoplay: true,
			dots: true,
			centerMode: true,
			variableWidth: true
		});
		// Reordering definition
		slick_move();
	}
	// First execution
	slick_start();

	// Reordering definition
	function slick_move(){
		// Remove event
		$(".slider .slide button").off();
		// When move button is clicked
		$(".slider .slide button").on("click", function(){
			// Add class to slide to be moved
			$(this).closest(".slide").addClass("_current");
			// Get data-move attribute
			let to = $(this).attr("data-move");
			// Unslick
			$(".slider.slick-initialized").slick("unslick");
			// Get HTML of slide to be moved
			let slideHTML = $(".slider .slide._current").prop("outerHTML");
			// When "left" is clicked
			if(to == "l"){
				// Get previous element
				let prev = $(".slider .slide._current").prev();
				// Remove slide to be moved
				$(".slider .slide._current").remove();
				// If there is a previous element
				if(prev.length){
					// Insert before previous element
					prev.before(slideHTML);
				}else{
					// If there is no previous element, insert at the end
					$(".slider .slide:last-child").after(slideHTML);
				}
			}else{
				// When "right" is clicked
				// Get next element
				let next = $(".slider .slide._current").next();
				// Remove slide to be moved
				$(".slider .slide._current").remove();
				// If there is a next element
				if(next.length){
					// Insert after next element
					next.after(slideHTML);
				}else{
					// If there is no next element, insert at the beginning
					$(".slider .slide:first-child").before(slideHTML);
				}
			}
			// Remove ._current
			$(".slider .slide._current").removeClass("_current");
			// Remove slick attributes
			$(".slider .slide").removeAttr("tabindex role id aria-describedby aria-hidden")
			// Reslick
			slick_start();
		});
	}
});

It might look a bit messy, but essentially,

When a button is clicked, slick is first "unslicked", the slides are reordered, and then slick is re-initialized.

Comments

We also welcome reports such as "It worked!".

After reviewing the content, we will publish it, omitting any personal information.

Enter your name and email address

Please enter if you would like a reply by email.

We will not disclose any personal information provided. It will only be used for replies.

It will be sent directly. Please confirm and click "Send".

If this was helpful, we appreciate your support!
Any support received will be used for childcare.

Send support via OFUSE


Alternatively, buy something to support us using the buttons below
(You don't have to buy the linked product.)

Amazon

Rakuten Ichiba

Yahoo! Shopping

PR

As an Amazon Associate, "Ken" earns income from qualifying purchases.

Share

Share on Twitter Share on Facebook Share on LINE Share on Hatena Bookmark