移动设备菜单

时间:2018-05-31 13:21:44

标签: javascript jquery mobile menu

我找到了很好的菜单(Ace Responsive Menu Plugin),风格很好看。它对于桌面工作正常,但对于移动设备非常糟糕:我的意思是在平板电脑上扩展视图(快速向上滑动)。倒塌的版本运作良好。我不擅长javascript并且认为对于谁知道javascript来说这不是一个大问题。

请帮助解决这个问题。

https://github.com/samsono/Ace-Responsive-Menu

/* 
Ace Responsive Menu Plugin
Version: 1.0
Author: Samson.Onna 
Email : samson3d@gmail.com
----------------------------------------*/

(function ($) {
    $.fn.aceResponsiveMenu = function (options) {

        //plugin's default options
        var defaults = {
            resizeWidth: '768',
            animationSpeed: 'slow',
            accoridonExpAll: false
        };

        //Variables
        var options = $.extend(defaults, options),
            opt = options,
            $resizeWidth = opt.resizeWidth,
            $animationSpeed = opt.animationSpeed,
            $expandAll = opt.accoridonExpAll,
            $aceMenu = $(this),
            $menuStyle = $(this).attr('data-menu-style');

        // Initilizing        
        $aceMenu.find('ul').addClass("sub-menu");
        $aceMenu.find('ul').siblings('a').append('<span class="arrow "></span>');
        if ($menuStyle == 'accordion') { $(this).addClass('collapse'); }

        // Window resize on menu breakpoint 
        if ($(window).innerWidth() <= $resizeWidth) {
            menuCollapse();
        }
        $(window).resize(function () {
            menuCollapse();
        });

        // Menu Toggle
        function menuCollapse() {
            var w = $(window).innerWidth();
            if (w <= $resizeWidth) {
                $aceMenu.find('li.menu-active').removeClass('menu-active');
                $aceMenu.find('ul.slide').removeClass('slide').removeAttr('style');
                $aceMenu.addClass('collapse hide-menu');
                $aceMenu.attr('data-menu-style', '');
                $('.menu-toggle').show();
            } else {
                $aceMenu.attr('data-menu-style', $menuStyle);
                $aceMenu.removeClass('collapse hide-menu').removeAttr('style');
                $('.menu-toggle').hide();
                if ($aceMenu.attr('data-menu-style') == 'accordion') {
                    $aceMenu.addClass('collapse');
                    return;
                }
                $aceMenu.find('li.menu-active').removeClass('menu-active');
                $aceMenu.find('ul.slide').removeClass('slide').removeAttr('style');
            }
        }

        //ToggleBtn Click
        $('#menu-btn').click(function () {
            $aceMenu.slideToggle().toggleClass('hide-menu');
        });


        // Main function 
        return this.each(function () {
            // Function for Horizontal menu on mouseenter
            $aceMenu.on('mouseover', '> li a', function () {
                if ($aceMenu.hasClass('collapse') === true) {
                    return false;
                }
                $(this).off('click', '> li a');
                $(this).parent('li').siblings().children('.sub-menu').stop(true, true).slideUp($animationSpeed).removeClass('slide').removeAttr('style').stop();
                $(this).parent().addClass('menu-active').children('.sub-menu').slideDown($animationSpeed).addClass('slide');
                return;
            });
            $aceMenu.on('mouseleave', 'li', function () {
                if ($aceMenu.hasClass('collapse') === true) {
                    return false;
                }
                $(this).off('click', '> li a');
                $(this).removeClass('menu-active');
                $(this).children('ul.sub-menu').stop(true, true).slideUp($animationSpeed).removeClass('slide').removeAttr('style');
                return;
            });
            //End of Horizontal menu function

            // Function for Vertical/Responsive Menu on mouse click
            $aceMenu.on('click', '> li a', function () {
                if ($aceMenu.hasClass('collapse') === false) {
                    //return false;
                }
                $(this).off('mouseover', '> li a');
                if ($(this).parent().hasClass('menu-active')) {
                    $(this).parent().children('.sub-menu').slideUp().removeClass('slide');
                    $(this).parent().removeClass('menu-active');
                } else {
                    if ($expandAll == true) {
                        $(this).parent().addClass('menu-active').children('.sub-menu').slideDown($animationSpeed).addClass('slide');
                        return;
                    }
                    $(this).parent().siblings().removeClass('menu-active');
                    $(this).parent('li').siblings().children('.sub-menu').slideUp().removeClass('slide');
                    $(this).parent().addClass('menu-active').children('.sub-menu').slideDown($animationSpeed).addClass('slide');
                }
            });
            //End of responsive menu function

        });
        //End of Main function
    }
})(jQuery);
/* 
Ace Responsive Menu Plugin
Version: 1.0
Author: Samson Onna
E-mail: samson3d@gmail.com
----------------------------------------*/
/* Import Fonts
----------------------------------------*/
@import url(http://fonts.googleapis.com/css?family=Roboto);

/* Ace Responsive Menu
----------------------------------------*/
a {
    text-shadow: none;
    color: #0d638f;
}
ul {
    margin: 0px;
    padding: 0px;
}
.centered{
	display: table;
	margin: 0 auto;
}
.menuholder{
	width:100%;
	background: #1067ac;  
    border-bottom: 3px solid #98d522;	
}
.ace-responsive-menu {
    list-style: none;
    margin: 0;
    padding: 0;
    float:left;
    width:100%;
    font-family: 'Roboto', sans-serif;
}
.ace-responsive-menu li{
    list-style: none;
}
.ace-responsive-menu li ul {
    display:none;
}
.ace-responsive-menu > li {
    display: block;
    margin: 0;
    padding: 0;
    border: 0px;
    float: left;
}
.ace-responsive-menu li a {
        color:#fff;
}
.ace-responsive-menu > li > a {
    display: block;
    position: relative;
    margin: 0;
    border: 0px;
    padding: 12px 20px 12px 12px;
    text-decoration: none;
    font-size: 16px;
    font-weight: 300;
    color: #fff;
}
.ace-responsive-menu li a i {
    padding-right: 5px;
    color: #FF5737;
}
.ace-responsive-menu > li > a i {
    font-size: 16px;
    text-shadow: none;
    color: #FF5737;
}
.ace-responsive-menu li ul.sub-menu li a i {
    padding-right: 10px;
}
.ace-responsive-menu li.menu-active > a {
    background: #98d522 !important;
    color:#fff;
}
.ace-responsive-menu li .menu-active {
    position: relative;
}
.ace-responsive-menu > li > a > .arrow:before {  
    margin-left: 15px;
    display: inline;
    font-size: 16px;
    font-family: FontAwesome;
    height: auto;
    content: "\f107";
    font-weight: 300;
    text-shadow: none;
    width: 10px;
    display: inline-block;
}
.ace-responsive-menu li ul.sub-menu li > a > .arrow:before {
    content: "\f105" !important;
}
.ace-responsive-menu > li > ul.sub-menu {
    display: none;
    list-style: none;
    clear: both;
    margin: 0;
    position: absolute;
}
.ace-responsive-menu li ul.sub-menu {
    background: #1067ac;
}
.ace-responsive-menu li ul.sub-menu > li {
    width: 185px;
}
.ace-responsive-menu li ul.sub-menu li a {
    display: block;
    margin: 0px 0px;
    padding: 12px 20px 12px 15px; /*12px 20px 12px 15px*/
    text-decoration: none;
    font-size: 15px;
    font-weight: normal;
    background: none;
}
.ace-responsive-menu > li > ul.sub-menu > li {
    position: relative;
}
.ace-responsive-menu > li > ul.sub-menu > li ul.sub-menu {
    position: absolute;
    left: 185px;
    top: 0px;
    display: none;
    list-style: none;
}
.ace-responsive-menu > li > ul.sub-menu > li ul.sub-menu > li ul.sub-menu {
    position: absolute;
    left: 185px;
    top: 0px;
    display: none;
    list-style: none;
}
.ace-responsive-menu > li > ul.sub-menu li > a > .arrow:before {
    float: right;
    margin-top: 1px;
    margin-right: 0px;
    display: inline;
    font-size: 16px;
    font-family: FontAwesome;
    height: auto;
    content: "\f104";
    font-weight: 300;
    text-shadow: none;
}

/* Menu Toggle Btn
----------------------------------------*/
.menu-toggle {
    display: none;
    float: left;
    width: 100%;
    background: #1067ac;
}
.menu-toggle h3 {
    float: left;
    color: #FFF;
    padding: 0px 10px;
    font-weight: 600;
    font-size: 16px;
}
.menu-toggle .icon-bar {
    display: block !important;
    width: 18px;
    height: 2px;
    background-color: #F5F5F5 !important;
    -webkit-box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
    -moz-box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
    margin: 3px;
}
.menu-toggle .icon-bar:hover {
    background-color: #F5F5F5 !important;
}
.menu-toggle #menu-btn {
    float: right;
    background: #98d522;
    border: 1px solid #fff;
    padding: 8px;
    cursor: pointer;
    margin: 10px;
}
.hide-menu {
    display: none;
}


