Skip to content

Instantly share code, notes, and snippets.

@shohagbhuiyan
Last active October 11, 2017 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shohagbhuiyan/00ae297ddbe92e445f79af81ee79c7d9 to your computer and use it in GitHub Desktop.
Save shohagbhuiyan/00ae297ddbe92e445f79af81ee79c7d9 to your computer and use it in GitHub Desktop.
//It will work for the element that has position: relative
function postionMiddle(e) {
var capHeightHalf = $(e).innerHeight() / 2,
capParentHeightHalf = $(e).parent().innerHeight() / 2,
capPosition = capParentHeightHalf - capHeightHalf;
$(e).css("top", capPosition );
}
postionMiddle(".caption"); // place your element instead .caption
//If you want to pass multiple value inside the function or want to plus/minus from the output value
/* function postionMiddle(e, v) {
var capHeightHalf = $(e).innerHeight() / 2,
capParentHeightHalf = $(e).parent().innerHeight() / 2,
capPosition = capParentHeightHalf - capHeightHalf;
if (v == null){
$(e).css("top", capPosition );
}else{
var capPosition2 = capPosition - v;
$(e).css("top", capPosition2 );
}
}
postionMiddle(".caption");
postionMiddle("nav", 32);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment