Advertisement
asimryu

center an element with jQuery

Nov 12th, 2019
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function elementCenter(id){
  2.     var obj = $("#"+id);
  3.     obj.css("position","absolute");
  4.     obj.css("top", Math.max(0, (($(window).height() - obj.outerHeight()) / 2) + $(window).scrollTop()) + "px");
  5.     obj.css("left", Math.max(0, (($(window).width() - obj.outerWidth()) / 2) + $(window).scrollLeft()) + "px");
  6. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement