I want to make a web page on mobile devices (both Android and iOS) with fingers swipe effect, but when I add event.preventDefault()
into ontouchstart
/ ontouchmove
/ ontouchend
callback functions the scroll bar of the web page is being disabled, it is a disaster :(
I made an ugly hack for this with scrollTop
so that the page can be scrolled:
element.ontouchmove = function(event) {
event.preventDefault();
var oldScrollTop = document.body.scrollTop;
var dist = final_y - start_y // here start_y is pageY from touchstart and final_y is current pageY
document.body.scrollTop = oldScrollTop - dist > 0 ? oldScrollTop - dist : 0;
//...
}
It works now but I still want to know:
- It is there any other better solutions about this?
- Why we must use "preventDefault()" in the callback functions? what we have prevented by this?
Thanks.