JavaScript監(jiān)聽觸摸事件代碼實例
這篇文章主要介紹了JavaScript監(jiān)聽觸摸事件代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
監(jiān)聽
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Wsscat滑動事件Demo</title>
</head>
<body>
<article>上下左右滑動</article>
</body>
<style>
* {
margin: 0;
padding: 0;
}
article {
background-color: #000000;
width: 100%;
height: 100px;
text-align: center;
line-height: 100px;
color: #FFFFFF;
}
</style>
<script>
(function() {
var touch = {};
function direction(startX, changeX, startY, changeY) {
return Math.abs(startX - changeX) >=
Math.abs(startY - changeY) ? (startX - changeX > 0 ? 'Left' : 'Right') : (startY - changeY > 0 ? 'Up' : 'Down')
}
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("點擊時的X坐標(biāo)" + nStartX + "和Y坐標(biāo)" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑動時的X坐標(biāo)" + nWhenChangX + "和Y坐標(biāo)" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑動后的X坐標(biāo)" + nChangX + "和Y坐標(biāo)" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
console.log(swDirection);
})
})()
</script>
</html>
觸摸
<!--touchstart
在觸摸開始時觸發(fā)事件
touchend
在觸摸結(jié)束時觸發(fā)事件
touchmove
在觸摸期間時觸發(fā)事件-->
<!DOCTYPE html>
<html lang="zh-cn" class="no-js">
<head>
<meta http-equiv="Content-Type">
<meta content="text/html; charset=utf-8">
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">
<link rel="stylesheet" type="text/css" href="css/reset.css" rel="external nofollow" />
</head>
<body>
<div class="page page-1-1 page-current">
<div class="wrap">
</div>
</div>
<div class="page page-2-1 hide">
<div class="wrap">
</div>
</div>
<div class="page page-2-2 hide">
<div class="wrap">
</div>
</div>
<div class="page page-3-1 hide">
<div class="wrap">
</div>
</div>
</body>
<script>
(function() {
var now = {
row: 1,
col: 1
},
last = {
row: 0,
col: 0
};
const towards = {
up: 1,
right: 2,
down: 3,
left: 4
};
var isAnimating = false;
var touch = {};
function direction(startX, changeX, startY, changeY) {
return Math.abs(startX - changeX) >=
Math.abs(startY - changeY) ? (startX - changeX > 0 ? 'Left' : 'Right') : (startY - changeY > 0 ? 'Up' : 'Down')
}
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("點擊時的X坐標(biāo)" + nStartX + "和Y坐標(biāo)" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑動時的X坐標(biāo)" + nWhenChangX + "和Y坐標(biāo)" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑動后的X坐標(biāo)" + nChangX + "和Y坐標(biāo)" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
console.log(swDirection);
//以回調(diào)的方法來寫這個動作
if(swDirection == 'Up') {
swipeUp(function() {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(now.col == 2) {
return;
} else if(last.row != 6) {
now.row = last.row + 1;
now.col = 1;
pageMove(towards.up);
}
})
}
if(swDirection == 'Down') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(now.col == 2) {
return;
} else if(last.row != 1) {
now.row = last.row - 1;
now.col = 1;
pageMove(towards.down);
}
}
if(swDirection == 'Left') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(last.row > 1 && last.row < 5 && last.col == 1) {
now.row = last.row;
now.col = 2;
pageMove(towards.left);
}
}
if(swDirection == 'Right') {
if(isAnimating) return;
last.row = now.row;
last.col = now.col;
if(last.row > 1 && last.row < 5 && last.col == 2) {
now.row = last.row;
now.col = 1;
pageMove(towards.right);
}
}
})
function swipeUp(callback) {
callback()
}
function hasClass(obj, cls) {
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
console.log(window.document)
function addClass(obj, cls) {
if(!hasClass(obj, cls)) obj.className += " " + cls;
}
function removeClass(obj, cls) {
if(hasClass(obj, cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
obj.className = obj.className.replace(reg, ' ');
}
}
function toggleClass(obj, cls) {
if(hasClass(obj, cls)) {
removeClass(obj, cls);
} else {
addClass(obj, cls);
}
}
function pageMove(tw) {
console.log(now);
console.log(now);
var lastPage = ".page-" + last.row + "-" + last.col,
nowPage = ".page-" + now.row + "-" + now.col;
switch(tw) {
case towards.up:
outClass = 'pt-page-moveToTop';
inClass = 'pt-page-moveFromBottom';
break;
case towards.right:
outClass = 'pt-page-moveToRight';
inClass = 'pt-page-moveFromLeft';
break;
case towards.down:
outClass = 'pt-page-moveToBottom';
inClass = 'pt-page-moveFromTop';
break;
case towards.left:
outClass = 'pt-page-moveToLeft';
inClass = 'pt-page-moveFromRight';
break;
}
isAnimating = true;
var $nowPage = document.querySelector(nowPage);
var $lastPage = document.querySelector(lastPage);
console.log($nowPage);
removeClass($nowPage, "hide");
addClass($lastPage, outClass)
addClass($nowPage, inClass);
setTimeout(function() {
removeClass($lastPage, 'page-current');
removeClass($lastPage, outClass);
addClass($lastPage, "hide");
addClass($nowPage, 'page-current');
removeClass($nowPage, inClass);
isAnimating = false;
}, 600);
}
})()
</script>
<style>
body {
width: 100%;
overflow: hidden;
}
.page {
width: 100%;
height: 100%;
position: absolute;
font-size: 100px;
text-align: center;
}
.page .wrap {
height: 500px;
}
.page-1-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-2-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-2-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-3-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-3-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-4-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-4-2 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-5-1 {
background-image: url(img/background/1.png);
background-size: cover;
}
.page-current {
z-index: 1;
}
.hide {
display: none;
}
.pt-page-moveToTop {
-webkit-animation: moveToTop .6s ease both;
animation: moveToTop .6s ease both;
}
@-webkit-keyframes moveToTop {
from {}
to {
-webkit-transform: translateY(-100%);
}
}
.pt-page-moveFromBottom {
-webkit-animation: moveFromBottom .6s ease both;
animation: moveFromBottom .6s ease both;
}
@-webkit-keyframes moveFromBottom {
from {
-webkit-transform: translateY(100%);
}
}
.pt-page-moveToBottom {
-webkit-animation: moveToBottom .6s ease both;
animation: moveToBottom .6s ease both;
}
@-webkit-keyframes moveToBottom {
from {}
to {
-webkit-transform: translateY(100%);
}
}
.pt-page-moveFromTop {
-webkit-animation: moveFromTop .6s ease both;
animation: moveFromTop .6s ease both;
}
@-webkit-keyframes moveFromTop {
from {
-webkit-transform: translateY(-100%);
}
}
.pt-page-moveToRight {
-webkit-animation: moveToRight .6s ease both;
animation: moveToRight .6s ease both;
}
@-webkit-keyframes moveToRight {
from {}
to {
-webkit-transform: translateX(100%);
}
}
.pt-page-moveToLeft {
-webkit-animation: moveToLeft .6s ease both;
animation: moveToLeft .6s ease both;
}
@-webkit-keyframes moveToLeft {
from {}
to {
-webkit-transform: translateX(-100%);
}
}
</style>
</html>
觸摸前后
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--利用touchstart和touchend觸摸前后監(jiān)聽到的四個坐標(biāo)分別是觸摸前的x,y坐標(biāo)和觸摸后的x,y坐標(biāo),
然后用數(shù)學(xué)公式進(jìn)行運算得出方向-->
</body>
<script type="text/javascript">
document.getElementsByTagName('body')[0].addEventListener('touchstart', function(e) {
touch.startY = e.targetTouches[0].pageY;
touch.startX = e.targetTouches[0].pageX;
//console.log("點擊時的X坐標(biāo)" + nStartX + "和Y坐標(biāo)" + nStartY);
});
document.getElementsByTagName('body')[0].addEventListener('touchmove', function(e) {
touch.whenChangY = e.changedTouches[0].pageY;
touch.whenChangX = e.changedTouches[0].pageX;
//console.log("滑動時的X坐標(biāo)" + nWhenChangX + "和Y坐標(biāo)" + nWhenChangY);
})
document.getElementsByTagName('body')[0].addEventListener('touchend', function(e) {
touch.changY = e.changedTouches[0].pageY;
touch.changX = e.changedTouches[0].pageX;
//console.log("滑動后的X坐標(biāo)" + nChangX + "和Y坐標(biāo)" + nChangY);
var swDirection = direction(touch.startX, touch.changX, touch.startY, touch.changY);
</script>
</html>
GitHub地址:https://github.com/lianglixiong
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Vue 實現(xiàn)顯示/隱藏層的思路(加全局點擊事件)
欄 目:JavaScript
下一篇:Vue通過配置WebSocket并實現(xiàn)群聊功能
本文標(biāo)題:JavaScript監(jiān)聽觸摸事件代碼實例
本文地址:http://www.jygsgssxh.com/a1/JavaScript/9347.html
您可能感興趣的文章
- 04-02javascript潛力,javascript強大嗎
- 04-02javascript點線,點線的代碼
- 04-02javascript移出,js 移入移出
- 04-02javascript替換字符串,js字符串的替換
- 04-02包含javascript舍的詞條
- 04-02javascript匿名,js匿名方法
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript警報,JavaScript警告
- 04-02javascript前身,javascript的前身
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面


閱讀排行
本欄相關(guān)
- 04-02javascript點線,點線的代碼
- 04-02javascript潛力,javascript強大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報,JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法


