
CSS
html<style> .button { background-color: blue; color: white; padding: 10px; transition: background-color 0.3s; } .button:hover, .button.touch-active { background-color: red; }</style><button class="button" id="myButton">点击我</button><script> var button = document.getElementById("myButton"); button.addEventListener("touchstart", function(event) { button.classList.add("touch-active"); }); button.addEventListener("touchend", function(event) { button.classList.remove("touch-active"); });</script>在上述代码中,我们为按钮元素添加了一个触摸事件监听器。当用户触摸按钮时,我们通过添加一个名为"touch-active"的类来改变按钮的样式,从而实现了CSS悬停/主动触摸反馈的效果。通过使用触摸事件来代替鼠标事件,我们可以在移动设备上实现类似于鼠标悬停的效果,为用户提供更好的交互体验。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号