gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:38:07 25 4
gpt4 key购买 nike

我正在使用这个响应式菜单: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. 您可以在点击完成后添加一个类。

    $(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');}

    最后,添加一个属性 title :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}}

    希望对您有所帮助!

关于javascript - 响应式导航 - 单击时如何更改菜单图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com