我想使用普通的java脚本为我的响应项目实现滑动功能..我遵循了这个tutorial ... 我无法实现..事件是正确启动的,但无论是移动设备还是系统浏览器都不会有效。
我的代码:
<html>
<head>
<style>
#touchsurface2{
height:200px;
width:300px;
background-color:#ffce44;
border:1px solid blue;
}
</style>
<script>
function ontouch(el, callback){
var touchsurface = el,
dir,
swipeType,
startX,
startY,
distX,
distY,
threshold = 150, //required min distance traveled to be considered swipe
restraint = 100, // maximum distance allowed at the same time in perpendicular direction
allowedTime = 500, // maximum time allowed to travel that distance
elapsedTime,
startTime,
handletouch = callback || function(evt, dir, phase, swipetype, distance){}
touchsurface.addEventListener('touchstart', function(e){
var touchobj = e.changedTouches[0]
dir = 'none'
swipeType = 'none'
dist = 0
startX = touchobj.pageX
startY = touchobj.pageY
startTime = new Date().getTime() // record time when finger first makes contact with surface
handletouch(e, 'none', 'start', swipeType, 0) // fire callback function with params dir="none", phase="start", swipetype="none" etc
e.preventDefault()
}, false)
touchsurface.addEventListener('touchmove', function(e){
var touchobj = e.changedTouches[0]
distX = touchobj.pageX - startX // get horizontal dist traveled by finger while in contact with surface
distY = touchobj.pageY - startY // get vertical dist traveled by finger while in contact with surface
if (Math.abs(distX) > Math.abs(distY)){ // if distance traveled horizontally is greater than vertically, consider this a horizontal movement
dir = (distX < 0)? 'left' : 'right'
handletouch(e, dir, 'move', swipeType, distX) // fire callback function with params dir="left|right", phase="move", swipetype="none" etc
}
else{ // else consider this a vertical movement
dir = (distY < 0)? 'up' : 'down'
handletouch(e, dir, 'move', swipeType, distY) // fire callback function with params dir="up|down", phase="move", swipetype="none" etc
}
e.preventDefault() // prevent scrolling when inside DIV
}, false)
touchsurface.addEventListener('touchend', function(e){
var touchobj = e.changedTouches[0]
elapsedTime = new Date().getTime() - startTime // get time elapsed
if (elapsedTime <= allowedTime){ // first condition for awipe met
if (Math.abs(distX) >= threshold && Math.abs(distY) <= restraint){ // 2nd condition for horizontal swipe met
swipeType = dir // set swipeType to either "left" or "right"
}
else if (Math.abs(distY) >= threshold && Math.abs(distX) <= restraint){ // 2nd condition for vertical swipe met
swipeType = dir // set swipeType to either "top" or "down"
}
}
// Fire callback function with params dir="left|right|up|down", phase="end", swipetype=dir etc:
handletouch(e, dir, 'end', swipeType, (dir =='left' || dir =='right')? distX : distY)
e.preventDefault()
}, false)
}
</script>
<script>
window.addEventListener('load', function(){
var el = document.getElementById('touchsurface2')
ontouch(el, function(evt, dir, phase, swipetype, distance){
var touchreport = ''
touchreport += '<b>Dir:</b> ' + dir + '<br />'
touchreport += '<b>Phase:</b> ' + phase + '<br />'
touchreport += '<b>Swipe Type:</b> ' + swipetype + '<br />'
touchreport += '<b>Distance:</b> ' + distance + '<br />'
el.innerHTML = touchreport
})
}, false);
</script>
</head>
<body>
<center>
<div id="touchsurface2">
</div>
</center>
</body>
</html>
我想在网络浏览器中使用鼠标滑动事件并触摸移动浏览器
任何人都可以给出解决方案.. 感谢提前