Last active
August 29, 2015 14:17
-
-
Save yesasha/488463fdc067faf6e9db to your computer and use it in GitHub Desktop.
Keep Aspect Ratio (new oop version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Keeps aspect ratio of an element by listening to window resize event | |
// and setting height = width / aspect ratio | |
// | |
// Copyright (c) 2015 Aleksandr Efremov | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
// Compress with GCC --compilation_level ADVANCED_OPTIMIZATIONS | |
// Usage: var myKeeper = new Keeper(); | |
// myKeeper.keep(ID, ratio); // Start keeper | |
// myKeeper.keep(); // Without any parameters - stop keeper and restore the initial height value | |
// | |
// I'm using underscores before property names, to make GCC to compress them | |
(function () { | |
// Using this as a namespace proxy | |
this['Keeper'] = Keeper; | |
function Keeper () {} | |
Keeper.prototype['keep'] = function (ID, ratio) { | |
// Stops previous listener if exists | |
window.removeEventListener('resize', this._handler, false); | |
// If started first time, there is no initial value | |
if (this._element) { | |
// If there is initial value, then it was already started with the ratio argument | |
this._element.style.height = this._initValue; | |
// Do it only once | |
this._element = null; | |
} | |
var element = document.getElementById(ID); | |
if (ratio && element) { | |
this._ratio = ratio; | |
this._element = element; | |
// Save the height value, to be able to restore it later | |
this._initValue = element.style.height; | |
// Trick to avoide closures | |
this._handler = this._origHandler.bind(this); | |
window.addEventListener('resize', this._handler); | |
// The callback will fire on resize, but we want it the first time to fire immediately | |
this._handler(); | |
} | |
}; | |
Keeper.prototype._origHandler = function () { | |
this._element.style.height = (parseFloat(getComputedStyle(this._element, null).width) / this._ratio) + 'px'; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment