-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrolltohighlight.js
53 lines (43 loc) · 1.45 KB
/
scrolltohighlight.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
/*
* jquery-scrollToHighlight extension to jquery.scrollTo by Ariel Flesler
*
* Created by Bastiaan ten Klooster
* http://www.bastiaan.io
*
* Under MIT License
*/
;(function ($, window, document) {
"use strict";
var defaults = {
highlightClass: 'scroll-to-highlight',
highlightStopEvent: 'transitionend',
onAfter: $.noop
};
function highlight(target, settings) {
$(target).addClass(settings.highlightClass);
$(target).one(settings.highlightStopEvent, function (event) {
$(event.currentTarget).removeClass(settings.highlightClass);
});
}
$.fn.scrollToHighlight = function (target, duration, options) {
if (typeof duration === 'object') {
options = duration;
duration = 0;
}
if (typeof options === 'function') {
options = { onAfter: options };
}
var element = this;
var settings = $.extend({}, defaults, options);
duration = duration || settings.duration;
delete settings.duration;
return element.each(function () {
$(element).scrollTo(target, duration, $.extend({}, settings, {
onAfter: function (target, scrollToSettings) {
settings.onAfter.call(this, target, scrollToSettings);
highlight(target, scrollToSettings);
}
}));
});
};
})(jQuery, window, document);