/* Accordion Menu Styles
----------------------------------------*/

ul[data-menu-style="accordion"] {
    width: 250px;
}
ul[data-menu-style="accordion"] > li {    
    display: block;
    margin: 0;
    padding: 0;
    border: 0px;
    float: none !important;
}
ul[data-menu-style="accordion"] > li:first-child {
    border-top: 2px solid #FD5025;
}
ul[data-menu-style="accordion"] li ul.sub-menu > li {
    width: 100%;
}
ul[data-menu-style="accordion"] > li > a > .arrow:before {
    float: right;
    content: "\f105";
}
ul[data-menu-style="accordion"] li.menu-active > a > .arrow:before {
    content: "\f107" !important;
}
ul[data-menu-style="accordion"] > li > ul.sub-menu {
    position: static;
}
ul[data-menu-style="accordion"] > li > a i {
    padding-right: 10px;
    color: #FF5737;
}
ul[data-menu-style="accordion"] > li > ul.sub-menu > li ul.sub-menu {
    position: static;
}
ul[data-menu-style="accordion"] > li > ul.sub-menu > li ul.sub-menu > li ul.sub-menu {
    position: static;
}
ul[data-menu-style="accordion"] > li {
    border-bottom: 1px solid #242424;
}
ul[data-menu-style="accordion"] li a:hover {
    background: #98d522 !important;
}
ul[data-menu-style="accordion"] ul.sub-menu li.menu-active > a > .arrow:before {
    content: "\f107" !important;
}

/* Vertical Menu Styles
----------------------------------------*/

ul[data-menu-style="vertical"] {
    width: 200px;
}
ul[data-menu-style="vertical"] > li {
    float: none;
}
ul[data-menu-style="vertical"] > li:first-child {
    border-top: 2px solid #FD5025;
}
ul[data-menu-style="vertical"] li ul.sub-menu > li {
    width: 100%;
}
ul[data-menu-style="vertical"] > li > a > .arrow:before {
    float: right;
    content: "\f105";
}
ul[data-menu-style="vertical"] > li.menu-active {
position:relative;
}
ul[data-menu-style="vertical"] > li > ul.sub-menu {
    position: absolute;
    left:200px;
    top:0px;
    width:200px;
}
ul[data-menu-style="vertical"] > li > a i {
    padding-right: 10px;
    color: #FF5737;
}
ul[data-menu-style="vertical"]> li > ul.sub-menu > li ul.sub-menu {
    position: absolute;
    width:200px;
    left: 200px;
}
ul[data-menu-style="vertical"] > li > ul.sub-menu > li ul.sub-menu > li ul.sub-menu {
    position: absolute;
    width:200px;
    left: 200px;
}
ul[data-menu-style="vertical"] > li {
    border-bottom: 1px solid #242424;
}
ul[data-menu-style="vertical"] li a:hover {
    background: #98d522 !important;
}

/* Responsive Menu Styles
----------------------------------------*/
/*Note: change the max-width asper your requirment and change the same in aceResponsiveMenu({resizeWidth: "768" }) function*/

