【问题标题】:Mobile devices touch event with css移动设备与 css 的触摸事件
【发布时间】:2019-04-03 04:18:09
【问题描述】:

我用手机做了一个简单的卡片系统。

当我使用下面的代码时,触摸事件检测非常好。

html, body {
  margin: 0;
  padding: 0;
}
.card1 {
  height: 200px;
  width: 200px;
  background-color: green;
}
.card1:active {
  background-color: blue;
}
<html>
<body ontouchstart="">
  <div class="wrap">
    <div class="card1"></div>
   </div>
</body>
</html>

不仅在移动设备上,在桌面上也能正常工作。

但手指离开屏幕后,又恢复到原来的状态。

我的目标是如果我点击触摸框,框将更改为background-color: blue;,并且不必返回background-color: green;

这里有什么解决办法吗?

谢谢。

【问题讨论】:

  • 尝试 onclick 而不是 ontouchstart

标签: javascript css


【解决方案1】:

可以通过js在点击事件上应用背景色来实现

$('.card1').click(function() {
    $('.card1').css({
        'background-color': 'blue',        
    });
});
html, body {
  margin: 0;
  padding: 0;
}
.card1 {
  height: 200px;
  width: 200px;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body ontouchstart="">
  <div class="wrap">
    <div class="card1"></div>
   </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多