代码很简单,chrome开发模式下,移动仿真界面,点击没有任何反应。代码没问题,在FF下开发模式移动仿真下的话,点击移动抬起都有效果。
chrome没有安任何插件。。。50版本的。为什么touch没有反应呢?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;" />
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
}
</style>
<script>
window.onload = function(){
var oBox = document.getElementById("box");
oBox.ontouchstart = function(e){
oBox.style.backgroundColor = "pink";
}
oBox.ontouchmove = function(e){
oBox.style.backgroundColor = "purple";
}
oBox.ontouchend = function(e){
oBox.style.backgroundColor = "red";
}
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>
拿去玩~说爱我~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
<title>Document</title>
<style>
#box{
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<div id="box"></div>
</body>
<script>
window.onload = function(){
var oBox = document.getElementById("box");
function touchStart(e){
event.preventDefault();
oBox.style.backgroundColor = "pink";
}
function touchMove(e){
event.preventDefault();
oBox.style.backgroundColor = "purple";
}
function touchEnd(e){
event.preventDefault();
oBox.style.backgroundColor = "red";
}
oBox.addEventListener("touchstart", touchStart, false);
oBox.addEventListener("touchmove", touchMove, false);
oBox.addEventListener("touchend", touchEnd, false);
}
</script>
</html>
用dom3级事件去绑定 touch事件不支持on这种
移动端的 touch 背景可以使用 :active
sass 代码如下
.item {
-webkit-tap-highlight-color: rgba(0,0,0,0); // 隐藏系统自带的背景
// add `ontouchstart` attribte on body
// to allow :active work (if :active not work)
&:active {
background: #ECECEC
}
}
然后加上下面的js
document.body.addEventListener('touchstart', function() {}, false);
// 也可以直接在body上添加 `ontouchstart` 属性,
参考