-
Notifications
You must be signed in to change notification settings - Fork 0
/
customScoll.JS
150 lines (124 loc) · 4.73 KB
/
customScoll.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
// FOR NON COMMERCIAL USE ONLY!
//
//
// V1.1 - Creator: Liocrea / www.liocrea.com
//
//
// HOW TO USE THIS PLUGIN:
// 1. Create a div#content that wraps the whole document (start just after <body>, end juste before </body>)
// 2. Make sur the id is #content and not something else or it won't work
// 3. Insert the script/link to the script within <script type="text/javascript"> tag right before </body>
// 4. Insert the customScroll.css file inside a <link> tag within the <head> of the page. You can customize almost everything to create YOUR scrollbar
// 5. No need to import any jQuery library. It is a 100% pur Javascript script
//
// Optimized for Chrome ans IE browsers. Not working for Firefox browers right now. I'm working on that
// Working for all browsers' sizes
//
// Issue/Bug? Contact lio@liototo.com or go on Github: https://github.com/LioCrea
//
if(!(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))) { // disable plugin if mobile
var isFirefox = typeof InstallTrigger !== 'undefined';
if(isFirefox)
alert('This plugin doesn\'t work well under Firefox');
var allContent = document.getElementById('content');
var parent = allContent.parentNode;
var creaWrapper = document.createElement('div');
creaWrapper.setAttribute('id' , 'wrapper');
parent.replaceChild(creaWrapper, allContent);
creaWrapper.appendChild(allContent);
var setScrollColor, setScrollBackColor;
setScrollColor = allContent.getAttribute('scrollerColor');
setScrollBackColor = allContent.getAttribute('scrollBackColor');
var myBody = document.body;
myBody.style.msOverflowStyle='none';
myBody.style.overflow = 'auto';
myBody.style.paddingRight = '10px';
var div1 , div2;
div1 = document.createElement('div');
div1.setAttribute('class' , 'customScroll');
div1.style.position = 'fixed';
div1.style.zIndex = '10000!important';
div1.style.height = '100vh';
div1.style.top = '0';
div1.style.right = '0';
document.body.appendChild(div1);
var customScroll = document.getElementsByClassName('customScroll')[0];
div2 = document.createElement('div');
div2.setAttribute('class' , 'scroller');
div2.style.cursor = 'pointer';
customScroll.appendChild(div2);
var wrap = document.getElementById('wrapper');
wrap.style.overflow = 'hidden';
var customScrollHeight = customScroll.offsetHeight;
var scroller = document.getElementsByClassName('scroller')[0];
var scrollerHeight = scroller.offsetHeight;
var doc = document.getElementById('content');
var docHeight = doc.offsetHeight;
var onLoadFactor = docHeight/customScrollHeight;
var onLoadScrollerHeight = customScrollHeight/onLoadFactor+'px';
var rTScroll, rTScrollerPos, cursorPos, newHeight;
var dragging = false;
scroller.style.height = onLoadScrollerHeight;
function onScroll(){
docHeight = doc.offsetHeight;
onLoadFactor = docHeight/customScrollHeight;
rTScroll = myBody.scrollTop;
rTScrollerPos = rTScroll/onLoadFactor;
scroller.style.marginTop = rTScrollerPos+'px';
}
function onClick(evt){
dragging = true;
cursorPos = evt.clientY;
scroller.style.top = cursorPos;
newHeight = cursorPos;
myBody.scrollTop = cursorPos*onLoadFactor;
doc.scrollTop = cursorPos;
console.log('cursor pos: '+cursorPos);
console.log('ScrollerTopPos: '+scroller.offsetTop);
console.log('NewTop: '+newHeight);
}
function onMouse(ev){
myBody.style.webkitUserSelect = 'none';
myBody.style.webkitTouchCallout = 'none';
myBody.style.khtmlUserSelect = 'none';
myBody.style.mozUserSelect = 'none';
myBody.style.msUserSelect = 'none';
myBody.style.userSelect = 'none';
var $el = $('.scroller');
var dragging = false; // drag it or not
var startY = 0;
var startT = 0;
dragging = true;
startY = ev.clientY;
startT = scroller.style.marginTop+'px';
window.addEventListener("mousemove" , function(ev) {
if (dragging){
myBody.scrollTop = ev.clientY*onLoadFactor;
// calculate new top
var newSlide = parseInt(startT,10) + (ev.clientY - startY);
//stay in parent
var maxSlide = div1.offsetHeight-scroller.offsetHeight;
newSlide = newSlide<0?0:newSlide>maxSlide?maxSlide:newSlide;
scroller.style.marginTop = newSlide +'px';
}
}, false)
window.addEventListener("mouseup" , function() {
dragging = false;
myBody.style.webkitUserSelect = 'default';
myBody.style.webkitTouchCallout = 'default';
myBody.style.khtmlUserSelect = 'default';
myBody.style.mozUserSelect = 'default';
myBody.style.msUserSelect = 'default';
myBody.style.userSelect = 'default';
}, false);
}
window.addEventListener("scroll", onScroll,true);
customScroll.addEventListener("click", onClick,false);
scroller.addEventListener("mousedown", onMouse,true);
//
//
// DEMO SCRIPT
// DEMO SCRIPT
// DEMO SCRIPT
//
//