javascript - 移动端touch的基础问题
大家讲道理
大家讲道理 2017-04-11 10:17:56
[JavaScript讨论组]

代码很简单,chrome开发模式下,移动仿真界面,点击没有任何反应。代码没问题,在FF下开发模式移动仿真下的话,点击移动抬起都有效果。

chrome没有安任何插件。。。50版本的。为什么touch没有反应呢?






Document




    

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
天蓬老师

拿去玩~说爱我~

<!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>
        <p id="box"></p>
    </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>
PHP中文网

用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` 属性,

参考

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号