简体   繁体   English

更改悬停交互以单击以触摸屏设备

[英]Change hover interaction to click for touch screen devices

I have content revealing on hovering over another element on the page. 我将鼠标悬停在页面上的另一个元素上时会感到满意。 On a touch screen this needs to be a click. 在触摸屏上,这需要单击一下。

I know that iOS and Android translate hover action on a link to a tabbing action, but I think I need another approach as my hover area spans more than one block element, not just a link. 我知道iOS和Android将链接上的悬停操作转换为Tab键操作,但是我想我需要另一种方法,因为我的悬停区域跨越多个块元素,而不仅仅是一个链接。

This is what I've got: 这就是我得到的:

<div>
<h3>Headline</h3>
<div>
<p><img src="http://placehold.it/300x200&text=image" /></p>
<p>I tillegg til sprellende fersk sjømat og sunne ferdigretter, kan vi tilby helnorske produkter fra spennede småprodusenter. <a href="pagename.html">Les mer &gt;></a></p>
</div></div>

and

div {width:300px; position:relative;}
p {padding-top:10px; margin:0;}
p+p {background:#fff;
display:block; height:100%; width:95%;
position:absolute; left:0; top:0;
opacity:0; transition:.3s ease-in-out opacity;
margin:0; padding:2% 5% 0 0;}

div:hover p+p {opacity:1;}

Here in action: http://jsfiddle.net/ju6bX/45/ 在这里起作用http : //jsfiddle.net/ju6bX/45/

I'm using Modernizr , so the 'no-touch' class gets added to the HTML tag. 我正在使用Modernizr ,因此将“ no-touch”类添加到HTML标记中。 Once the content has appeared after being tabbed, it should be hidden again if anything else on the page is tabbed (visitors don't need to be able to close it any other way). 一旦在选项卡上显示了内容,就应该再次隐藏该内容(如果页面上的其他选项卡也已被隐藏)(访问者不需要能够以其他任何方式关闭它)。

Javascript? JavaScript的?

I guess I would need some JS to add the click functionality if the device is touch enabled, but this is where I'm getting stuck, as my Javascript knowledge is poor to say the least. 我想如果设备启用了触摸功能,我将需要一些JS来添加单击功能,但这就是我遇到的问题,因为至少我的Java知识不足。

:focus? :焦点?

It's not quite clear to me whether :focus would work for my scenario on touch screens. 我尚不清楚:focus是否适用于我在触摸屏上的场景。 Would simply adding this pseudo class do the trick? 只需添加此伪类即可解决问题?

Many thanks for any help here. 非常感谢您的帮助。 (Btw, also using jQuery if this is relevant to any answers) (顺便说一句,如果这与任何答案相关,也使用jQuery)

Okay, you're going to want to define your event (you can do it based on the no-touch class) before you do anything. 好的,您将需要在执行任何操作之前定义您的事件(您可以基于非接触式课程来定义它)。 From there, I just opted to toggle a class, which will still allow for your CSS transitions. 从那里,我只是选择切换一个类,该类仍将允许您进行CSS转换。

$('body').hasClass('no-touch') ? event = 'mouseenter mouseleave' : event = 'click';
//!$('body').hasClass('no-touch') ? event = 'mouseenter mouseleave' : event = 'click';

$('div div').on(event, function() {
    $(this).find('p + p').toggleClass('open');
});

Fiddle 小提琴

You can see it both ways if you comment out the first line then uncomment the second. 如果您注释掉第一行然后取消注释第二行,则可以同时看到两种方式。

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

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