ui.core.position.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;(function($) {
  2. $.fn.extend({
  3. scrollParent: function() {
  4. var scrollParent;
  5. if(($.browser.msie && (/(static|relative)/).test(this.css('position'))) || (/absolute/).test(this.css('position'))) {
  6. scrollParent = this.parents().filter(function() {
  7. return (/(relative|absolute|fixed)/).test($.curCSS(this,'position',1)) && (/(auto|scroll)/).test($.curCSS(this,'overflow',1)+$.curCSS(this,'overflow-y',1)+$.curCSS(this,'overflow-x',1));
  8. }).eq(0);
  9. } else {
  10. scrollParent = this.parents().filter(function() {
  11. return (/(auto|scroll)/).test($.curCSS(this,'overflow',1)+$.curCSS(this,'overflow-y',1)+$.curCSS(this,'overflow-x',1));
  12. }).eq(0);
  13. }
  14. return (/fixed/).test(this.css('position')) || !scrollParent.length ? $(document) : scrollParent;
  15. }
  16. });
  17. $.fn.positionAround = function(event, o) {
  18. var options = $.extend({
  19. around: 'mouse',
  20. direction: 'default',
  21. forceDirection: false,
  22. offset: [0, 0]
  23. }, o);
  24. var leftOffset = 0,
  25. topOffset = 0,
  26. height = this[0].offsetHeight,
  27. width = this[0].offsetWidth,
  28. op = this.offsetParent(),
  29. sp = $(this.scrollParent().length ? this.scrollParent() : document.body),
  30. spBorderTop = parseInt(op.css('borderTopWidth'),10),
  31. spBorderLeft= parseInt(op.css('borderLeftWidth'),10),
  32. opBorderTop = parseInt(op.css('borderTopWidth'),10),
  33. opBorderLeft= parseInt(op.css('borderLeftWidth'),10),
  34. opOffset = op.offset()
  35. //spOffset = sp.offset()
  36. ;
  37. //Ugly fix for the issues of offset related to the body element
  38. opOffset = (/(html|body)/).test(op[0].tagName.toLowerCase()) ? { top: 0, left: 0 } : opOffset;
  39. spOffset = (/(html|body)/).test(op[0].tagName.toLowerCase()) ? { top: 0, left: 0 } : spOffset;
  40. var bottomEdge = (/(html|body)/).test(op[0].tagName.toLowerCase()) ? $(window).height() : spOffset.top + spBorderTop + sp.height();
  41. var rightEdge = (/(html|body)/).test(op[0].tagName.toLowerCase()) ? $(window).width() : spOffset.left + spBorderLeft + sp.width();
  42. if($(options.around).length && $(options.around)[0].nodeName) { //If around is an element
  43. var element = $(options.around),
  44. offset = element.offset(),
  45. relHeight = element[0].offsetHeight,
  46. relWidth = element[0].offsetWidth
  47. ;
  48. if((/(left|right)/).test(options.direction)) {
  49. leftOffset = ( options.direction == 'left' ? (offset.left - spOffset.left - spBorderLeft > width || options.forceDirection) : (rightEdge-offset.left-relWidth < width && !options.forceDirection) ) ? -(width) : relWidth;
  50. topOffset = event ? ( bottomEdge - offset.top < height ? -(height-relHeight) : 0 ) : 0;
  51. } else {
  52. topOffset = ( options.direction == 'above' ? (offset.top - spOffset.top - spBorderTop > height || options.forceDirection) : (bottomEdge-offset.top-relHeight < height && !options.forceDirection) ) ? -(height) : relHeight;
  53. }
  54. this.css({
  55. left: offset.left - opOffset.left - opBorderLeft + leftOffset + options.offset[0],
  56. top: offset.top - opOffset.top - opBorderTop + topOffset + options.offset[1]
  57. });
  58. } else {
  59. if((/(below|default)/).test(options.direction)) {
  60. topOffset = bottomEdge - event.pageY < height && !options.forceDirection ? -(height) : 0;
  61. } else if ((/above/).test(options.direction)) {
  62. topOffset = (event.pageY - spOffset.top - spBorderTop) > height || options.forceDirection ? -(height) : 0;
  63. }
  64. if((/(right|default)/).test(options.direction)) {
  65. leftOffset = rightEdge - event.pageX < width && !options.forceDirection ? -(width) : 0;
  66. } else if ((/left/).test(options.direction)) {
  67. leftOffset = (event.pageX - spOffset.left - spBorderLeft) > width || options.forceDirection ? -(width) : 0;
  68. }
  69. this.css({
  70. left: event.pageX - opOffset.left - opBorderLeft + leftOffset + options.offset[0],
  71. top: event.pageY - opOffset.top - opBorderTop + topOffset + options.offset[1]
  72. });
  73. }
  74. };
  75. })(jQuery);