简体   繁体   English

CSS:悬停、焦点或活动不适用于移动设备

[英]CSS: hover, focus or active not working on mobile devices

I have simple card when I hover on it card scale.当我将鼠标悬停在卡片比例上时,我有简单的卡片。 But on mobile devices is not working hover, focus or active.但在移动设备上无法悬停、聚焦或活动。

On mobile devices I need when someone click on this card, card make hover, focus or active.在移动设备上,当有人点击这张卡片时,我需要卡片悬停、聚焦或激活。

Is there any option how to do it?有没有办法做到这一点?

 .card { transition: transform .2s; background-repeat: no-repeat; max-width: 25%; min-width: 25%; height: 200px; margin: 50px; background: lightblue; } .hover-content { display: none; width: 80%; height: 50%; margin: 0 auto; } .content { background: red; } .card:hover, .card:focus, .card:active { position: relative; z-index: 2; opacity: 1; transform: scale(1.2); } .card:hover .hover-content, .card:focus .hover-content, .card:active .hover-content { margin-top: 20px; height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; }
 <div class="card"> <div class="content"> <p>iasJ ASJKAS AISJ</p> </div> <div class="hover-content"> <p>aksainsi qjsn asj iqjsi asijqs </p> </div> </div>

A div element cannot be focused without a tab index.没有选项卡索引,div 元素无法聚焦。 See what happens if you set the tab index of one of your divs to being a number.看看如果您将其中一个 div 的标签索引设置为数字会发生什么。

Hover, of course, makes no sense if the interface is a touch interface.当然,如果界面是触摸界面,Hover 就没有意义了。 Fingers cannot hover over most touch screens, so I believe most mobile browsers either treat it funny or ignore it.手指不能悬停在大多数触摸屏上,所以我相信大多数移动浏览器要么把它当作有趣的东西,要么忽略它。

If you are looking for a pure CSS solution then you may have trouble finding one.如果您正在寻找纯 CSS 解决方案,那么您可能很难找到。 Historically, css does not handle touchscreen or mobile hover states very well at all.从历史上看,css 根本不能很好地处理触摸屏或移动悬停状态。 There are a lot of inconsistencies from one device to another on just how they handle these states.从一种设备到另一种设备在处理这些状态的方式上存在很多不一致之处。

If you do not want to get javascript involved, then what I'd suggest is just making the layout of your page simpler on the mobile layout.如果你不想让JavaScript的参与,那么我会建议只是把你的页面上的移动布局简单的布局。 Meaning, use media queries to display your information in a more straightforward way.意思是,使用媒体查询以更直接的方式显示您的信息。

If you do want to get javascript involved then you could use the touchstart and touchend events.如果您确实想使用 javascript,那么您可以使用touchstarttouchend事件。 In that case you could attach those events to the html elements you want and just trigger the css states with your js.在这种情况下,您可以将这些事件附加到您想要的 html 元素,并使用您的 js 触发 css 状态。 This is definitely going to be a slower way to accomplish what you need but if you absolutely need the mobile view to have a hover state then that's a way to do it.这肯定是完成您需要的较慢的方法,但是如果您绝对需要移动视图具有悬停状态,那么这是一种方法。

Hope that helps.希望有帮助。

Hover doesnt work in mobile rather onclick.悬停在移动设备中不起作用,而在单击时不起作用。

you may need to look for some touch event css if you looking for only css as solution.如果您只寻找 css 作为解决方案,则可能需要寻找一些触摸事件 css。

else writing custom js function is the way否则编写自定义js函数是方法

For focus or active events, you can change temporarily the style with JQuery like this:对于焦点活动事件,您可以使用 JQuery 临时更改样式,如下所示:

$('#your-element').addClass("your-class"); 
setTimeout(function(){ $('#your-element').removeClass("your-class"); }, 300);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM