Guest User

Untitled

a guest
Jan 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. $.fn.centerInClient = function(options) {
  2. /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
  3. /// Ideally the selected set should only match a single element.
  4. /// </summary>
  5. /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>
  6. /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow
  7. /// and attached to the body element to ensure proper absolute positioning.
  8. /// Be aware that this may cause ID hierachy for CSS styles to be affected.
  9. /// </param>
  10. /// <returns type="jQuery" />
  11. var opt = { forceAbsolute: false,
  12. container: window, // selector of element to center in
  13. completeHandler: null
  14. };
  15. $.extend(opt, options);
  16.  
  17. return this.each(function(i) {
  18. var el = $(this);
  19. var jWin = $(opt.container);
  20. var isWin = opt.container == window;
  21.  
  22. // force to the top of document to ENSURE that
  23. // document absolute positioning is available
  24. if (opt.forceAbsolute) {
  25. if (isWin)
  26. el.remove().appendTo("body");
  27. else
  28. el.remove().appendTo(jWin.get(0));
  29. }
  30.  
  31. // have to make absolute
  32. el.css("position", "absolute");
  33.  
  34. // height is off a bit so fudge it
  35. var heightFudge = isWin ? 2.0 : 1.8;
  36.  
  37. var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
  38. var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
  39.  
  40. el.css("left", x + jWin.scrollLeft());
  41. el.css("top", y + jWin.scrollTop());
  42.  
  43. // if specified make callback and pass element
  44. if (opt.completeHandler)
  45. opt.completeHandler(this);
  46. });
  47. }
Add Comment
Please, Sign In to add comment