MediaWiki:Gadget-VideoResizer.js: Difference between revisions
From the Super Mario Wiki, the Mario encyclopedia
Jump to navigationJump to search
(Created page with "→Automatically resize videos to fit the page: function videoResizer() { $('.autoResize').each(function(){ var body = $('#mw-content-text .mw-parser-output'); var sel...") |
No edit summary |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
/* Automatically resize videos to fit the page */ | /* Automatically resize videos to fit the page width */ | ||
if (typeof iframe.attr('data-orig-width') == 'undefined') { | function autoResizer() { | ||
$('#mw-content-text .autoResize').each(function() { | |||
var body = $('#mw-content-text .mw-parser-output'); | |||
var iframe = $(this).find('iframe'); | |||
if (typeof iframe.attr('data-orig-width') == 'undefined') { | |||
iframe.attr('data-orig-width', iframe.width()); | |||
iframe.attr('data-orig-height', iframe.height()); | |||
} | |||
var newWidth = $(this).parent().parent().is('section') ? body.width() : body.width() - 10; | |||
if (newWidth < iframe.attr('data-orig-width')) { | |||
var aspect = iframe.attr('data-orig-width') / iframe.attr('data-orig-height'); | |||
var newWidth = Math.ceil(newWidth); | |||
var newHeight = Math.floor(newWidth / aspect); | |||
$(this).css('width', newWidth); | |||
iframe.attr('width', newWidth); | |||
iframe.attr('height', newHeight); | |||
} else if (iframe.width() != iframe.attr('data-orig-width')) { | |||
$(this).css('width', iframe.attr('data-orig-width')); | |||
iframe.attr('width', iframe.attr('data-orig-width')); | |||
iframe.attr('height', iframe.attr('data-orig-height')); | |||
} | |||
}); | |||
} | } | ||
$( | $(autoResizer); | ||
$(window).resize(function() { | $(window).resize(function() { | ||
clearTimeout(window.resizeFinished); | |||
window.resizeFinished = setTimeout(function() { | |||
$(autoResizer); | |||
}, 100); | |||
}); | }); |
Latest revision as of 05:48, February 21, 2023
/* Automatically resize videos to fit the page width */
function autoResizer() {
$('#mw-content-text .autoResize').each(function() {
var body = $('#mw-content-text .mw-parser-output');
var iframe = $(this).find('iframe');
if (typeof iframe.attr('data-orig-width') == 'undefined') {
iframe.attr('data-orig-width', iframe.width());
iframe.attr('data-orig-height', iframe.height());
}
var newWidth = $(this).parent().parent().is('section') ? body.width() : body.width() - 10;
if (newWidth < iframe.attr('data-orig-width')) {
var aspect = iframe.attr('data-orig-width') / iframe.attr('data-orig-height');
var newWidth = Math.ceil(newWidth);
var newHeight = Math.floor(newWidth / aspect);
$(this).css('width', newWidth);
iframe.attr('width', newWidth);
iframe.attr('height', newHeight);
} else if (iframe.width() != iframe.attr('data-orig-width')) {
$(this).css('width', iframe.attr('data-orig-width'));
iframe.attr('width', iframe.attr('data-orig-width'));
iframe.attr('height', iframe.attr('data-orig-height'));
}
});
}
$(autoResizer);
$(window).resize(function() {
clearTimeout(window.resizeFinished);
window.resizeFinished = setTimeout(function() {
$(autoResizer);
}, 100);
});