Skip to content

Instantly share code, notes, and snippets.

@Nosgoroth
Last active April 11, 2018 17:30
Show Gist options
  • Save Nosgoroth/10be0a21abf1517baea10cc88657d3a1 to your computer and use it in GitHub Desktop.
Save Nosgoroth/10be0a21abf1517baea10cc88657d3a1 to your computer and use it in GitHub Desktop.
Fits an image to window width and height whenever it changes (Part of a deprecated userscript for Mangadex)
function CenterHandler(){
this.$img = jQuery("#current_page");
this.$w = jQuery(window);
this.$w.on('resize orientationchange', this.center.bind(this));
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
this.observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === "attributes" && mutation.attributeName === "src") {
this.center();
}
}.bind(this));
}.bind(this));
this.observer.observe(this.$img.get(0), { attributes: true });
this.center();
setTimeout(this.center.bind(this), 1000);
this.applyGlobalCssChanges();
}
CenterHandler.prototype.applyGlobalCssChanges = function(){
//jQuery("#content > .row").css("margin-bottom", 0);
jQuery("#top_nav").css("position", "static");
jQuery("footer.footer").css({
"position": "static",
"margin-top": "30px"
});
jQuery("body").css({
"padding-top": 0,
"padding-bottom": 0,
"margin-bottom": 0
});
};
CenterHandler.prototype.center = function() {
var _i = this.$img.get(0),
iw = _i.naturalWidth,
ih = _i.naturalHeight,
iar = iw/ih,
ww = window.innerWidth,
wh = window.innerHeight,
war = ww/wh
;
if (war>iar) {
this.$img.css({
"width": "auto",
"height": wh+"px"
});
} else {
this.$img.css({
"width": ww+"px",
"height": "auto"
});
}
this.scroll();
};
CenterHandler.prototype.scroll = function() {
jQuery('html, body').animate({
scrollTop: this.$img.offset().top
}, 300);
};
(function() {
'use strict';
jQuery.noConflict();
jQuery(document).ready(function(){
window._center = new CenterHandler();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment