[英]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.