javascript - 移动端touch的基础问题

查看:181
本文介绍了javascript - 移动端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>

解决方案

拿去玩~说爱我~

<!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>

这篇关于javascript - 移动端touch的基础问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