Skip to content

Instantly share code, notes, and snippets.

@oerpli
Last active June 18, 2017 14:15
Show Gist options
  • Save oerpli/5096ae2083a79d20d4862364d59ebb14 to your computer and use it in GitHub Desktop.
Save oerpli/5096ae2083a79d20d4862364d59ebb14 to your computer and use it in GitHub Desktop.
Image Zoom
// ==UserScript==
// @name WheelZoom
// @namespace http://oerpli.github.io/
// @version 0.1
// @description Right Click + Mouse wheel to change image size, Right Click + Middle Click to reset current image
// @author oerpli
// @match http://*/*
// @match https://*/*
// @grant none
// @require http://code.jquery.com/jquery-3.2.1.slim.min.js
// ==/UserScript==
(function() {
//sdfsdf 'use strict';
var currentlyActive = false;
var usedZoom = false;
var zoomElement = null;
var timeout;
var clicker = $('img');
clicker.on('mousedown',function(e){
if(e.which == 3){
timeout = setInterval(function(){
currentlyActive = true;
zoomElement = $(this);
}, 100);
}
return false;
});
$(document).mouseup(function(e){
clearInterval(timeout);
currentlyActive = false;
if(usedZoom){
e.preventDefault();
usedZoom = false;
}
return false;
});
$("img").on('mousedown', function(e) {
if(e.which == 3){ // right click
e.preventDefault();
this.addEventListener("mousewheel", ZoomElement, false);
if (!$(this).attr('origw')){
$(this).attr('origw',$(this).width());
$(this).attr('origh',$(this).height());
}
return false;
}
if(e.which == 2){ // middle click = reset to default
if($(this).attr('origw')){
e.preventDefault();
$(this).height($(this).attr('origh'));
$(this).width($(this).attr('origw'));
$(this).attr('origw','');
return false;
}
}
});
function ZoomElement(e, x){
if(!currentlyActive) return;
usedZoom = true;
var e = window.event || e;
e.preventDefault();
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
//scrolling down?
if(delta > 0)
ratio = 1.1;
else
ratio = 1.0/1.1;
newHeight = Math.floor($(this).height() * ratio);
newWidth = Math.floor($(this).width() * ratio);
$(this).height(newHeight);
$(this).width(newWidth);
return false;
}
function MouseWheelHandler() {
return function (e) {
// cross-browser wheel delta
var e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
//scrolling down?
if (delta < 0) {
return -1;
} else {
return +1;
}
return 0;
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment