简体   繁体   English

移动屏幕上的可点击子菜单

[英]Clickable Submenu on Mobile Screens

I am building a WP theme and using default sub-menu behavior as user mouseover and drop down sub-menu appears. 我正在构建WP主题,并使用默认的子菜单行为作为用户鼠标悬停并显示下拉子菜单。 but as on mobile screen hover feature don't work and i simply hide the sub-menu on mobile resolution. 但是由于无法在移动屏幕上使用悬停功能,因此我只是隐藏了移动分辨率下的子菜单。 But as we can see in WP dashboard the sub menus converted with clickable feature and we can access the sub-menu by clicking the parent. 但是,正如我们在WP仪表板中看到的那样,子菜单是通过可点击功能转换的,我们可以通过单击父菜单来访问子菜单。 how can i implement this feature in my theme? 如何在主题中实现此功能?

Here is what you are looking for 这是您要寻找的

<!DOCTYPE html>
<html>

<head>
    <style>
        a {
            text-decoration: none;
        }
        .container {
            width: 90%;
            max-width: 900px;
            margin: 10px auto;
        }
        .toggleMenu {
            display: none;
            background: #666;
            padding: 10px 15px;
            color: #fff;
        }
        .nav {
            list-style: none;
            *zoom: 1;
            background: #175e4c;
        }
        .nav:before,
        .nav:after {
            content: " ";
            display: table;
        }
        .nav:after {
            clear: both;
        }
        .nav ul {
            list-style: none;
            width: 9em;
        }
        .nav a {
            padding: 10px 15px;
            color: #fff;
        }
        .nav li {
            position: relative;
        }
        .nav > li {
            float: left;
            border-top: 1px solid #104336;
        }
        .nav > li > .parent {
            background-image: url("images/downArrow.png");
            background-repeat: no-repeat;
            background-position: right;
        }
        .nav > li > a {
            display: block;
        }
        .nav li ul {
            position: absolute;
            left: -9999px;
        }
        .nav > li.hover > ul {
            left: 0;
        }
        .nav li li.hover ul {
            left: 100%;
            top: 0;
        }
        .nav li li a {
            display: block;
            background: #1d7a62;
            position: relative;
            z-index: 100;
            border-top: 1px solid #175e4c;
        }
        .nav li li li a {
            background: #249578;
            z-index: 200;
            border-top: 1px solid #1d7a62;
        }
        @media screen and (max-width: 768px) {
            .active {
                display: block;
            }
            .nav > li {
                float: none;
            }
            .nav > li > .parent {
                background-position: 95% 50%;
            }
            .nav li li .parent {
                background-image: url("images/downArrow.png");
                background-repeat: no-repeat;
                background-position: 95% 50%;
            }
            .nav ul {
                display: block;
                width: 100%;
            }
            .nav > li.hover > ul,
            .nav li li.hover ul {
                position: static;
            }
        }
    </style>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
    <div class="container">

        <a class="toggleMenu" href="#">Menu</a>
        <ul class="nav">
            <li class="test">
                <a href="#">Shoes</a>
                <ul>
                    <li>
                        <a href="#">Womens</a>
                        <ul>
                            <li><a href="#">Sandals</a>
                            </li>
                            <li><a href="#">Loafers</a>
                            </li>
                            <li><a href="#">Flats</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Mens</a>
                        <ul>
                            <li><a href="#">Loafers</a>
                            </li>
                            <li><a href="#">Sneakers</a>
                            </li>
                            <li><a href="#">Formal</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>



            <li>
                <a href="#">Shipping Info</a>
            </li>
        </ul>
    </div>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        var ww = document.body.clientWidth;

        $(document).ready(function() {
            $(".nav li a").each(function() {
                if ($(this).next().length > 0) {
                    $(this).addClass("parent");
                };
            })

            $(".toggleMenu").click(function(e) {
                e.preventDefault();
                $(this).toggleClass("active");
                $(".nav").toggle();
            });
            adjustMenu();
        })

        $(window).bind('resize orientationchange', function() {
            ww = document.body.clientWidth;
            adjustMenu();
        });

        var adjustMenu = function() {
            if (ww < 768) {
                $(".toggleMenu").css("display", "inline-block");
                if (!$(".toggleMenu").hasClass("active")) {
                    $(".nav").hide();
                } else {
                    $(".nav").show();
                }
                $(".nav li").unbind('mouseenter mouseleave');
                $(".nav li a.parent").unbind('click').bind('click', function(e) {
                    // must be attached to anchor element to prevent bubbling
                    e.preventDefault();
                    $(this).parent("li").toggleClass("hover");
                });
            } else if (ww >= 768) {
                $(".toggleMenu").css("display", "none");
                $(".nav").show();
                $(".nav li").removeClass("hover");
                $(".nav li a").unbind('click');
                $(".nav li").unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
                    // must be attached to li so that mouseleave is not triggered when hover over submenu
                    $(this).toggleClass('hover');
                });
            }
        }
    </script>
</body>

</html>

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

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