简体   繁体   English

如何让 mouseover/mouseout 与调整大小一起工作?

[英]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")
           });
        }

(Last part found at this question) (在这个问题上找到的最后一部分)

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.

相关问题 如何让div在悬停时上下移动? Mouseover和Mouseout冲突 - How can I get div to move up and down on hover? Mouseover and Mouseout conflict 如何使用javascript打开div? 鼠标悬停和鼠标悬停样式 - How can I open a div with javascript ? Mouseover & Mouseout style 如何将鼠标悬停,鼠标悬停和单击结合起来。 没有.mouseout破坏.click - How can i combine mouseover, mouseout and click. Without having .mouseout ruin .click 如何不被鼠标悬停和鼠标悬停(按时间)发送垃圾邮件? - How not to get spammed with mouseover and mouseout (by time)? 原型点击,鼠标悬停和鼠标移动不能一起工作? - Prototype click, mouseover and mouseout can't work together? 当我在特定区域鼠标悬停&amp;&amp; mouseout时如何禁用/取消setTimeOut? - How can i disable/cancel setTimeOut when i mouseover && mouseout in specific areas? Fancybox:在鼠标悬停/鼠标悬停时使用它 - Fancybox: Getting it to work on mouseover/mouseout jQuery Mouseover / Mouseout 不适用于 Live - jQuery Mouseover / Mouseout will not work with Live 仅当未触发另一个mouseover事件时,如何才能为jquery mouseout事件创建异常? - How can I create an exception for a jquery mouseout event only if another mouseover event hasn't been triggered? 我怎样才能更好地控制这个 jQuery UI 脚本,它会在 mouseover/mouseout 事件上为 div 添加一个叠加层? - How can I better control this jQuery UI script, which adds an overlay to a div on a mouseover/mouseout event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM