[英]How can I get mouseover/mouseout to work with resizing?
I'm working on a dropdown menu, and it works both on mobile and desktop, but I'm running into an issue with the resizing.我正在处理下拉菜单,它在移动设备和桌面设备上都适用,但我遇到了调整大小的问题。 When I resize, down to mobile, the mouseover and mouseout is still working.当我调整大小时,缩小到移动设备时,mouseover 和 mouseout 仍然有效。
$(window).on("load resize", function(){
var width = $(window).width();
if ($(this).width() < 1023){
if($(".nav-more").length == 0){
$(".menu-item-has-children > a").append('<div class="nav-more">+</div>');
};
$(".nav-more").on("click", function(e){
e.preventDefault();
var cssDisplay = $(this).parent().parent().find('.sub-menu').css("display");
if (cssDisplay == 'none'){
$(this).parent().parent().find('.sub-menu').css("display", "contents");
}
else{
$(this).parent().parent().find('.sub-menu').css("display", "none")
}
});
} else if($(this).width() > 1023){
$(".menu li").mouseover(function(){
$(this).find('.sub-menu').css("display", "block");
$(".menu li").mouseout(function(){
$(this).find('.sub-menu').css("display", "none")
});
});
}
});
What did I miss?我错过了什么?
Mobile devices doesn't have a "mouse" so they never trigger "mouseover", i guess your issue goes only at development using something like chrome developer tools... if that's the case it's caused by a bug at Chrome DevTools.移动设备没有“鼠标”,所以它们永远不会触发“鼠标悬停”,我猜你的问题只出现在使用 chrome 开发人员工具之类的开发中......如果是这种情况,它是由 Chrome DevTools 中的错误引起的。
You may read more about the bug here .您可以在此处阅读有关该错误的更多信息。
As a work around you can add the following to validate if you are actually emulating a mobile device by detecting touch events:作为解决方法,您可以添加以下内容以验证您是否真的通过检测触摸事件来模拟移动设备:
if ('TouchEvent' in window && 'ontouchstart' in window) {
$(".menu li").mouseover(function(){
$(this).find('.sub-menu').css("display", "block");
$(".menu li").mouseout(function(){
$(this).find('.sub-menu').css("display", "none")
});
}
You need to remove your mouseover
/ mouseout
event handlers in the case where the screen size is small:在屏幕尺寸较小的情况下,您需要删除mouseover
/ mouseout
事件处理程序:
$(window).on("load resize", function(){
var width = $(window).width();
if ($(this).width() < 1023){
$(".menu li").off('mouseover');
$(".menu li").off('mouseout');
if($(".nav-more").length == 0){
$(".menu-item-has-children > a").append('<div class="nav-more">+</div>');
};
$(".nav-more").on("click", function(e){
e.preventDefault();
var cssDisplay = $(this).parent().parent().find('.sub-menu').css("display");
if (cssDisplay == 'none'){
$(this).parent().parent().find('.sub-menu').css("display", "contents");
}
else{
$(this).parent().parent().find('.sub-menu').css("display", "none")
}
});
} else if($(this).width() > 1023){
$(".menu li").mouseover(function(){
$(this).find('.sub-menu').css("display", "block");
$(".menu li").mouseout(function(){
$(this).find('.sub-menu').css("display", "none")
});
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.