Skip to content

Instantly share code, notes, and snippets.

@connected
Last active October 12, 2022 23:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connected/f907145b51a033aa13e0fbfe0ffaed74 to your computer and use it in GitHub Desktop.
Save connected/f907145b51a033aa13e0fbfe0ffaed74 to your computer and use it in GitHub Desktop.
bxSlider mouse drag extension
/**
* Include this code right after bxSlider.
* Use "mouseDrag" options upon bxSlider initialization to enable mouse drag:
* $('#slider').bxSlider({
* mouseDrag: true
* });
*/
(function ($) {
var bxSlider = jQuery.fn.bxSlider;
var $window = $(window);
jQuery.fn.bxSlider = function () {
var slider = bxSlider.apply(this, arguments);
if (!this.length || !arguments[0].mouseDrag) {
return slider;
}
var posX;
var $viewport = this.parents('.bx-viewport');
$viewport
.on('dragstart', dragHandler)
.on('mousedown', mouseDownHandler);
function dragHandler(e) {
e.preventDefault();
}
function mouseDownHandler(e) {
posX = e.pageX;
$window.on('mousemove.bxSlider', mouseMoveHandler);
}
function mouseMoveHandler(e) {
if (posX < e.pageX) {
slider.goToPrevSlide();
} else {
slider.goToNextSlide();
}
$window.off('mousemove.bxSlider');
}
return slider;
};
})(jQuery);
@paulakfleck
Copy link

Hi!

It only goes to right, or am I doing something wrong?
Thank you for this extension.

@derSteffen
Copy link

On the Desktop there is no effect by using the MouseMove!

@dangercodes
Copy link

add new params in bxSlider options "mouseDrag" and set this "true"

@makinero
Copy link

Any example to show ?

@Danish8321
Copy link

Nothing is happening...
any reference...

@tridoan
Copy link

tridoan commented May 12, 2017

This doesnt work... :(

Copy link

ghost commented Jun 14, 2017

it's working perfectly
Thanks

@mathews8881
Copy link

Perfect. Thanks dude!
I added the CSS for the particular element to disable the selection while moving:
{-webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none;-ms-user-select: none; user-select: none; cursor:pointer; cursor:hand;}

@kyiwinhtin
Copy link

It doesn't work on google chorme when i drag two slide move
In firefox perfectly fine

@mttodorov
Copy link

It works, but it works only with one-click inside the slider, there is a bug when you click and hold and try actually to drag the slides - the text gets selected. How can I prevent this to happen?

@logicfire
Copy link

It works well but when you resize the window it doesn't work anymore.
any solution for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment