Skip to content

Instantly share code, notes, and snippets.

@yuryshulaev
Last active May 11, 2020 04:42
Show Gist options
  • Save yuryshulaev/fce36d11a1a1adc04d49 to your computer and use it in GitHub Desktop.
Save yuryshulaev/fce36d11a1a1adc04d49 to your computer and use it in GitHub Desktop.
Greasemonkey drag-style scrolling userscript
// ==UserScript==
// @name DragScroll
// @include *
// @version 1
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==
(function ($) {
var dragging = false;
var initialLeft, initialTop, initialX, initialY;
var scrollable = $(window);
var button = 3;
scrollable.contextmenu(function (e) {
return false;
});
scrollable.mousedown(function (e) {
if (e.which !== button) {
return true;
}
dragging = true;
initialLeft = $(this).scrollLeft();
initialTop = $(this).scrollTop();
initialX = e.clientX;
initialY = e.clientY;
return false;
});
scrollable.mousemove(function (e) {
if (dragging) {
$(this).scrollLeft(initialLeft - (e.clientX - initialX));
$(this).scrollTop(initialTop - (e.clientY - initialY));
return false;
}
});
scrollable.mouseup(function (e) {
if (e.which !== button) {
return true;
}
dragging = false;
return false;
});
})(jQuery.noConflict(true));
@Noomene
Copy link

Noomene commented May 8, 2020

Hey yuryshulaev,
This script is amazing. I tried another one on greasemonkey with palemoon and the scrolling speed basically depended on the page. But this one works perfectly out of the box.
Now ... I need your help on how to configure the mouse button used to dragscroll. How can I change it from right button to left button.
Also, is there a disable drag button, like ctrl for example?
Thanks a lot man ... this is some really great work in so few lines.

@yuryshulaev
Copy link
Author

yuryshulaev commented May 9, 2020

Hi. You can change it to use the left mouse button by replacing if (e.which !== 3) { with if (e.which !== 1) { on line 19. To also disable it when Ctrl is being held you can change this line to if (e.which !== 1 || e.ctrlKey) {. You might also want to remove the scrollable.contextmenu part (lines 14 to 16) if you use the left button and don’t want to disable context menus.

@yuryshulaev
Copy link
Author

yuryshulaev commented May 9, 2020

I have also used this addon: https://addons.mozilla.org/ru/firefox/addon/scroll_anywhere/ — it has a lot of settings.

@Noomene
Copy link

Noomene commented May 10, 2020

Thanks a lot mate. This || e.ctrlKey is what I was looking for.

However, I was already using that scroll anywhere add on on waterfox, but since I switched to pale moon, it didn't work there and greasemonkey seemed like the only solution. But then someone directed to an old repo of old firefox addons and I found a good alternative to it for pale moon.

Thanks a lot again for your reply mate, I appreciate your help and this little gem of script.

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