@media screen and (max-width: 768px) {
    .centered {
		width:98%;
    }
    ul[data-menu-style="vertical"] , ul[data-menu-style="accordion"],
    ul[data-menu-style="vertical"] li ul.sub-menu {
        width: 100% !important;
    } 
    .ace-responsive-menu {
        float: left;
        width:100%;
    }
    .ace-responsive-menu > li {
        border-bottom: 1px solid #fff;
       float: none;
    }   
    .ace-responsive-menu li a:hover {
        background: #98d522 !important;
    }
    .ace-responsive-menu > li:first-child {
        border-top: 2px solid #98d522;
    }    
    .ace-responsive-menu > li > a i {
        padding-right: 10px;
        color: #FF5737;
    }
    .ace-responsive-menu > li > a > .arrow:before {
        float: right;
        content: "\f105";
    }
    li.menu-active > a > .arrow:before {
        content: "\f107" !important;
    }
    .ace-responsive-menu li ul.sub-menu > li {
        width: 100%;
    }
    .ace-responsive-menu li ul.sub-menu li ul.sub-menu li a
        {
        padding-left: 30px;
    }  
    .ace-responsive-menu li ul.sub-menu li ul.sub-menu li ul.sub-menu li a 
       {
        padding-left: 50px;
    }  
    .ace-responsive-menu > li > ul.sub-menu {
        position: static;
    }
    .ace-responsive-menu > li > ul.sub-menu > li ul.sub-menu {
        position: static;
    }
    .ace-responsive-menu > li > ul.sub-menu > li ul.sub-menu > li ul.sub-menu {
        position: static;
    }
    .ace-responsive-menu li ul.sub-menu li.menu-active > a > .arrow:before {
        content: "\f107" !important;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Ace Responsive Menu</title>
	
	<!--Responsiveness-->
    <meta content="width=device-width, initial-scale=1.0" name="viewport" />
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    
	<!--FontAwesome-->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <!--[if IE 7]>
    <link rel="stylesheet" href="font-awesome/css/font-awesome-ie7.min.css">
    <![endif]-->
	
	<!--CSS Style-->
	    <!--Scripts-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
     <script type="text/javascript">
         $(document).ready(function () {
             $("#respMenu").aceResponsiveMenu({
                 resizeWidth: '768', // Set the same in Media query       
                 animationSpeed: 'fast', //slow, medium, fast
                 accoridonExpAll: false //Expands all the accordion menu on click
             });
         });
	</script>
</head>
<body>
    <div class="menuholder">
    <nav class="centered">
        <!-- Menu Toggle btn-->
        <div class="menu-toggle">
            <h3>Menu</h3>
            <button type="button" id="menu-btn">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <!-- Responsive Menu Structure-->
        <!--Note: declare the Menu style in the data-menu-style="horizontal" (options: horizontal, vertical, accordion) -->
        <ul id="respMenu" class="ace-responsive-menu" data-menu-style="horizontal">
             <li>
                <a href="javascript:void(0);">Home</a>
            </li>
            <li>
                <a href="javascript:void(0);">About Us</a>
                <!-- Level Two-->
                <ul>
                    <li>
                        <a href="#">Sub Item One</a>
                    </li>
                    <li>
                        <a href="#">Sub Item Two</a>
                    </li>
                    <li>
                        <a href="#">Sub Item Three</a>
                    </li>
                    <li>
                        <a href="#">Sub Item Four</a>
                    </li>
                </ul>
            </li>

            <li>
                <a href="javascript:void(0);">4 Level Menu</a>
                <!-- Level Two-->
                <ul>
                    <li>
                        <a href="javascript:void(0);">Sub Item One</a>
                    </li>
                    <li>
                        <a href="#">Sub Item Two</a>
                    </li>
                    <li>
                        <a href="javascript:void(0);">Sub Item Three</a>
                        <!-- Level Three-->
                        <ul>
                            <li><a href="#">Sub Item Link 1</a></li>
                            <li>
                                <a href="javascript:void(0);">Sub Item Link 2</a>
                                <!-- Level Four-->
                                <ul>
                                    <li><a href="#">Sub Item Link 1</a></li>
                                    <li><a href="#">Sub Item Link 2</a></li>
                                    <li><a href="#">Sub Item Link 3</a></li>
                                </ul>
                            </li>
                            <li><a href="#">Sub Item Link 3</a></li>
                        </ul>
                    </li>

                    <li>
                        <a href="#">Sub Item Four</a>
                    </li>
                </ul>
            </li>
            <li>
                <a class="" href="javascript:void(0);">Services</a>
                <ul>
                    <li>
                        <a href="#">Sub Item One</a>
                    </li>
                    <li>
                        <a href="javascript:void(0);">Sub Item Two</a>
                        <ul>
                            <li><a href="#">Sub Item Link 1</a></li>
                            <li><a href="#">Sub Item Link 2</a></li>
                            <li><a href="#">Sub Item Link 3</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="javascript:void(0);">Sub Item Three							
                        </a>
                        <ul>
                            <li><a href="#">Sub Item Link 1</a></li>
                            <li><a href="#">Sub Item Link 1</a></li>
                            <li><a href="#">Sub Item Link 1</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Sub Item Four
                        </a>
                    </li>
                </ul>
            </li>
            <li>
                <a href="javascript:void(0);">Products</a>
            </li>
            <li class="last">
                <a href="javascript:void(0);">Contact Us</a>
            </li>
        </ul>
    </nav>
	</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您希望移动设备只需在平板电脑上查看,则可以编辑/覆盖

中的默认css
@media screen and (max-width: 768px) {

.centered {
        width:98%; /* for mobile*/
    }
....
} 

@media screen and (max-width: 991px) {
    .centered {
        width:98%; /* for tablet*/
    }
.....
}

并在设置

后在平板电脑上获得相同的行为
$("#respMenu").aceResponsiveMenu({
                 resizeWidth: '991', // Set the same in Media query       
                 animationSpeed: 'slow', //slow, medium, fast
                 accoridonExpAll: false //Expands all the accordion menu on click
             });