-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollbarpins.js
executable file
·114 lines (95 loc) · 3 KB
/
jquery.scrollbarpins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(function( $ )
{
// Function-level strict mode syntax, see https://developer.mozilla.org/en/JavaScript/Strict_mode
"use strict";
$.scrollbarpins = function( el, options )
{
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $( el );
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data( "scrollbarpins", base );
base.events =
{
pinsClick: function()
{
base.$el.find("[data-scrollbarpins]").each(function(i, el)
{
i = i + 1;
$(".pin-" + i).off("click.scrollbarpins");
$(".pin-" + i).on("click.scrollbarpins", function()
{
$("html,body").animate(
{
scrollTop: $(el).offset().top + "px"
}, 500);
});
});
}
},
base.load =
{
events: function()
{
base.events.pinsClick();
},
pins: function()
{
var $pinsContainer = $("<ol/>").attr("id", "scrollbarPinsContainer");
base.$el.find("[data-scrollbarpins]").each(function(i, el)
{
var $el = $(el),
$pin = $("<li/>").addClass("pin");
$pin
.addClass("pin-" + $el.data("scrollbarpins").id)
.css(
{
"top": base.pin.position(i + 1, el) + "px"
})
.html($el.data("scrollbarpins").title);
$pinsContainer.append($pin);
});
$("body").append($pinsContainer);
}
},
base.pin =
{
position: function(i, el)
{
var position = $(window).height() * ($(el).offset().top / $("body").height()) - i * 20;
return position;
}
},
/**
* Merge the default options with the customized options
*/
base.options = $.extend(
{},
$.scrollbarpins.defaultOptions,
options
);
/**
* Initialize the scrollbarpins (immediately invoked function)
*/
base.init = (function()
{
base.load.pins();
base.load.events();
})();
};
$.scrollbarpins.defaultOptions = {};
$.fn.scrollbarpins = function( options )
{
return this.each( function()
{
( new $.scrollbarpins( this, options ) );
});
};
})(jQuery);
$(document).ready(function()
{
$("#content").scrollbarpins();
});