开发者社区> 问答> 正文

移动端touch的基础问题

代码很简单,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>

展开
收起
杨冬芳 2016-06-13 10:18:21 2974 0
1 条回答
写回答
取消 提交回答
  • IT从业
    <!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>
    2019-07-17 19:35:11
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《 优酷APP全量支持“暗黑模式” 设计与技术完整总结》 立即下载
基于浏览器的实时构建探索之路--玄寂 立即下载
移动 App 性能监测实践(iOS篇) 立即下载