简体   繁体   English

如何根据不同的设备使用鼠标悬停和单击?

[英]How can I use hover and click depending on different devices?

I have a menu button which is half visible and half outside the screen. 我有一个菜单按钮,该按钮一半可见,一半在屏幕外。 The full button is visible only when it is 'hovered' (I've written CSS for this, the button moves horizontal so it is displayed fully). 完整按钮只有在“悬停”时才可见(我为此编写了CSS ,该按钮水平移动,因此可以完整显示)。 Now this works on Desktop website. 现在,它可以在桌面网站上使用。 But as you know hover is not available on Mobile sites, so hover is converted to click. 但是如您所知,悬停在移动网站上不可用,因此悬停会转换为点击。 The click makes the button fully visible but it does not hide again when clicked again, because we have not defined click event to display and hide it, because it won't interfere with hover. 单击使按钮完全可见,但再次单击时它不会再次隐藏,因为我们尚未定义要显示和隐藏的click事件,因为它不会干扰悬停。

So is there any short and efficient way to do this, so that hover works on desktop and click works on phone for the same (display and hide) function? 那么,有什么短而有效的方法可以做到这一点,以便将鼠标悬停在桌面上工作,而单击在电话上工作以实现相同的(显示和隐藏)功能?

You can use CSS or jQuery or both. 您可以使用CSS或jQuery或同时使用两者。

its simple try this 简单尝试一下

1 - disable hover on mobile device using media query 1-使用媒体查询在移动设备上禁用悬停

@media screen and (max-width:767px){ @media屏幕和(最大宽度:767px){

button:hover{ just disable css ur using } } button:hover {只需使用}}禁用css ur}

2- add this jquery - this code adding and removing a class on the button click 2-添加此jQuery-此代码在按钮上单击添加和删除类

$('button').click(function(){ $(this).toggleClass('btnClick'); $('button')。click(function(){$(this).toggleClass('btnClick');

}); });

3: now write the same hover css for this class 3:现在为此类编写相同的悬停CSS

.btnClick{ .btnClick {

} }

For your case, I suggest you set a hover function within a break-point that suits desktops and a click function within a break-point that suits mobile device. 对于您的情况,建议您在适合台式机的断点内设置hover function ,并在适合移动设备的断点内设置click function See my example below and adjust as needed. 请参阅下面的示例,并根据需要进行调整。

 $(document).ready(function() { // When document is ready check window width, then choose hover/click if ($(window).width() > 768) { $("div").hover(function() { $(this).toggleClass("increase"); }); } else { $("div").click(function() { $(this).toggleClass("increase"); }); } }); 
 div { width: 50px; height: 15px; line-height: 45px; margin: 0 auto; background: red; color: white; border-radius: 5px; text-align: center; cursor: pointer; overflow: hidden; transition: all 1s; } div.increase { width: 150px; height: 45px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> I am the Button</div> 

Note: in my example; 注意:在我的示例中; hover toggles the height/width when device width is wider than 768px, while a click toggles the height/width when device width is less than 768px 当设备宽度小于768px时, hover可切换高度/宽度,而当设备宽度小于768px时, click可切换高度/宽度

暂无
暂无

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

相关问题 如何根据所显示的对象在单击时显示不同的文本? - How can I show different text on click depending on object shown? 如何将 hover 转换为 JavaScript 中的点击? - How can I convert a hover to a click in JavaScript? 如何在iPad等移动设备上处理悬停? - How can I handle hover on mobile devices like iPad? 如何根据热点设置不同的背景 hover - How to set different background depending on hotspot hover 如何根据拖动的 object 使用带有拖放功能的 if 语句打开不同的链接? - How can I use if-statements with drag-and-drop to open different links depending on the object that is dragged? 如何使&:hover可用? 我可以一起使用焦点和悬停吗? - How to make &:hover accessible? Can i use focus and hover together? 如何在我的.js.erb文件中使用单击逻辑来渲染不同的HTML,具体取决于所单击的按钮? - How do I use click logic in my .js.erb file to render different HTML depending on the button which was clicked? 是否可以根据浏览器的宽度使用不同的skrollr锚值? - Can I use different skrollr anchor values depending on the width of the browser? 如何在 jQuery hover 中使用 for 循环 - How can I use for loop in jQuery hover 如何使用鼠标悬停(或悬停)以便在 go 在不同的文本上显示不同的图像? JAVASCRIPT - How do I use mouseover (or hover) so that I can display different images when I go over different texts? JAVASCRIPT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM