1.单点触摸拖拽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>touch拖动元素</title>
<style>
html,body{
margin: 0;
padding: 0;
height: 3000px;
}
.box{
width: 2rem;
height: 2rem;
background-color: #ccc;
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
window.onload = function () {
//rem布局
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let oBox = document.getElementsByClassName('box')[0];
oBox.addEventListener('touchstart',function (e) {
//存距离
let disX = e.targetTouches[0].pageX - oBox.offsetLeft;
let disY = e.targetTouches[0].pageY - oBox.offsetTop;
//默认阻止滚动事件和冒泡
e.preventDefault();
e.cancelBubble = true;
function fnMove(e) {
oBox.style.left = e.targetTouches[0].pageX - disX + 'px';
oBox.style.top = e.targetTouches[0].pageY - disY + 'px';
}
function fnEnd() {
oBox.removeEventListener('touchmove',fnMove,false);
oBox.removeEventListener('touchend',fnEnd,false);
}
oBox.addEventListener('touchmove',fnMove,false);
oBox.addEventListener('touchend',fnEnd,false);
},false)
}
2.多点触摸
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>touch多点触摸</title>
<style>
html,body{margin: 0;padding: 0;height: 3000px;}
.box{width: 2rem;height: 2rem;position: absolute;}
.box1{background-color: red;left: 0; top: 0;}
.box2{background-color: green;left: 210px; top: 50px;}
.box3{background-color: blue;left: 150px; top: 130px;}
</style>
<script>
window.onload = function () {
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let oBoxs = document.getElementsByClassName('box');
Array.from(oBoxs).forEach(oBoxs=>{
drag(oBoxs);
});
function drag(obj) {
obj.addEventListener('touchstart',e=>{
e.preventDefault();
e.cancelBubble = true;
let id = e.targetTouches[0].identifier;
let disX = e.targetTouches[0].pageX - obj.offsetLeft;
let disY = e.targetTouches[0].pageY - obj.offsetTop;
function fnMove(e) {
e.preventDefault();
e.cancelBubble = true;
obj.style.left = e.targetTouches[0].pageX - disX + 'px';
obj.style.top = e.targetTouches[0].pageY - disY + 'px';
}
function fnEnd(e) {
let found = false;
Array.from(e.changedTouches).forEach(touch=>{
if(touch.identifier == id){
found = true;
}
});
if(found){
obj.removeEventListener('touchmove',fnMove,false);
obj.removeEventListener('touchend',fnEnd,false);
}
}
obj.addEventListener('touchmove',fnMove,false);
obj.addEventListener('touchend',fnEnd,false);
},false);
}
}
</script>
</head>
<body>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
</body>
</html>
3.touch手势——旋转
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>touch手势-旋转</title>
<style>
html,body{margin: 0;padding: 0;height: 3000px;}
.box{
width: 4rem; height: 4rem;
background-color: yellowgreen; text-align: center;
line-height: 4rem; margin: 2rem auto 0;
font-size: 0.5rem; transform: rotate(45deg)}
</style>
<script>
window.onload = function () {
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let oBox = document.querySelector('.box');
let old_ang = 45;
let start_ang,ang;
oBox.addEventListener('touchstart',e=>{
if(e.targetTouches.length >= 2){
e.preventDefault();
e.cancelBubble = true;
start_ang = old_ang;
let disX = e.targetTouches[0].clientX - e.targetTouches[1].clientX;
let disY = e.targetTouches[0].clientY - e.targetTouches[1].clientY;
ang = Math.atan2(disY,disX)*180/Math.PI;
oBox.addEventListener('touchmove',fnMove,false);
oBox.addEventListener('touchend',fnEnd,false);
}
function fnMove(e) {
e.preventDefault();
e.cancelBubble = true;
if(e.targetTouches.length >= 2){
let disX = e.targetTouches[0].clientX - e.targetTouches[1].clientX;
let disY = e.targetTouches[0].clientY - e.targetTouches[1].clientY;
let ang2 = Math.atan2(disY,disX)*180/Math.PI;
old_ang = start_ang + ang2 - ang;
oBox.style.transform = `rotate(${old_ang}deg)`;
}
}
function fnEnd() {
oBox.removeEventListener('touchmove',fnMove,false);
oBox.removeEventListener('touchend',fnEnd,false);
}
},false);
}
</script>
</head>
<body>
<div class="box">
旋转旋转旋转
</div>
</body>
</html>
4.touch手势——缩放
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>touch手势-缩放</title>
<style>
html,body{margin: 0;padding: 0;height: 3000px;}
.box{
width: 4rem; height: 4rem;
background-color: yellowgreen; text-align: center;
line-height: 4rem; margin: 2rem auto 0;
font-size: 0.5rem;}
</style>
<script>
window.onload = function () {
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let oBox = document.querySelector('.box');
oBox.addEventListener('touchstart',e=>{
e.preventDefault();
e.cancelBubble = true;
let w = oBox.offsetWidth,h = oBox.offsetHeight;
let dis = Math.sqrt(Math.pow(e.targetTouches[0].clientX - e.targetTouches[1].clientX,2) + Math.pow(e.targetTouches[0].clientY - e.targetTouches[1].clientY,2));
if(e.targetTouches.length >= 2){
oBox.addEventListener('touchmove',fnMove,false);
oBox.addEventListener('touchend',fnEnd,false);
}
function fnMove(e) {
e.preventDefault();
e.cancelBubble = true;
if(e.targetTouches.length >= 2){
let dis2 = Math.sqrt(Math.pow(e.targetTouches[0].clientX - e.targetTouches[1].clientX,2) + Math.pow(e.targetTouches[0].clientY - e.targetTouches[1].clientY,2));
oBox.style.width = dis2*w/dis + 'px';
oBox.style.lineHeight= oBox.style.height = dis2*h/dis + 'px';
}
}
function fnEnd() {
oBox.removeEventListener('touchmove',fnMove,false);
oBox.removeEventListener('touchend',fnEnd,false);
}
},false);
}
</script>
</head>
<body>
<div class="box">
缩放
</div>
</body>
</html>
5.方向锁定&banner滚动&下拉刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<title>方向锁定&banner滚动&下拉刷新</title>
<style>
*{
margin: 0;
padding: 0;
list-style: none;
}
body{
overflow: hidden;
}
#wrap{
background: #ccc;
}
.load_more{
position: relative;
font-size: 15px;
line-height: 30px;
text-align: center;
z-index: 1;
}
#content{
position: relative;
z-index: 2;
top: -30px;
background: #fff;
transform: translateY(0);
}
#banner{
width: 7.5rem;
height: 3rem;
overflow: hidden;
}
#banner ul{
width: 37.5rem;
height: 3rem;
transform: translateX(0);
}
#banner ul li{
float: left;
width: 7.5rem;
height: 3rem;
line-height: 3rem;
text-align: center;
}
</style>
</head>
<body>
<div id="wrap">
<div class="load_more">
下拉刷新
</div>
<div id="content">
<div id="banner">
<ul>
<li style="background: #0FC">0</li>
<li style="background: #FC0">1</li>
<li style="background: #F0C">2</li>
<li style="background: #0CF">3</li>
<li style="background: #CF0">4</li>
</ul>
</div>
<ol></ol>
</div>
</div>
<script>
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let banner = document.getElementById('banner');
let content = document.getElementById('content');
let load_more = document.getElementsByClassName('load_more')[0];
let banner_ul = banner.children[0];
let banner_left = 0;
let content_top = 0;
function loadData(fn) {
$.ajax({
url: 'data.txt?t=' + Math.random(),
success(str){
let arr = str.split('\n');
$('ol').html('');
arr.forEach(s=>{
$(`<li>${s}</li>`).appendTo('ol');
});
fn&&fn();
},
error(){
alert('加载失败');
}
})
}
loadData();
banner.addEventListener('touchstart',function (e) {
let startX = e.targetTouches[0].clientX; //手指初始位置X
let startY = e.targetTouches[0].clientY; //手指初始位置Y
let dir = ''; //方向
let disX = startX - banner_left;
let disY = startY - content_top;
banner_ul.style.transition = '';
content.style.transition = '';
function fnMove(e) {
let x = e.targetTouches[0].clientX;
let y = e.targetTouches[0].clientY;
//阈值5
if(!dir){
if(Math.abs(x- startX) >= 5){
dir = 'x';
}else if(Math.abs(y - startY) >= 5){
dir = 'y';
}
}else{
if(dir === 'x'){ //处理banner滚动
banner_left = x - disX;
banner_ul.style.transform = `translateX(${banner_left}px)`;
}else if(dir === 'y'){ //处理页面滚动
content_top = y - disY;
if(content_top > 0){
content.style.transform = `translateY(${content_top/3}px)`;
if(content_top >= 100){
load_more.innerHTML = '松开加载';
}else{
load_more.innerHTML = '下拉刷新';
}
}else{
content.style.transform = `translateY(${content_top}px)`;
}
}
}
}
function fnEnd() {
banner.removeEventListener('touchmove',fnMove,false);
banner.removeEventListener('touchend',fnEnd,false);
if(dir === 'x'){ //处理banner滚动
let n = Math.round(-banner_left/banner.offsetWidth);
if(n < 0){
n = 0;
}else if(n >= banner_ul.children.length){
n = banner_ul.children.length - 1;
}
banner_left = -n * banner.offsetWidth;
banner_ul.style.transition = '0.7s all ease';
banner_ul.style.transform = `translateX(${banner_left}px)`;
}else if(dir === 'y'){ //处理页面滚动
if(content_top >= 100){
content_top = 30;
load_more.innerHTML = '正在加载...';
loadData(function () {
load_more.innerHTML = '下拉刷新';
content_top = 0;
content.style.transition = '0.7s all ease';
content.style.transform = `translateY(${content_top}px)`;
})
}else{
content_top = 0;
}
content.style.transition = '0.7s all ease';
content.style.transform = `translateY(${content_top}px)`;
}
}
banner.addEventListener('touchmove',fnMove,false);
banner.addEventListener('touchend',fnEnd,false);
},false);
</script>
</body>
</html>
6.双击+长按
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<title>双击+长按</title>
<style>
#btn{
width: 2rem;
}
</style>
</head>
<body>
<input type="button" value="按钮" id="btn">
<script>
document.documentElement.style.fontSize = document.documentElement.clientWidth*100/750 + 'px';
let btn = document.getElementById('btn');
let t = 0;
let lastTap = 0;
let timer = null;
btn.addEventListener('touchstart',function () {
t = Date.now();
timer = setTimeout(function () {
console.log('长按了');
},1500);
},false);
btn.addEventListener('touchend',function () {
clearTimeout(timer);
if(Date.now() - t <= 700){
console.log('tap');
if(Date.now() - lastTap <= 300){
console.log('double tap');
}
lastTap = Date.now();
}else{
console.log('long tap');
}
},false);
</script>
</body>
</html>