Requires

Provides

Element.Pin.js

Extends the Element native object to include the pin method useful for fixed positioning for elements.

License:
MIT-style license
Authors:
Aaron Newton
  1. 27
  2. 28
  3. 29
  4. 30
  5. 31
  6. 32
  7. 33
  8. 34
  9. 35
  10. 36
  11. 37
  12. 38
  13. 39
  14. 40
  15. 41
  16. 42
  17. 43
  18. 44
  19. 45
  20. 46
  21. 47
  22. 48
  23. 49
  24. 50
  25. 51
  26. 52
  27. 53
  28. 54
  29. 55
  30. 56
  31. 57
  32. 58
  33. 59
  34. 60
  35. 61
  36. 62
  37. 63
  38. 64
  39. 65
  40. 66
  41. 67
  42. 68
  43. 69
  44. 70
  45. 71
  46. 72
  47. 73
  48. 74
  49. 75
  50. 76
  51. 77
  52. 78
  53. 79
  54. 80
  55. 81
  56. 82
  57. 83
  58. 84
  59. 85
  60. 86
  61. 87
  62. 88
  63. 89
  64. 90
  65. 91
  66. 92
  67. 93
  68. 94
  69. 95
  70. 96
  71. 97
  72. 98
  73. 99
  74. 100
  75. 101
  76. 102
  77. 103
  78. 104
  79. 105
  80. 106
  81. 107
  82. 108
  83. 109
  84. 110
  85. 111
  86. 112
  87. 113
  88. 114
  89. 115
  90. 116
  91. 117
  92. 118
  93. 119
  94. 120
(function(){ var supportsPositionFixed = false; window.addEvent('domready', function(){ var test = new Element('div').setStyles({ position: 'fixed', top: 0, right: 0 }).inject(document.body); supportsPositionFixed = (test.offsetTop === 0); test.dispose(); }); Element.implement({ pin: function(enable){ if (this.getStyle('display') == 'none') return null; var pinnedPosition, scroll = window.getScroll(); if (enable !== false){ pinnedPosition = this.getPosition(); if (!this.retrieve('pin:_pinned')){ var currentPosition = { top: pinnedPosition.y - scroll.y, left: pinnedPosition.x - scroll.x }; if (supportsPositionFixed){ this.setStyle('position', 'fixed').setStyles(currentPosition); } else { this.store('pin:_pinnedByJS', true); this.setStyles({ position: 'absolute', top: pinnedPosition.y, left: pinnedPosition.x }).addClass('isPinned'); var scrollFixer = function(){ if (this.retrieve('pin:_pinned')) var scroll = window.getScroll(); this.setStyles({ top: currentPosition.top.toInt() + scroll.y, left: currentPosition.left.toInt() + scroll.x }); }.bind(this); this.store('pin:_scrollFixer', scrollFixer); window.addEvent('scroll', scrollFixer); } this.store('pin:_pinned', true); } } else { var offsetParent; if (!Browser.ie){ var parent = this.getParent(); offsetParent = (parent.getComputedStyle('position') != 'static' ? parent : parent.getOffsetParent()); } pinnedPosition = this.getPosition(offsetParent); this.store('pin:_pinned', false); var reposition; if (supportsPositionFixed && !this.retrieve('pin:_pinnedByJS')){ reposition = { top: pinnedPosition.y + scroll.y, left: pinnedPosition.x + scroll.x }; } else { this.store('pin:_pinnedByJS', false); window.removeEvent('scroll', this.retrieve('pin:_scrollFixer')); reposition = { top: pinnedPosition.y, left: pinnedPosition.x }; } this.setStyles(Object.merge(reposition, {position: 'absolute'})).removeClass('isPinned'); } return this; }, unpin: function(){ return this.pin(false); }, togglepin: function(){ this.pin(!this.retrieve('pin:_pinned')); } }); })();