Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. (function ( $ ) {
  2.  
  3. /**
  4. * A jQuery plugin that will position an element relative to another element, regardles of whether or not they share the
  5. * same parent in the DOM.
  6. *
  7. * Note: This must be called within a $(document).ready() call to work properly. If loading images in the element
  8. * that aren't specifically sized via CSS, it may be necessary to call this within a $(window).load() call
  9. * depending on the positioning used.
  10. *
  11. * @param $el The element off of which relative positioning should be based.
  12. * @param top Defines top positioning, which can be a number, percentage or 'centerline'.
  13. * @param left Defines left positioning, which can be a number, percentage or 'centerline'.
  14. * @returns {*} Returns the original element so that chaining works properly.
  15. */
  16. $.fn.positionRelativeTo = function ( $el, top, left ) {
  17.  
  18. var isPct = function ( value ) {
  19. return isNaN(value) ? '%' === value.substr(- 1) : false;
  20. };
  21.  
  22. var convertPercentageToDecimal = function ( percentage ) {
  23. var num = isNaN(percentage) ? percentage.replace('%', '') : percentage;
  24. return isNaN(num) ? 0 : parseInt(num, 10) / 100;
  25. };
  26.  
  27. var getPosition = function ( $this ) {
  28.  
  29. var position = { top: 0, left: 0 };
  30.  
  31. // Top
  32. if ( typeof top === 'undefined' ) {
  33. position.top = $el.offset().top;
  34. } else if ( ! isNaN(top) ) {
  35. position.top = $el.offset().top + parseInt(top, 10);
  36. } else if ( isPct(top) ) {
  37. position.top = $el.offset().top + ( $el.outerHeight() * convertPercentageToDecimal(top) );
  38. } else if ( 'centerline' === top ) {
  39. position.top = Math.round(( $el.offset().top + ( $el.outerHeight() / 2 ) ) - ( $this.outerHeight() / 2 ));
  40. }
  41.  
  42. // Left
  43. if ( typeof left === 'undefined' ) {
  44. position.left = $el.offset().left;
  45. } else if ( ! isNaN(left) ) {
  46. position.left = $el.offset().left + parseInt(left, 10);
  47. } else if ( isPct(left) ) {
  48. position.left = $el.offset().left + ( $el.outerWidth() * convertPercentageToDecimal(left) );
  49. } else if ( 'centerline' === left ) {
  50. position.left = Math.round(( $el.offset().left + ( $el.outerWidth() / 2 ) ) - ( $this.outerWidth() / 2 ));
  51. }
  52.  
  53. return position;
  54. };
  55.  
  56. return this.each(function () {
  57.  
  58. if ( 1 === $el.length ) {
  59.  
  60. var $self = $(this);
  61.  
  62. $self.detach().appendTo('body').css('position', 'absolute');
  63.  
  64. $self.offset(getPosition($self));
  65.  
  66. $(window).resize(function () {
  67. $self.offset(getPosition($self));
  68. });
  69.  
  70. }
  71.  
  72. });
  73.  
  74. };
  75.  
  76. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement