Skip to content

Instantly share code, notes, and snippets.

@mattlenz
Created March 1, 2012 01:02
Show Gist options
  • Save mattlenz/1946355 to your computer and use it in GitHub Desktop.
Save mattlenz/1946355 to your computer and use it in GitHub Desktop.
Centre align and element relative to parent (or window)
$.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