File: touch.js

package info (click to toggle)
angelscript 2.35.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,388 kB
  • sloc: cpp: 71,969; asm: 1,558; makefile: 665; xml: 214; javascript: 42; python: 22; ansic: 22; sh: 7
file content (52 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (2)
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
// Code provided by Alex Andreotti based on the information found here:
// http://chris-barr.com/2010/05/scrolling_a_overflowauto_element_on_a_touch_screen_device/

function isTouchDevice(){
	try{
		document.createEvent("TouchEvent");
		return true;
	}catch(e){
		return false;
	}
}

function touchScroll(el){
	if(isTouchDevice()){
		var scrollStartPos=0;
		var lastPos=0;
		var delta=0;
		var capture=false;

		el.addEventListener("touchstart", function(event) {
			scrollStartPos=this.scrollTop+event.touches[0].pageY;
			lastPos = event.touches[0].pageY;
			if (capture) {
				event.preventDefault();
				capture = false;
			}
		},false);

		el.addEventListener("touchmove", function(event) {
			var deltaY = scrollStartPos-event.touches[0].pageY;
			delta = event.touches[0].pageY - lastPos;
			//document.title = deltaY+':'+lastPos+':'+delta;
			this.velocity = delta;
			lastPos = event.touches[0].pageY;
			if (delta == 0) {
				capture = false;
			} else {
				capture = !(delta <= 0 && this.scrollTop+this.clientHeight==this.scrollHeight) && !(delta >= 0 && this.scrollTop == 0);
			}
			if (capture) {
				this.scrollTop = deltaY;
				event.preventDefault();
			}
		},false);
	}
}

// Add the touchScroll event handler to the nav-tree and main content divs
$(document).ready(function() {
	$('#doc-content, #nav-tree').each(function(){ touchScroll(this); });
});