1 | (function ($) {
|
2 | $.fn.pushpin = function (options) {
|
3 |
|
4 | var defaults = {
|
5 | top: 0,
|
6 | bottom: Infinity,
|
7 | offset: 0
|
8 | };
|
9 |
|
10 |
|
11 | if (options === "remove") {
|
12 | this.each(function () {
|
13 | if (id = $(this).data('pushpin-id')) {
|
14 | $(window).off('scroll.' + id);
|
15 | $(this).removeData('pushpin-id').removeClass('pin-top pinned pin-bottom').removeAttr('style');
|
16 | }
|
17 | });
|
18 | return false;
|
19 | }
|
20 |
|
21 | options = $.extend(defaults, options);
|
22 |
|
23 |
|
24 | $index = 0;
|
25 | return this.each(function() {
|
26 | var $uniqueId = Materialize.guid(),
|
27 | $this = $(this),
|
28 | $original_offset = $(this).offset().top;
|
29 |
|
30 | function removePinClasses(object) {
|
31 | object.removeClass('pin-top');
|
32 | object.removeClass('pinned');
|
33 | object.removeClass('pin-bottom');
|
34 | }
|
35 |
|
36 | function updateElements(objects, scrolled) {
|
37 | objects.each(function () {
|
38 |
|
39 | if (options.top <= scrolled && options.bottom >= scrolled && !$(this).hasClass('pinned')) {
|
40 | removePinClasses($(this));
|
41 | $(this).css('top', options.offset);
|
42 | $(this).addClass('pinned');
|
43 | }
|
44 |
|
45 |
|
46 | if (scrolled < options.top && !$(this).hasClass('pin-top')) {
|
47 | removePinClasses($(this));
|
48 | $(this).css('top', 0);
|
49 | $(this).addClass('pin-top');
|
50 | }
|
51 |
|
52 |
|
53 | if (scrolled > options.bottom && !$(this).hasClass('pin-bottom')) {
|
54 | removePinClasses($(this));
|
55 | $(this).addClass('pin-bottom');
|
56 | $(this).css('top', options.bottom - $original_offset);
|
57 | }
|
58 | });
|
59 | }
|
60 |
|
61 | $(this).data('pushpin-id', $uniqueId);
|
62 | updateElements($this, $(window).scrollTop());
|
63 | $(window).on('scroll.' + $uniqueId, function () {
|
64 | var $scrolled = $(window).scrollTop() + options.offset;
|
65 | updateElements($this, $scrolled);
|
66 | });
|
67 |
|
68 | });
|
69 |
|
70 | };
|
71 | }( jQuery )); |
\ | No newline at end of file |