Last active
October 12, 2022 23:37
-
-
Save connected/f907145b51a033aa13e0fbfe0ffaed74 to your computer and use it in GitHub Desktop.
bxSlider mouse drag extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works well but when you resize the window it doesn't work anymore.
any solution for it?