Created
July 24, 2012 06:41
-
-
Save riix/3168417 to your computer and use it in GitHub Desktop.
객체를 부모 엘리먼트 중앙에 세로 정렬, vertical centering the matched elements
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
| //save this code as jquery.verticalAlign.js | |
| (function ($) { | |
| $.fn.extend({ | |
| verticalAlign: function () { | |
| //Iterate over the current set of matched elements | |
| return this.each(function () { | |
| var obj = $(this); | |
| // calculate the new padding and height | |
| var childHeight = obj.height(); | |
| var parentHeight = obj.parent().height(); | |
| var diff = Math.round(((parentHeight - childHeight) / 2)); | |
| // apply new values | |
| obj.css({ | |
| "display": "block", | |
| "margin-top": diff | |
| }); | |
| }); | |
| } | |
| }); | |
| })(jQuery); | |
| //use like this: | |
| $(document).ready(function ($) { | |
| $("#someDiv").verticalAlign(); | |
| }); | |
| //webkit browsers require window.load(): | |
| $(window).load(function ()) { | |
| $("#someDiv").verticalAlign(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment