Pause Resume Slider using jQuery

In a previous tutorial, we have seen about how to create jQuery image slider. In this tutorial, we are adding pause resume control to this slider.

We are handling slider pause, resume functions in the mouse over, mouse out events respectively. In this example, we have a boolean variable as a flag based on which the slider’s pause and resume functionalities are handled.

View Demo

jQuery Pause Resume Action

The HTML code for the slider window is same what we have seen in jQuery image slider tutorial. But, the jQuery function is slightly changed for supporting pause and resume action. The script is,

$.fn.startSlider = function(){
	var sliderDIV=this;
	var img=sliderDIV.find(".img-slide");
	
	sliderDIV.css({overflow:"auto"});
	img.css({position:'absolute'});
	img.hide().first().show().addClass("active");
	
	var pause = false;
	var iterateTickerElement = function() {
		setInterval(function(){
			if(!pause) {
			var next = $(".img-slide.active").next();
			$(".img-slide.active").hide("slide",{direction:'left'},2000);
			$(".img-slide.active").removeClass("active");
			if(next.length == 0) next = $(".img-slide").first();
			next.addClass("active");
			next.show("slide",{direction:'right'},1000);
			}
		},2000);
	}

	sliderDIV.hover(
	function(){
		pause=true;
	},
	function(){
		pause=false;
	});	
	iterateTickerElement();
}

View DemoDownload

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 12, 2022
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?