老师,我不会限定他的边界,要怎么做?可以教教我吗?
老师,我不会限定他的边界,要怎么做?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximun-scale=1,minimum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>touch</title>
<style>
.box{
width: 50px;
height: 50px;
background-color: red;
left: 20px;
top: 20px;
}
body{
height: 2000px;
background-color: pink;
}
</style>
</head>
<body>
<div id="box" class="box"></div>
<script>
var boxEI = document.getElementById('box');
boxEI.addEventListener('touchstart',handleStart,false);
boxEI.addEventListener('touchmove',handleMove,false);
boxEI.addEventListener('touchend',handleEnd,false);
var currentPos = {
x:0,
y:0
},
distance = {},
currentPage = {},
targetPage = {};
function handleStart(ev){
var touch = ev.changedTouches[0];
// get start pos
currentPage.x = touch.pageX;
currentPage.y = touch.pageY;
}
function handleMove(ev){
var touch = ev.changedTouches[0];
// get end pos
targetPage.x = touch.pageX;
targetPage.y = touch.pageY;
// caculate distance
distance.x = targetPage.x - currentPage.x;
distance.y = targetPage.y - currentPage.y;
// move
move(this, distance.x+currentPos.x, distance.y+currentPos.y);
}
function handleEnd(ev){
currentPos.x += distance.x;
currentPos.y += distance.y;
}
function move(el, x, y){
el.style.transform = "translate3d("+ x + "px," + y + "px, 0)";
}
</script>
</body>
</html>6
收起
正在回答 回答被采纳积分+1
1回答
3.WebAPP开发与小程序
- 参与学习 人
- 提交作业 622 份
- 解答问题 6815 个
微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。
了解课程






恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星