Browse code

Implement a new fullpage.js based scrolling and slide handling and remove the old one

Benjamin Roth authored on27/03/2015 12:46:35
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,497 @@
1
+/*! Copyright (c) 2011 Piotr Rochala (http://rocha.la)
2
+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
3
+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
4
+ *
5
+ * Version: 1.3.2 (modified for fullpage.js)
6
+ *
7
+ */
8
+(function($) {
9
+
10
+  jQuery.fn.extend({
11
+    slimScroll: function(options) {
12
+
13
+      var defaults = {
14
+
15
+        // width in pixels of the visible scroll area
16
+        width : 'auto',
17
+
18
+        // height in pixels of the visible scroll area
19
+        height : '250px',
20
+
21
+        // width in pixels of the scrollbar and rail
22
+        size : '7px',
23
+
24
+        // scrollbar color, accepts any hex/color value
25
+        color: '#000',
26
+
27
+        // scrollbar position - left/right
28
+        position : 'right',
29
+
30
+        // distance in pixels between the side edge and the scrollbar
31
+        distance : '1px',
32
+
33
+        // default scroll position on load - top / bottom / $('selector')
34
+        start : 'top',
35
+
36
+        // sets scrollbar opacity
37
+        opacity : .4,
38
+
39
+        // enables always-on mode for the scrollbar
40
+        alwaysVisible : false,
41
+
42
+        // check if we should hide the scrollbar when user is hovering over
43
+        disableFadeOut : false,
44
+
45
+        // sets visibility of the rail
46
+        railVisible : false,
47
+
48
+        // sets rail color
49
+        railColor : '#333',
50
+
51
+        // sets rail opacity
52
+        railOpacity : .2,
53
+
54
+        // whether  we should use jQuery UI Draggable to enable bar dragging
55
+        railDraggable : true,
56
+
57
+        // defautlt CSS class of the slimscroll rail
58
+        railClass : 'slimScrollRail',
59
+
60
+        // defautlt CSS class of the slimscroll bar
61
+        barClass : 'slimScrollBar',
62
+
63
+        // defautlt CSS class of the slimscroll wrapper
64
+        wrapperClass : 'slimScrollDiv',
65
+
66
+        // check if mousewheel should scroll the window if we reach top/bottom
67
+        allowPageScroll : false,
68
+
69
+        // scroll amount applied to each mouse wheel step
70
+        wheelStep : 20,
71
+
72
+        // scroll amount applied when user is using gestures
73
+        touchScrollStep : 200,
74
+
75
+        // sets border radius
76
+        borderRadius: '7px',
77
+
78
+        // sets border radius of the rail
79
+        railBorderRadius : '7px'
80
+      };
81
+
82
+      var o = $.extend(defaults, options);
83
+
84
+      // do it for every element that matches selector
85
+      this.each(function(){
86
+
87
+      var isOverPanel, isOverBar, isDragg, queueHide, touchDif,
88
+        barHeight, percentScroll, lastScroll,
89
+        divS = '<div></div>',
90
+        minBarHeight = 30,
91
+        releaseScroll = false;
92
+
93
+        // used in event handlers and for better minification
94
+        var me = $(this);
95
+
96
+        // ensure we are not binding it again
97
+        if (me.parent().hasClass(o.wrapperClass))
98
+        {
99
+            // start from last bar position
100
+            var offset = me.scrollTop();
101
+
102
+            // find bar and rail
103
+            bar = me.parent().find('.' + o.barClass);
104
+            rail = me.parent().find('.' + o.railClass);
105
+
106
+            getBarHeight();
107
+
108
+            // check if we should scroll existing instance
109
+            if ($.isPlainObject(options))
110
+            {
111
+              // Pass height: auto to an existing slimscroll object to force a resize after contents have changed
112
+              if ( 'height' in options && options.height == 'auto' ) {
113
+                me.parent().css('height', 'auto');
114
+                me.css('height', 'auto');
115
+                var height = me.parent().parent().height();
116
+                me.parent().css('height', height);
117
+                me.css('height', height);
118
+              }
119
+
120
+              if ('scrollTo' in options)
121
+              {
122
+                // jump to a static point
123
+                offset = parseInt(o.scrollTo);
124
+              }
125
+              else if ('scrollBy' in options)
126
+              {
127
+                // jump by value pixels
128
+                offset += parseInt(o.scrollBy);
129
+              }
130
+              else if ('destroy' in options)
131
+              {
132
+                // clear slimscroll mouse event listeners
133
+                detachWheel();
134
+
135
+                // remove slimscroll elements
136
+                bar.remove();
137
+                rail.remove();
138
+                me.unwrap();
139
+                return;
140
+              }
141
+
142
+              // scroll content by the given offset
143
+              scrollContent(offset, false, true);
144
+            }
145
+
146
+            return;
147
+        }
148
+
149
+        // optionally set height to the parent's height
150
+        o.height = (options.height == 'auto') ? me.parent().height() : options.height;
151
+
152
+        // wrap content
153
+        var wrapper = $(divS)
154
+          .addClass(o.wrapperClass)
155
+          .css({
156
+            position: 'relative',
157
+            overflow: 'hidden',
158
+            width: o.width,
159
+            height: o.height
160
+          });
161
+
162
+        // update style for the div
163
+        me.css({
164
+          overflow: 'hidden',
165
+          width: o.width,
166
+          height: o.height
167
+        });
168
+
169
+        // create scrollbar rail
170
+        var rail = $(divS)
171
+          .addClass(o.railClass)
172
+          .css({
173
+            width: o.size,
174
+            height: '100%',
175
+            position: 'absolute',
176
+            top: 0,
177
+            display: (o.alwaysVisible && o.railVisible) ? 'block' : 'none',
178
+            'border-radius': o.railBorderRadius,
179
+            background: o.railColor,
180
+            opacity: o.railOpacity,
181
+            zIndex: 90
182
+          });
183
+
184
+        // create scrollbar
185
+        var bar = $(divS)
186
+          .addClass(o.barClass)
187
+          .css({
188
+            background: o.color,
189
+            width: o.size,
190
+            position: 'absolute',
191
+            top: 0,
192
+            opacity: o.opacity,
193
+            display: o.alwaysVisible ? 'block' : 'none',
194
+            'border-radius' : o.borderRadius,
195
+            BorderRadius: o.borderRadius,
196
+            MozBorderRadius: o.borderRadius,
197
+            WebkitBorderRadius: o.borderRadius,
198
+            zIndex: 99
199
+          });
200
+
201
+        // set position
202
+        var posCss = (o.position == 'right') ? { right: o.distance } : { left: o.distance };
203
+        rail.css(posCss);
204
+        bar.css(posCss);
205
+
206
+        // wrap it
207
+        me.wrap(wrapper);
208
+
209
+        // append to parent div
210
+        me.parent().append(bar);
211
+        me.parent().append(rail);
212
+
213
+        // make it draggable and no longer dependent on the jqueryUI
214
+        if (o.railDraggable){
215
+          bar.bind("mousedown", function(e) {
216
+            var $doc = $(document);
217
+            isDragg = true;
218
+            t = parseFloat(bar.css('top'));
219
+            pageY = e.pageY;
220
+
221
+            $doc.bind("mousemove.slimscroll", function(e){
222
+              currTop = t + e.pageY - pageY;
223
+              bar.css('top', currTop);
224
+              scrollContent(0, bar.position().top, false);// scroll content
225
+            });
226
+
227
+            $doc.bind("mouseup.slimscroll", function(e) {
228
+              isDragg = false;hideBar();
229
+              $doc.unbind('.slimscroll');
230
+            });
231
+            return false;
232
+          }).bind("selectstart.slimscroll", function(e){
233
+            e.stopPropagation();
234
+            e.preventDefault();
235
+            return false;
236
+          });
237
+        }
238
+
239
+        // on rail over
240
+        rail.hover(function(){
241
+          showBar();
242
+        }, function(){
243
+          hideBar();
244
+        });
245
+
246
+        // on bar over
247
+        bar.hover(function(){
248
+          isOverBar = true;
249
+        }, function(){
250
+          isOverBar = false;
251
+        });
252
+
253
+        // allow scrolling on page load
254
+        // based on hack in http://stackoverflow.com/a/6593995/1547641
255
+        // Chrome seems to return an rgba() value while other browsers return
256
+        // the "transparent" value.
257
+        if (
258
+
259
+           me.css('background-color') == 'transparent' ||
260
+           me.css('background-color') == 'rgba(0, 0, 0, 0)'
261
+        ) {
262
+           isOverPanel = true;
263
+           showBar();
264
+           hideBar();
265
+        }
266
+        else {
267
+          isOverPanel = false;
268
+          hideBar();
269
+        }
270
+
271
+        // show on parent mouseover
272
+        me.hover(function(){
273
+          isOverPanel = true;
274
+          showBar();
275
+          hideBar();
276
+        }, function(){
277
+          isOverPanel = false;
278
+          hideBar();
279
+        });
280
+
281
+        // support for mobile
282
+        me.bind('touchstart', function(e,b){
283
+          if (e.originalEvent.touches.length)
284
+          {
285
+            // record where touch started
286
+            touchDif = e.originalEvent.touches[0].pageY;
287
+          }
288
+        });
289
+
290
+        me.bind('touchmove', function(e){
291
+          // prevent scrolling the page if necessary
292
+          if(!releaseScroll)
293
+          {
294
+  		      e.originalEvent.preventDefault();
295
+		      }
296
+          if (e.originalEvent.touches.length)
297
+          {
298
+            // see how far user swiped
299
+            var diff = (touchDif - e.originalEvent.touches[0].pageY) / o.touchScrollStep;
300
+            // scroll content
301
+            scrollContent(diff, true);
302
+            touchDif = e.originalEvent.touches[0].pageY;
303
+          }
304
+        });
305
+
306
+        // set up initial height
307
+        getBarHeight();
308
+
309
+        // check start position
310
+        if (o.start === 'bottom')
311
+        {
312
+          // scroll content to bottom
313
+          bar.css({ top: me.outerHeight() - bar.outerHeight() });
314
+          scrollContent(0, true);
315
+        }
316
+        else if (o.start !== 'top')
317
+        {
318
+          // assume jQuery selector
319
+          scrollContent($(o.start).position().top, null, true);
320
+
321
+          // make sure bar stays hidden
322
+          if (!o.alwaysVisible) { bar.hide(); }
323
+        }
324
+
325
+        // attach scroll events
326
+        attachWheel();
327
+
328
+        function _onWheel(e)
329
+        {
330
+          // use mouse wheel only when mouse is over
331
+          if (!isOverPanel) { return; }
332
+
333
+          var e = e || window.event;
334
+
335
+          var delta = 0;
336
+          if (e.wheelDelta) { delta = -e.wheelDelta/120; }
337
+          if (e.detail) { delta = e.detail / 3; }
338
+
339
+          var target = e.target || e.srcTarget || e.srcElement;
340
+          if ($(target).closest('.' + o.wrapperClass).is(me.parent())) {
341
+            // scroll content
342
+            scrollContent(delta, true);
343
+          }
344
+
345
+          // stop window scroll
346
+          if (e.preventDefault && !releaseScroll) { e.preventDefault(); }
347
+          if (!releaseScroll) { e.returnValue = false; }
348
+        }
349
+
350
+        function scrollContent(y, isWheel, isJump)
351
+        {
352
+          releaseScroll = false;
353
+          var delta = y;
354
+          var maxTop = me.outerHeight() - bar.outerHeight();
355
+
356
+          if (isWheel)
357
+          {
358
+            // move bar with mouse wheel
359
+            delta = parseInt(bar.css('top')) + y * parseInt(o.wheelStep) / 100 * bar.outerHeight();
360
+
361
+            // move bar, make sure it doesn't go out
362
+            delta = Math.min(Math.max(delta, 0), maxTop);
363
+
364
+            // if scrolling down, make sure a fractional change to the
365
+            // scroll position isn't rounded away when the scrollbar's CSS is set
366
+            // this flooring of delta would happened automatically when
367
+            // bar.css is set below, but we floor here for clarity
368
+            delta = (y > 0) ? Math.ceil(delta) : Math.floor(delta);
369
+
370
+            // scroll the scrollbar
371
+            bar.css({ top: delta + 'px' });
372
+          }
373
+
374
+          // calculate actual scroll amount
375
+          percentScroll = parseInt(bar.css('top')) / (me.outerHeight() - bar.outerHeight());
376
+          delta = percentScroll * (me[0].scrollHeight - me.outerHeight());
377
+
378
+          if (isJump)
379
+          {
380
+            delta = y;
381
+            var offsetTop = delta / me[0].scrollHeight * me.outerHeight();
382
+            offsetTop = Math.min(Math.max(offsetTop, 0), maxTop);
383
+            bar.css({ top: offsetTop + 'px' });
384
+          }
385
+
386
+          // scroll content
387
+          me.scrollTop(delta);
388
+
389
+          // fire scrolling event
390
+          me.trigger('slimscrolling', ~~delta);
391
+
392
+          // ensure bar is visible
393
+          showBar();
394
+
395
+          // trigger hide when scroll is stopped
396
+          hideBar();
397
+        }
398
+
399
+        function attachWheel()
400
+        {
401
+          if (window.addEventListener)
402
+          {
403
+            this.addEventListener('DOMMouseScroll', _onWheel, false );
404
+            this.addEventListener('mousewheel', _onWheel, false );
405
+          }
406
+          else
407
+          {
408
+            document.attachEvent("onmousewheel", _onWheel)
409
+          }
410
+        }
411
+
412
+        function detachWheel()
413
+        {
414
+          if (window.removeEventListener)
415
+          {
416
+            this.removeEventListener('DOMMouseScroll', _onWheel);
417
+            this.removeEventListener('mousewheel', _onWheel);
418
+          }
419
+          else
420
+          {
421
+            document.detachEvent('onmousewheel', _onWheel);
422
+          }
423
+        }
424
+
425
+        function getBarHeight()
426
+        {
427
+          // calculate scrollbar height and make sure it is not too small
428
+          barHeight = Math.max((me.outerHeight() / me[0].scrollHeight) * me.outerHeight(), minBarHeight);
429
+          bar.css({ height: barHeight + 'px' });
430
+
431
+          // hide scrollbar if content is not long enough
432
+          var display = barHeight == me.outerHeight() ? 'none' : 'block';
433
+          bar.css({ display: display });
434
+        }
435
+
436
+        function showBar()
437
+        {
438
+          // recalculate bar height
439
+          getBarHeight();
440
+          clearTimeout(queueHide);
441
+
442
+          // when bar reached top or bottom
443
+          if (percentScroll == ~~percentScroll)
444
+          {
445
+            //release wheel
446
+            releaseScroll = o.allowPageScroll;
447
+
448
+            // publish approporiate event
449
+            if (lastScroll != percentScroll)
450
+            {
451
+                var msg = (~~percentScroll == 0) ? 'top' : 'bottom';
452
+                me.trigger('slimscroll', msg);
453
+            }
454
+          }
455
+          else
456
+          {
457
+            releaseScroll = false;
458
+          }
459
+          lastScroll = percentScroll;
460
+
461
+          // show only when required
462
+          if(barHeight >= me.outerHeight()) {
463
+            //allow window scroll
464
+            releaseScroll = true;
465
+            return;
466
+          }
467
+          bar.stop(true,true).fadeIn('fast');
468
+          if (o.railVisible) { rail.stop(true,true).fadeIn('fast'); }
469
+        }
470
+
471
+        function hideBar()
472
+        {
473
+          // only hide when options allow it
474
+          if (!o.alwaysVisible)
475
+          {
476
+            queueHide = setTimeout(function(){
477
+              if (!(o.disableFadeOut && isOverPanel) && !isOverBar && !isDragg)
478
+              {
479
+                bar.fadeOut('slow');
480
+                rail.fadeOut('slow');
481
+              }
482
+            }, 1000);
483
+          }
484
+        }
485
+
486
+      });
487
+
488
+      // maintain chainability
489
+      return this;
490
+    }
491
+  });
492
+
493
+  jQuery.fn.extend({
494
+    slimscroll: jQuery.fn.slimScroll
495
+  });
496
+
497
+})(jQuery);
0 498
\ No newline at end of file