Advertisement
afsarwebdev

Sticky sidebar

Sep 10th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.94 KB | None | 0 0
  1. //Sticky sidebar
  2. //Html
  3. <div class="main-content" data-sticky_parent>
  4. <div class="right-content" data-sticky_column>Right content goes here. Place more content than sticky sidebar content.</div>
  5. <div class="right-sidebar sticky" data-sticky_column>Right sidebar which is sticky</div>
  6. </div>
  7.  
  8. //JS
  9. // Generated by CoffeeScript 1.6.2
  10. // http://leafo.net/sticky-kit/
  11. /**
  12. @license Sticky-kit v1.1.2 | MIT | Leaf Corcoran 2015 | http://leafo.net
  13. */
  14.  
  15. if ($(window).width() > 992) {
  16. (function() {
  17. var $, win;
  18.  
  19. $ = this.jQuery || window.jQuery;
  20.  
  21. win = $(window);
  22.  
  23. $.fn.stick_in_parent = function(opts) {
  24. var doc, elm, enable_bottoming, inner_scrolling, manual_spacer, offset_top, outer_width, parent_selector, recalc_every, sticky_class, _fn, _i, _len;
  25.  
  26. if (opts == null) {
  27. opts = {};
  28. }
  29. sticky_class = opts.sticky_class, inner_scrolling = opts.inner_scrolling, recalc_every = opts.recalc_every, parent_selector = opts.parent, offset_top = opts.offset_top, manual_spacer = opts.spacer, enable_bottoming = opts.bottoming;
  30. if (offset_top == null) {
  31. offset_top = 0;
  32. }
  33. if (parent_selector == null) {
  34. parent_selector = void 0;
  35. }
  36. if (inner_scrolling == null) {
  37. inner_scrolling = true;
  38. }
  39. if (sticky_class == null) {
  40. sticky_class = "is_stuck";
  41. }
  42. doc = $(document);
  43. if (enable_bottoming == null) {
  44. enable_bottoming = true;
  45. }
  46. outer_width = function(el) {
  47. var computed, w, _el;
  48.  
  49. if (window.getComputedStyle) {
  50. _el = el[0];
  51. computed = window.getComputedStyle(el[0]);
  52. w = parseFloat(computed.getPropertyValue("width")) + parseFloat(computed.getPropertyValue("margin-left")) + parseFloat(computed.getPropertyValue("margin-right"));
  53. if (computed.getPropertyValue("box-sizing") !== "border-box") {
  54. w += parseFloat(computed.getPropertyValue("border-left-width")) + parseFloat(computed.getPropertyValue("border-right-width")) + parseFloat(computed.getPropertyValue("padding-left")) + parseFloat(computed.getPropertyValue("padding-right"));
  55. }
  56. return w;
  57. } else {
  58. return el.outerWidth(true);
  59. }
  60. };
  61. _fn = function(elm, padding_bottom, parent_top, parent_height, top, height, el_float, detached) {
  62. var bottomed, detach, fixed, last_pos, last_scroll_height, offset, parent, recalc, recalc_and_tick, recalc_counter, spacer, tick;
  63.  
  64. if (elm.data("sticky_kit")) {
  65. return;
  66. }
  67. elm.data("sticky_kit", true);
  68. last_scroll_height = doc.height();
  69. parent = elm.parent();
  70. if (parent_selector != null) {
  71. parent = parent.closest(parent_selector);
  72. }
  73. if (!parent.length) {
  74. throw "failed to find stick parent";
  75. }
  76. fixed = false;
  77. bottomed = false;
  78. spacer = manual_spacer != null ? manual_spacer && elm.closest(manual_spacer) : $("<div />");
  79. if (spacer) {
  80. spacer.css('position', elm.css('position'));
  81. }
  82. recalc = function() {
  83. var border_top, padding_top, restore;
  84.  
  85. if (detached) {
  86. return;
  87. }
  88. last_scroll_height = doc.height();
  89. border_top = parseInt(parent.css("border-top-width"), 10);
  90. padding_top = parseInt(parent.css("padding-top"), 10);
  91. padding_bottom = parseInt(parent.css("padding-bottom"), 10);
  92. parent_top = parent.offset().top + border_top + padding_top;
  93. parent_height = parent.height();
  94. if (fixed) {
  95. fixed = false;
  96. bottomed = false;
  97. if (manual_spacer == null) {
  98. elm.insertAfter(spacer);
  99. spacer.detach();
  100. }
  101. elm.css({
  102. position: "",
  103. top: "",
  104. width: "",
  105. bottom: ""
  106. }).removeClass(sticky_class);
  107. restore = true;
  108. }
  109. top = elm.offset().top - (parseInt(elm.css("margin-top"), 10) || 0) - offset_top;
  110. height = elm.outerHeight(true);
  111. el_float = elm.css("float");
  112. if (spacer) {
  113. spacer.css({
  114. width: outer_width(elm),
  115. height: height,
  116. display: elm.css("display"),
  117. "vertical-align": elm.css("vertical-align"),
  118. "float": el_float
  119. });
  120. }
  121. if (restore) {
  122. return tick();
  123. }
  124. };
  125. recalc();
  126. if (height === parent_height) {
  127. return;
  128. }
  129. last_pos = void 0;
  130. offset = offset_top;
  131. recalc_counter = recalc_every;
  132. tick = function() {
  133. var css, delta, recalced, scroll, will_bottom, win_height;
  134.  
  135. if (detached) {
  136. return;
  137. }
  138. recalced = false;
  139. if (recalc_counter != null) {
  140. recalc_counter -= 1;
  141. if (recalc_counter <= 0) {
  142. recalc_counter = recalc_every;
  143. recalc();
  144. recalced = true;
  145. }
  146. }
  147. if (!recalced && doc.height() !== last_scroll_height) {
  148. recalc();
  149. recalced = true;
  150. }
  151. scroll = win.scrollTop();
  152. if (last_pos != null) {
  153. delta = scroll - last_pos;
  154. }
  155. last_pos = scroll;
  156. if (fixed) {
  157. if (enable_bottoming) {
  158. will_bottom = scroll + height + offset > parent_height + parent_top;
  159. if (bottomed && !will_bottom) {
  160. bottomed = false;
  161. elm.css({
  162. position: "fixed",
  163. bottom: "",
  164. top: offset
  165. }).trigger("sticky_kit:unbottom");
  166. }
  167. }
  168. if (scroll < top) {
  169. fixed = false;
  170. offset = offset_top;
  171. if (manual_spacer == null) {
  172. if (el_float === "left" || el_float === "right") {
  173. elm.insertAfter(spacer);
  174. }
  175. spacer.detach();
  176. }
  177. css = {
  178. position: "",
  179. width: "",
  180. top: ""
  181. };
  182. elm.css(css).removeClass(sticky_class).trigger("sticky_kit:unstick");
  183. }
  184. if (inner_scrolling) {
  185. win_height = win.height();
  186. if (height + offset_top > win_height) {
  187. if (!bottomed) {
  188. offset -= delta;
  189. offset = Math.max(win_height - height, offset);
  190. offset = Math.min(offset_top, offset);
  191. if (fixed) {
  192. elm.css({
  193. top: offset + "px"
  194. });
  195. }
  196. }
  197. }
  198. }
  199. } else {
  200. if (scroll > top) {
  201. fixed = true;
  202. css = {
  203. position: "fixed",
  204. top: offset
  205. };
  206. css.width = elm.css("box-sizing") === "border-box" ? elm.outerWidth() + "px" : elm.width() + "px";
  207. elm.css(css).addClass(sticky_class);
  208. if (manual_spacer == null) {
  209. elm.after(spacer);
  210. if (el_float === "left" || el_float === "right") {
  211. spacer.append(elm);
  212. }
  213. }
  214. elm.trigger("sticky_kit:stick");
  215. }
  216. }
  217. if (fixed && enable_bottoming) {
  218. if (will_bottom == null) {
  219. will_bottom = scroll + height + offset > parent_height + parent_top;
  220. }
  221. if (!bottomed && will_bottom) {
  222. bottomed = true;
  223. if (parent.css("position") === "static") {
  224. parent.css({
  225. position: "relative"
  226. });
  227. }
  228. return elm.css({
  229. position: "absolute",
  230. bottom: padding_bottom,
  231. top: "auto"
  232. }).trigger("sticky_kit:bottom");
  233. }
  234. }
  235. };
  236. recalc_and_tick = function() {
  237. recalc();
  238. return tick();
  239. };
  240. detach = function() {
  241. detached = true;
  242. win.off("touchmove", tick);
  243. win.off("scroll", tick);
  244. win.off("resize", recalc_and_tick);
  245. $(document.body).off("sticky_kit:recalc", recalc_and_tick);
  246. elm.off("sticky_kit:detach", detach);
  247. elm.removeData("sticky_kit");
  248. elm.css({
  249. position: "",
  250. bottom: "",
  251. top: "",
  252. width: ""
  253. });
  254. parent.position("position", "");
  255. if (fixed) {
  256. if (manual_spacer == null) {
  257. if (el_float === "left" || el_float === "right") {
  258. elm.insertAfter(spacer);
  259. }
  260. spacer.remove();
  261. }
  262. return elm.removeClass(sticky_class);
  263. }
  264. };
  265. win.on("touchmove", tick);
  266. win.on("scroll", tick);
  267. win.on("resize", recalc_and_tick);
  268. $(document.body).on("sticky_kit:recalc", recalc_and_tick);
  269. elm.on("sticky_kit:detach", detach);
  270. return setTimeout(tick, 0);
  271. };
  272. for (_i = 0, _len = this.length; _i < _len; _i++) {
  273. elm = this[_i];
  274. _fn($(elm));
  275. }
  276. return this;
  277. };
  278.  
  279. }).call(this);
  280.  
  281. // Generated by CoffeeScript 1.9.2
  282. (function() {
  283. var reset_scroll;
  284.  
  285. $(function() {
  286. return $("[data-sticky_column]").stick_in_parent({
  287. parent: "[data-sticky_parent]"
  288. });
  289. });
  290.  
  291. reset_scroll = function() {
  292. var scroller;
  293. scroller = $("body,html");
  294. scroller.stop(true);
  295. if ($(window).scrollTop() !== 0) {
  296. scroller.animate({
  297. scrollTop: 0
  298. }, "fast");
  299. }
  300. return scroller;
  301. };
  302.  
  303. window.scroll_it = function() {
  304. var max;
  305. max = $(document).height() - $(window).height();
  306. return reset_scroll().animate({
  307. scrollTop: max
  308. }, max * 3).delay(100).animate({
  309. scrollTop: 0
  310. }, max * 3);
  311. };
  312.  
  313. window.scroll_it_wobble = function() {
  314. var max, third;
  315. max = $(document).height() - $(window).height();
  316. third = Math.floor(max / 3);
  317. return reset_scroll().animate({
  318. scrollTop: third * 2
  319. }, max * 3).delay(100).animate({
  320. scrollTop: third
  321. }, max * 3).delay(100).animate({
  322. scrollTop: max
  323. }, max * 3).delay(100).animate({
  324. scrollTop: 0
  325. }, max * 3);
  326. };
  327.  
  328. $(window).on("resize", (function(_this) {
  329. return function(e) {
  330. return $(document.body).trigger("sticky_kit:recalc");
  331. };
  332. })(this));
  333.  
  334. }).call(this);
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement