Front-End & Daily

[bxSlider] How to add a pause button or pause on hover

[bxSlider] How to add a pause button or pause on hover

Here's how to add pause/play buttons and how to pause the slider on hover in the jQuery slider plugin "bxSlider".

Table of Contents

Pause/Play with Standard Features

First, let's look at how to add pause/play buttons using bxSlider's standard features.

You can add pause/play buttons to the bottom right by simply adding an option.

Just set the "autoControls" option to true.

$("#bxslider").bxSlider({
	auto: true,
	autoControls: true, //Add pause/play buttons
});

However, with this method, you cannot freely customize the HTML, so if you want to customize the appearance, please see the next method.

How to Add Your Own Pause/Play Buttons

How to add your own customized buttons. Try clicking the pause/play buttons.

The HTML looks like this. Prepare the slider and the pause/play buttons.

<div id="bxslider">
	<div class="slide"><img src="img01.jpg"></div>
	<div class="slide"><img src="img02.jpg"></div>
	<div class="slide"><img src="img03.jpg"></div>
	<div class="slide"><img src="img04.jpg"></div>
</div>

<button id="bxslider_pause">Pause</button>
<button id="bxslider_start">Play</button>

The pause/play buttons don't necessarily have to be <button> tags.

And here's the essential JavaScript.

const slider = $("#bxslider").bxSlider({
	//Options as you like
	auto: true
});

//Pause
$("#bxslider_pause").on("click", function(){
	slider.stopAuto();
});

//Play
$("#bxslider_start").on("click", function(){
	slider.startAuto();
});

Set bxSlider to the variable slider.
Clicking pause will pause with slider.stopAuto();
Clicking play will resume with slider.startAuto();

Toggle Pause/Play with a Single Button

Here's how to add such a button.

For HTML, prepare the slider and one button.

<div id="bxslider">
	<div class="slide"><img src="img01.jpg"></div>
	<div class="slide"><img src="img02.jpg"></div>
	<div class="slide"><img src="img03.jpg"></div>
	<div class="slide"><img src="img04.jpg"></div>
</div>

<button id="bxslider_pause">Pause/Play</button>

In JavaScript, toggle pause/play by adding or removing the .paused class from the button.

const slider = $("#bxslider").bxSlider({
	auto: true,
	pause: 1500
});

//When the button is clicked
$("#bxslider_pause").on("click", function(){
	if($(this).hasClass("paused")){
		//When paused
		//Resume
		slider.startAuto();
		//Remove .paused
		$(this).removeClass("paused");
	}else{
		//When playing
		//Pause
		slider.stopAuto();
		//Add paused
		$(this).addClass("paused");
	}
});

How to Pause on Hover

How to pause the slider while hovering over it with the mouse.

Other slider plugins like "slick" do this by default.

It should pause on hover.

HTML. Wrap bxslider in another div.

<div class="bx_wrap">
	<div id="bxslider">
		<div class="slide"><img src="img01.jpg"></div>
		<div class="slide"><img src="img02.jpg"></div>
		<div class="slide"><img src="img03.jpg"></div>
		<div class="slide"><img src="img04.jpg"></div>
	</div>
</div>

In JavaScript, pause when the mouse pointer enters .bx_wrap. Resume when it leaves.

const slider = $("#bxslider").bxSlider({
	auto: true
});

$('.bx_wrap').mouseover(function(e) {
	//Pause
	slider.stopAuto();
}).mouseout(function(e) {
	//Resume
	slider.startAuto();
});

Comments

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

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

Enter your name and email address

Please enter if you would like a reply by email.

Personal information provided will not be disclosed. It will only be used for replies.

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

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

Send support via OFUSE


Or support us by buying something from the buttons below
(You don't have to buy the linked product.)

Amazon

Rakuten Ichiba

Yahoo! Shopping

PR

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

Share

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