Repository navigation
Expand file tree
/
Copy pathscroll.js
More file actions
64 lines (52 loc) · 1.73 KB
/
Copy pathscroll.js
File metadata and controls
64 lines (52 loc) · 1.73 KB
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
// JavaScript Document
var speed = 0;
var scroll = 0;
var container = $('.slider');
var container_w = container.width();
var max_scroll = container[0].scrollWidth - container.outerWidth();
container.on('mousemove', function (e) {
var mouse_x = e.pageX - container.offset().left;
var mouseperc = 100 * mouse_x / container_w;
speed = mouseperc - 50;
}).on('mouseleave', function () {
speed = 0;
});
function updatescroll() {
if (speed !== 0) {
scroll += speed / 5;
if (scroll < 0) scroll = 0;
if (scroll > max_scroll) scroll = max_scroll;
$('.slider').scrollLeft(scroll);
}
$("#speed").html('Speed: ' + speed);
$("#scroll").html('Scroll: ' + scroll);
window.requestAnimationFrame(updatescroll);
}
window.requestAnimationFrame(updatescroll);
//wheel
var item = document.getElementsByTagName('body')[0];
window.addEventListener('wheel', function(e) {
if (e.deltaY > 0) item.scrollLeft += 100;
else item.scrollLeft -= 100;
});
//links
$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});