简体   繁体   English

jQuery类交换未按预期运行

[英]JQuery class swap not acting as expected

I'm writing a script whose function is to convert a drop down menu to an accordion style menu depending on the screen width. 我正在编写一个脚本,其功能是根据屏幕宽度将下拉菜单转换为手风琴样式菜单。 To do this, the script swaps out class names based on the width. 为此,脚本会根据宽度换出类名称。 Visually, everything works as expected when the screen size changes. 从视觉上看,当屏幕大小更改时,一切都按预期工作。 Also, if I open up the developer tools in chrome and adjust screen width, I can see the class names change in HTML. 另外,如果我使用chrome打开开发人员工具并调整屏幕宽度,则可以看到HTML中的类名已更改。

Functionally, however, the browser seems to hold onto whatever class name it was when the page loaded. 从功能上讲,浏览器似乎保留了页面加载时的类名。 Here's the script: 这是脚本:

$(function()
{       
    var screenWidth;
    var screenMode;
    var initial_check = 0;
    var currentMode;

    //check main window width
    function checkWidth()
    {
        screenWidth = $(window).width();

        //main size changes
        if (screenWidth >= 769)
        {
            screenMode = "large";
            changeDisplayMode("large");
        }
        else if (screenWidth < 769 && screenWidth > 350)
        {
            screenMode = "medium";
            changeDisplayMode("medium");
        }
        else if (screenWidth < 350)
        {
            screenMode = "medium";
            changeDisplayMode("medium");
        }

        //size change to stack View Cart items and search button
        if (screenWidth < 603)
        {
            //show search magnify glass

        }
    }

    //check width when entering page
    checkWidth();

    $(window).resize(checkWidth);


    /////////////////////////////////////////////////////////////////////////////////
    ///////////                                                               ///////
    ///////////   Code for navigation transition from drop down to accordion  ///////
    ///////////                                                               ///////
    /////////////////////////////////////////////////////////////////////////////////

    function changeDisplayMode(mode)
    {
        if (currentMode != mode)
        {
            if (mode == "large")
            {   
                //set on first launch
                initial_check = 1;

                if (initial_check == 1)
                {
                    $("#header_image").show();

                    $("nav").removeClass("nav_medium").addClass("nav_large");
                    $("div.main_menu_item_medium").removeClass("main_menu_item_medium").addClass("main_menu_item_large");
                    $("div.sub_menu_medium").removeClass("sub_menu_medium").addClass("sub_menu_large");
                    $("div.sub_menu_item_medium").removeClass("sub_menu_item_medium").addClass("sub_menu_item_large");
                }

                currentMode = "large";
            }
            else if (mode == "medium")
            {
                //set on first launch
                initial_check = 1;

                if (initial_check == 1)
                {
                    $("#header_image").hide();

                    $("nav").removeClass("nav_large").addClass("nav_medium");
                    $("div.main_menu_item_large").removeClass("main_menu_item_large").addClass("main_menu_item_medium");
                    $("div.sub_menu_large").removeClass("sub_menu_large").addClass("sub_menu_medium");
                    $("div.sub_menu_item_large").removeClass("sub_menu_item_large").addClass("sub_menu_item_medium");
                }   

                currentMode = "medium";
            }       
        }   
    }


    //hide inital dropdown menus
    $('.nav_large li ul').hide();       
    $('.sub_menu_large').hide();
    $('.nav_medium li ul').hide();      
    $('.sub_menu_medium').hide();

    //navigation hover in large mode
    $('.nav_large ul li').hover(

        function()
        {
            $('.sub_menu_large', this).show();
            console.log("large hover");
        },

        function()
        {
            $('.sub_menu_large', this).hide();
            console.log("large hidden");
        }

    );

    //navigation hover in medium mode
    $('.nav_medium ul li').hover(

        function()
        {
            $('.sub_menu_medium', this).show();
            console.log("medium hover");
        },

        function()
        {
            $('.sub_menu_medium', this).hide();
            console.log("medium hidden");
        }

    );

    //accordion style clicks in medium mode
    $('.nav_medium ul li').click (

        function()
        {
            $('.sub_menu_medium', this).show();
            console.log("clicked medium");
        }

    );


});

As you can see, there's a large and medium mode. 正如你所看到的,有一个largemedium模式。 If I load the page in large mode, adjust the browser to medium, and hover over an element, it still logs as it's in large mode. 如果我以大模式加载页面,将浏览器调整为中,然后将鼠标悬停在某个元素上,它仍然会以大模式记录。 Same thing going from medium to large. 同样的事情从大到大。 As stated, I can see the HTML change correctly when viewing the source. 如前所述,查看源代码时,我可以看到HTML正确更改。

So, I guess I'm looking for some sort of refresh that doesn't reload the page? 所以,我想我正在寻找某种不会重新加载页面的刷新? Is the class name cached somehow? 是否以某种方式缓存了类名? I've tested this in chrome and firefox with identical results. 我已经在chrome和firefox中测试了此结果。

The problem is that you might be changing the size of the browser to test. 问题在于您可能正在更改要测试的浏览器的大小。 However this approach will not work and will always give you the same width of the browser window no matter how much you reduce it. 但是,这种方法将不起作用,并且无论您将其缩小多少,都将始终为您提供相同宽度的浏览器窗口。

In case you want to test it for different window sizes you need to do it through the Emulator of the browsers. 如果要针对不同的窗口大小进行测试,则需要通过浏览器的Emulator器进行测试。 In chrome you can press f12 and then Esc, you will get a tab named Emulation. 在chrome中,您可以按f12键,然后按Esc键,您将获得一个名为Emulation的选项卡。 set the screen size from there and test it 从那里设置屏幕尺寸并进行测试

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

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