Skip to content

Instantly share code, notes, and snippets.

@jaycliff
Last active August 29, 2015 14:14
Show Gist options
  • Save jaycliff/fae8fd2470d45771c11d to your computer and use it in GitHub Desktop.
Save jaycliff/fae8fd2470d45771c11d to your computer and use it in GitHub Desktop.
/*
Copyright 2015 Jaycliff Arcilla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*jslint browser: true, devel: true, bitwise: true, nomen: true, unparam: true, sub: true, regexp: true */
/*global jQuery, window, document, setTimeout, clearTimeout*/
/* https://gist.github.com/jaycliff/fae8fd2470d45771c11d */
(function ($) {
"use strict";
var eacher;
function getDisplayType(element) {
var cStyle = element.currentStyle || window.getComputedStyle(element, '');
return cStyle.display;
}
function wrapItWithThis(element, wrapper) {
wrapper.appendChild(element.parentNode.replaceChild(wrapper, element));
}
eacher = function () {
var $this = $(this), x = eacher.option, css_options;
if (!$this.is(':visible') && x !== 1) {
return;
}
if (!$.data(this, 'wrapper')) {
switch (getDisplayType(this)) {
case 'block':
$.data(this, 'wrapper', $('<div class="jquery-scale-container">').css('position', 'relative'));
break;
case 'inline-block':
$.data(this, 'wrapper', $('<span class="jquery-scale-container">').css('position', 'relative').css('display', 'inline-block'));
break;
case 'inline':
$.data(this, 'wrapper', $('<span class="jquery-scale-container">').css('position', 'relative').css('display', 'inline-block'));
break;
}
}
if (!$.data(this, 'css_options')) {
$.data(this, 'css_options', {
'transform-origin': 'left top',
'-ms-transform-origin': 'left top',
'-moz-transform-origin': 'left top',
'-webkit-transform-origin': 'left top',
'position': 'absolute',
'top': '0',
'left': '0'
});
}
css_options = $.data(this, 'css_options');
css_options.transform = 'scale(' + x + ')';
css_options['-ms-transform'] = 'scale(' + x + ')';
css_options['-moz-transform'] = 'scale(' + x + ')';
css_options['-webkit-transform'] = 'scale(' + x + ')';
if (this.parentNode !== $.data(this, 'wrapper').get(0)) {
wrapItWithThis(this, $.data(this, 'wrapper').get(0));
}
$this.css(css_options);
$.data(this, 'original_width', $this.width());
$.data(this, 'original_height', $this.height());
if (x === 1) {
$this.unwrap().css('position', 'static');
} else {
$.data(this, 'wrapper')
.width(Math.floor($this.data('original_width') * x))
.height(Math.floor($this.data('original_height') * x));
}
};
$.fn.scale = function (x) {
eacher.option = x;
return this.each(eacher);
};
}(window.jQuery || (window.module && window.module.exports)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment