简体   繁体   English

jQuery-调整大小后,点击事件不起作用

[英]Jquery - Click event doesn't work after resized

I'm doing a responsive menu that check the window.resize event and, when it fits the minimum browser width, a click function for a button is allowed. 我正在做一个响应菜单,检查window.resize事件,当它适合浏览器的最小宽度时,允许单击按钮的功能。 If the browser width is greater, then the click function is unbound. 如果浏览器宽度更大,则单击功能将不受限制。 I need to do this because the same element that is the button on the mobile version, is a visual element on the desktop version. 我需要这样做,因为与移动版本上的按钮相同的元素是台式机版本上的可视元素。

The problem is that, with the code that I have now, when the page is loaded (desktop or mobile screen), the click function works fine. 问题是,使用我现在拥有的代码,在加载页面(桌面或移动屏幕)时,单击功能可以正常工作。 But, if I resize the browser and click on the element again, it doesn't work. 但是,如果我调整浏览器的大小并再次单击该元素,则它将不起作用。 If I want to mobile nav works, I need to refresh the page to make it work. 如果要移动导航正常工作,则需要刷新页面以使其正常工作。 Really annoying. 真烦人。

To help understand what is happening, I've made a simple example. 为了帮助理解正在发生的事情,我举了一个简单的例子。 Here's is the simple code (just to check the click function issue) 这是简单的代码(仅用于检查点击功能问题)

HTML HTML

<!-- WRAPPER -->
<div id="freakers-wrapper">
    <!-- HEADER -->
    <header id="header">
        <div class="header-bottom">
            <div class="container">
                <div class="row">
                    <div class="col-md-5">
                        <!-- MENU -->
                        <nav class="menu-wrapper-left">
                            <a class="menu-trigger" href="#">
                                <span></span>
                            </a>

                            <ul id="main-menu" class="menu menu--main menu--left main-menu">
                                <li><a href="#">Home</a></li>

                                <li class="has-children">
                                    <a href="#">Shop</a>

                                    <ul class="sub-menu is-hidden">
                                        <li class="go-back"><a href="#">Back</a></li>
                                        <li><a href="#">Shop 1</a></li>
                                        <li><a href="#">Shop 2</a></li>
                                        <li><a href="#">Shop 3</a></li>
                                    </ul>
                                </li>
                                <li><a href="#" >Blog</a></li>
                            </ul>   <!-- end main-menu -->
                        </nav>  <!-- end menu-wrapper -->
                    </div>

                    <div class="col-md-2">
                        <div class="logo">
                            <a href="#">
                                <img src="images/logo.png" alt="Logo" class="img-responsive">
                            </a>
                        </div>
                    </div>

                    <div class="col-md-5">
                        <!-- MENU -->
                        <nav class="menu-wrapper-right">
                            <ul id="main-menu" class="menu menu--main menu--right main-menu">
                                <li><a href="#">help</a></li>
                                <li><a href="#">lookbook</a></li>
                                <li><a href="#">model</a></li>
                            </ul>   <!-- end main-menu -->
                        </nav>  <!-- end menu-wrapper -->
                    </div>
                </div>
            </div>
        </div>  <!-- end header-bottom -->
    </header>   <!-- end header -->

    <!-- MOBILE NAV -->
    <div id="mobile-nav"></div>
</div>  <!-- end freakers-wrapper -->

JS JS

(function($) {
    "use strict";

    $(document).ready(function () {

        $(window).on('load resize', function(){
            moveNavigation();
        });

        /* ----------------------------------------------------------------------
            Main Menu
        ---------------------------------------------------------------------- */

        //if you change this breakpoint, don't forget to update this value as well
        var MqL = 1030,
            menuLeft = $('.menu-wrapper-left').html(),
            menuRight = $('.menu-wrapper-right').html();
            console.log(menuRight);
            console.log(menuLeft);
        //move nav element position according to window width
        // moveNavigation();

        //mobile - open lateral menu clicking on the menu icon
        $(document).on('click', '.menu-trigger', function(event){
            event.preventDefault();
            if($('#freakers-wrapper').hasClass('push-content')){
                closeNav();
            }else{
                $('#freakers-wrapper').addClass('push-content');
                $('#mobile-nav .menu').addClass('menu--open');
                $(this).addClass('nav-is-visible');
            }
        });

        //open submenu
        $('.has-children').on('click', function(event){
            var selected = $(this);
            if( selected.children('ul').hasClass('is-hidden') ) {
                selected.children('ul').removeClass('is-hidden');
            }
        });

        //submenu items - go back link
        $('.go-back').on('click', function(evt){
            evt.stopPropagation();
            $(this).parent('ul')
                .addClass('is-hidden')
                .parent('.has-children')
                .parent('ul');
        });

        function closeNav(){
            $('#freakers-wrapper').removeClass('push-content');
            $('.menu--main').removeClass('menu--open');
            $('.has-children ul').addClass('is-hidden');
        }

        function checkWindowWidth() {
            //check window width (scrollbar included)
            var e = window, 
                a = 'inner';
            if (!('innerWidth' in window )) {
                a = 'client';
                e = document.documentElement || document.body;
            }
            if ( e[ a+'Width' ] >= MqL ){
                closeNav();

                if ( $('.menu-trigger').hasClass('menu-trigger-open') ){
                    $('.menu-trigger').removeClass('menu-trigger-open');
                }
                return true;
            } else {

                var menuElm = $('.main-menu .has-children');

                if($('.sub-menu ul', menuElm).hasClass('left-menu')){
                    $('.sub-menu ul', menuElm).removeClass('left-menu');
                }
                return false;
            }
        }

        function moveNavigation(){
            var navigation = $('.menu--main'),
                desktop = checkWindowWidth();

            if ( desktop ) {
                $('#mobile-nav').children().remove();
                $('.menu-wrapper-left').html(menuLeft);
                $('.menu-wrapper-right').html(menuRight);
            } else {
                navigation.appendTo('#mobile-nav');
                $('.menu--main').not(':first').remove().children('li').appendTo('.menu--main:first');
            }
        }

        $(".menu-trigger").click(function() {
            $(this).toggleClass("menu-trigger-open");
        });
    });
}(jQuery));

If you want to see it in action, I've made a Codepen (try resize to see it working) 如果您想看到它的实际效果,我已经制作了一个Codepen(尝试调整大小以使其正常工作)

http://codepen.io/thehung1724/full/JYmzWr/ http://codepen.io/thehung1724/full/JYmzWr/

I hope I could explain well my problem. 我希望我能很好地解释我的问题。 I've searched and didn't found a solution for this issue (or maybe I just didn't know really well what to look for). 我已经搜索过但没有找到解决此问题的解决方案(或者也许我只是不太清楚该寻找什么)。 Although when you resize the screen, you can still click on the menu icon, but please notice it doesn't transform to 'X' letter and you can't click to show sub-menu 尽管调整屏幕大小时,您仍然可以单击菜单图标,但是请注意,它不会转换为'X'字母,并且您无法单击以显示子菜单

Works 作品

在此处输入图片说明

Doesn't work 不起作用

在此处输入图片说明

Any help would be appreciated! 任何帮助,将不胜感激!

An easy fix would be to replace 一个简单的解决方法是更换

$('.has-children').on('click', function(event){

with

$(document).on('click', '.has-children', function (event) {

When you clone the nav from one place to another you are losing the pointer to the on click function for .has-children 当您将导航从一个地方克隆到另一个地方时,您将失去指向.has-children的on click函数的指针

With the $(document).on.... you're actually binding the function to the document which doesn't get disposed. 使用$(document).on ....,实际上是将函数绑定到不会被处理的文档。


Update: Replace the following section of javascript 更新:替换以下javascript部分

        $('.has-children').on('click', function (event) {
            var selected = $(this);
            if (selected.children('ul').hasClass('is-hidden')) {
                selected.children('ul').removeClass('is-hidden');
            }
        });
        $('.go-back').on('click', function (evt) {
            evt.stopPropagation();
            $(this).parent('ul').addClass('is-hidden').parent('.has-children').parent('ul');
        });

With

    $(document).on('click', '.has-children', function (event) {
        var selected = $(this);
        if (selected.children('ul').hasClass('is-hidden')) {
            selected.children('ul').removeClass('is-hidden');
        }
    });
    $(document).on('click', '.go-back', function (evt) {
        evt.stopPropagation();
        $(this).parent('ul').addClass('is-hidden').parent('.has-children').parent('ul');
    });

Further update: I've Forked your code in CodePen with the above changes: http://codepen.io/anon/pen/JYmVXm 进一步更新:通过以上更改,我已在CodePen中分叉了您的代码: http ://codepen.io/anon/pen/JYmVXm

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

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