Skip to content

Instantly share code, notes, and snippets.

@faridv
Forked from psdtohtml5/gist:5908031
Created June 17, 2018 07:48
Show Gist options
  • Save faridv/c595a687eb8fa065885d1f05d621f434 to your computer and use it in GitHub Desktop.
Save faridv/c595a687eb8fa065885d1f05d621f434 to your computer and use it in GitHub Desktop.
jQuery: Drag and scroll horizontal div
<script type="text/javascript">
$(document).ready(function() {
$('.dragger').mousedown(function (event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('scrollLeft', this.scrollLeft)
.addClass("dragging");
return false;
}).mouseup(function (event) {
$(this)
.data('down', false)
.removeClass("dragging");
}).mousemove(function (event) {
if ($(this).data('down') == true) {
this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
}
}).mousewheel(function (event, delta) {
this.scrollLeft -= (delta * 30);
}).css({
'overflow' : 'hidden',
'cursor' : '-moz-grab'
});
$(window).mouseout(function (event) {
if ($('.team-form-data').data('down')) {
try {
if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
$('.team-form-data').data('down', false);
}
} catch (e) {}
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment