javascript - 移动端touch的基础问题

 纸鸢漫天飞舞 发布于 2022-11-14 00:28

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

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






Document




    

3 个回答
  • 移动端的 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` 属性,

    参考

    2022-11-14 02:13 回答
  • 用dom3级事件去绑定 touch事件不支持on这种

    2022-11-14 02:13 回答
  • 拿去玩~说爱我~

    <!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>
    
    2022-11-14 02:13 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有