This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
150 lines (139 loc) · 3.94 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function (global) {
'use strict';
var storage = global.localStorage;
if (storage == null) {
// if the localStorage is not exists
storage = {
// eslint-disable-next-line
getItem: function () { return null },
setItem: function () { },
removeItem: function () { },
clear: function () { },
};
}
// get the saved bookmark object
var getBookmark = function () {
var mark = storage.getItem('bookmark');
if (mark == null) {
return null;
}
try {
return JSON.parse(mark);
} catch (e) {
// invalid object saved in the storage
// console.warn('Invalid bookmark object.');
return null;
}
};
var link;
// register everything
var init = function () {
// bookmark-link style
var style = global.document.createElement('style');
style.type = 'text/css';
var text = '.book-mark-link{'
+ 'border-bottom:none;'
+ 'display:block;'
+ 'position:fixed;'
+ 'color:#222;'
+ 'font-size:26px !important;'
+ 'top:-10px;left:20px;'
+ 'transition:.3s;'
+ '}'
+ '.book-mark-link:hover,.book-mark-link-fixed{top:-2px}'
// do not show when the width is not enough
+ '@media(max-width:1090px){.book-mark-link{display:none}}';
text = global.document.createTextNode(text);
style.appendChild(text);
global.document.head.appendChild(style);
// create a link element
// eslint-disable-next-line max-len
link = $('<a class="book-mark-link book-mark-link-fixed fa fa-bookmark" href="#"></a>');
$(global.document.body).append(link);
var currentTop = 0;
// scroll event
$(global).on('scroll.bookmark', function () {
var top = global.document.documentElement.scrollTop;
if (top > 0) {
if (currentTop === 0) {
link.removeClass('book-mark-link-fixed');
currentTop = top;
}
} else {
if (currentTop > 0) {
!link.hasClass('book-mark-link-fixed') &&
link.addClass('book-mark-link-fixed');
currentTop = 0;
}
}
});
};
var loadBookmark = function () {
var mark = getBookmark();
if (mark == null) {
return;
}
// found the bookmark
$(function () {
init();
link.attr('href', mark.lastUri + '#book:mark');
});
};
var doScroll = function (top) {
if (!isNaN(top)) {
$(function () {
// eslint-disable-next-line max-len
$(global.document.documentElement).animate({ 'scrollTop': top }, 'fast');
});
}
};
var doSaveScroll = function (path, mark) {
if (mark == null) {
mark = {};
}
var top = global.document.documentElement.scrollTop;
mark.lastUri = path;
mark[path] = top;
storage.setItem('bookmark', JSON.stringify(mark));
link.animate({ top: -26 }, 'fast', function () {
setTimeout(function () {
link.css('top', '');
}, 400);
});
return mark;
};
var scrollToMark = function (trigger, hash) {
var path = global.location.pathname;
var mark = getBookmark();
$(function () {
init();
// save the position by clicking the icon
link.click(function () {
mark = doSaveScroll(path, mark);
return false;
});
// register beforeunload event when the trigger is auto
if (trigger === 'auto') {
// register beforeunload event
global.addEventListener('beforeunload', function () {
doSaveScroll(path, mark);
});
}
// auto scroll to the position
if (mark == null) {
return;
}
// and if the page opens with a specific hash, just jump out
var skips = [hash, '#comments'];
// eslint-disable-next-line
if (skips.filter(function (h) { return h === global.location.hash }).length > 0) {
return;
}
doScroll(mark[path]);
});
};
global.bookmark = {
loadBookmark: loadBookmark,
scrollToMark: scrollToMark,
};
})(window);