响应式导航 - 单击时如何更改菜单图标

时间:2013-12-28 13:41:59

标签: javascript css nav

我正在使用此响应式菜单:http://www.hongkiat.com/blog/responsive-web-nav/

我想对导航进行这两项改进,但我似乎无法知道如何做到这一点:

1:点击菜单图标时,我希望图标更改为其他图标。确切地说,我只是希望菜单图标(3行)在单击时垂直翻转。当我将光标悬停在翻转的图标上时,我想要标题:'关闭菜单'

2:当我点击菜单外面时,我还想关闭菜单。我已完成建议的here,但是当菜单图标未显示时,给出的建议也会关闭菜单。

jQuery的:

$(function() {
    var pull       = $('#pull');
        menu       = $('.nav-top ul');
        menuHeight = menu.height();
    $(pull).on('click', function(e) {
        e.stopPropagation();
        e.preventDefault();
        menu.slideToggle();
    });
    $(document).on('click',function(e){
        if(!menu.is(":hidden")){
            menu.slideToggle();
        }
    });
    $(window).resize(function(){
        var w = $(window).width();
        if(w > 320 && menu.is(':hidden')) {
            menu.removeAttr('style');
        }
    });
});

CSS:

a#pull{display:none}
@media all and (min-width: 150px) and (max-width: 319px) { 
    .nav-top ul {
        display: none;
        height: auto;
        width: 100%
    }
    .nav-top li {
        width: 100%
    }
    .nav-top a {
        width: 100%;
        text-align: left;
        text-indent: 5px;
        border-bottom: 1px solid #576979;
        border-right: 1px solid #576979
    }
    .nav-top a#pull {
        background-color: #283744;
        display: block;
        position: relative;
        width: 100%
    }
    .nav-top a#pull:after {
        background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAJklEQVQ4y2P8//8/AzUB438qmzhCDWQYeQaOJpvRZDMay0M/2QAA9B1Hzdwz85IAAAAASUVORK5CYII=)no-repeat;
        content: "";
        display: inline-block;
        height: 30px;
        position: absolute;
        right: 0;
        top: 10px;
        width: 22px
    }
}

编辑1:

JQUERY

$(function() {
            var pull    = $('.pull');
            menu        = $('.nav-top ul');
            menuHeight  = menu.height();

            $(pull).on('click', function(e) {
                e.stopPropagation();
                e.preventDefault();
                menu.slideToggle();
            });if(pull.is(':visible')){
            $(document).on('click',function(e){
                if(!menu.is(":hidden")){
                    menu.slideToggle();
                }
            });}

            $(window).resize(function(){
                var w = $(window).width();
                if(w > 320 && menu.is(':hidden')) {
                    menu.removeAttr('style');
                    pull.addClass('.vertical');
                }else {
    pull.removeClass('vertical');}

            });
        });

HTML

<nav class="nav-top clearfix">
<ul class=clearfix>
<li><a class="home-link" href="/">Home</a></li>
<li><a class="biography-link" href="/biography">Biography</a></li>
<li><a class="music-link" href="/music">Music</a></li>
<li><a class="gallery-link" href="/gallery">Gallery</a></li>
<li><a class="agenda-link" href="/agenda">Agenda</a></li>
<li><a class="contact-link" href="/contact">Contact</a></li>
</ul>
<a href="#" class="pull" title="Open Menu">Menu</a>
<a href="#" class="pull" title="Close Menu">Menu</a>
</nav>

CSS

a.pull{display:none}
@media all and (min-width: 150px) and (max-width: 319px) { 
    .nav-top ul {
        display: none;
        height: auto;
        width: 100%
    }
    .nav-top li {
        width: 100%
    }
    .nav-top a {
        width: 100%;
        text-align: left;
        text-indent: 5px;
        border-bottom: 1px solid #576979;
        border-right: 1px solid #576979
    }
    .nav-top a.pull {
        background-color: #283744;
        display: block;
        position: relative;
        width: 100%
    }
    .nav-top a.pull:after {
        background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAJklEQVQ4y2P8//8/AzUB438qmzhCDWQYeQaOJpvRZDMay0M/2QAA9B1Hzdwz85IAAAAASUVORK5CYII=)no-repeat;
        content: "";
        display: inline-block;
        height: 30px;
        position: absolute;
        right: 0;
        top: 10px;
        width: 22px
    }


.vertical {
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAAeElEQVQ4jcWSwQmAMBAEB7EAidp/JRpEEQyKDwvShxeU/DYPXQiBwAxH9uDDeOB8nU4VjIngVAUO2HIFDbAbtOYIggG7TTICgyKYgAWoFei/NMAM9ArkuRfD8fx2UASxltjzAbQ5gthzpcCpQNrzwu4peS/VCbJzAb0PK1vzdF+uAAAAAElFTkSuQmCC);
    content: "";
    display: inline-block;
    height: 30px;
    position: absolute;
    right: 0;
    top: 10px;
    width: 22px;}

1 个答案:

答案 0 :(得分:0)

  1. 您可以在点击完成后添加课程。

    $(pull).on('click', function(e) {
    pull.addClass('.vertical')

    然后编辑垂直类:

    .yourbutton .vertical {
        ...
        float: left;
        height: 18px;
        margin-right: 5px;
        margin-top: 1px;
        width: 2px;}

    您必须删除该类,因此此代码优于第一个:

    if(!menu.is(":hidden")){
        pull.addClass('.vertical');
    } else {
        pull.removeClass('vertical');
    }

    最后,添加一个属性标题:a href“#”title =“Close menu”id =“pull”

  2. 仅在拉出可见时才制作动画:

    if(pull.is(':visible')){
    // Do your stuff
    }

    然后,你的css文件应该是这样的:

    #pull{display:none;}
    
    @media only screen and (max-width : 320px) {
        #pull{display:block}
    }

    希望它能帮到你!