Created
March 1, 2012 01:02
-
-
Save mattlenz/1946355 to your computer and use it in GitHub Desktop.
Centre align and element relative to parent (or window)
This file contains 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
$.fn.centre = (dims = ['left', 'top'], parent = window, offset = 0) -> | |
that = @ | |
_.each dims, (dim) -> | |
prop = 'width' if dim == 'left' | |
prop = 'height' if dim == 'top' | |
parent_prop = eval("$(parent).#{prop}()") | |
this_prop = eval("$(that).#{prop}()") | |
$(that).css("margin-#{dim}", (parent_prop - this_prop) / 2) if dim and parent_prop and this_prop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment