菜单切换jQuery时更改导航栏背景颜色

时间:2015-12-01 05:55:17

标签: javascript jquery html css

我试图在任何滚动之前在页面顶部制作导航栏,以便在切换导航菜单时将背景颜色从透明变为白色。我已经尝试添加类.whitenavbar和jQuery(/ 导航栏切换颜色 /),但它不起作用,现在阻止菜单切换?

感谢任何帮助!

https://jsfiddle.net/43taovan/

CSS:

happy-dog

的jQuery              

.navigation{
width:100%;
background: #fff;
padding-top: 40px;
padding-bottom: 40px;
}
.navlist {
display: inline-block;
}
.navlist:after {
content: '';
display: block;
height: 1.5px;
width: 0;
background: transparent;
transition: width .5s ease, background-color .5s ease;
}
.navlist:hover:after {
width: 100%;
background: grey;
}
/*----/----navlist*/

/*global styles*/
 body {
width: 100%;
margin: 0;
list-style: none;
text-decoration: none;
font-size:1.05em;
font-family: Helvetica Neue, Helvetica, Arial, Sans-serif;
}
a {
appearance: none;
font-size:1.05em;
font-family: Helvetica Neue, Helvetica, Arial, Sans-serif;
background:transparent;
color: grey;
border:none;
letter-spacing:0.15em;
text-transform:uppercase;
transition: color 0.5s ease;
list-style: none;
text-decoration: none;
}

/*----/----global styles*/

/*navigation icon*/
 #toggle-menu {
float:right;
display: block;
width: 15px;
height: 15px;
padding: 20px;
}
#toggle-menu div {
width: 15px;
height: 15px;
position: relative;
}
.header #toggle-menu span {
display: block;
width: 15px;
height: 3px;
position: absolute;
-webkit-transition: -webkit-transform 0.2s ease-in-out, top 0.2s ease-in-out 0.2s, opacity 0.2s ease-in-out 0.2s;
-moz-transition: -moz-transform 0.2s ease-in-out, top 0.2s ease-in-out 0.2s, opacity 0.2s ease-in-out 0.2s;
transition: transform 0.2s ease-in-out, top 0.2s ease-in-out 0.2s, opacity 0.2s ease-in-out 0.2s;
-webkit-transform-origin: center;
-moz-transform-origin: center;
transform-origin: center;
}
#toggle-menu span.top {
top: 0px;
}
#toggle-menu span.middle {
top: 6px;
}
#toggle-menu span.bottom {
top: 12px;
}
/*----/----navigation icon*/

/*navigation background transition*/
 .bg {
background-color: #fff !important;
border-bottom: 0.5px solid rgba(0, 0, 0, 0.2);
}
.show {
opacity: 1;
}
.navfade {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.header .logo { /* Before scroll */
color: white;
}
.bg .logo { /* After scroll */
color: #545454;
}
.navinstagram {
fill: #fff
}
.bg .navinstagram{
fill: #545454;
}

#toggle-menu span{
background: #fff;
}
.bg #toggle-menu span {
background: #545454;
}

.whitenavbar {
background: #fff;
}
/*----/----navigation background transition*/

/*navigation icon animation*/
 #toggle-menu.menu-is-active span {
-webkit-transition: -webkit-transform 0.2s ease-in-out 0.2s, top 0.2s ease-in-out, opacity 0.2s ease-in-out;
-moz-transition: -moz-transform 0.2s ease-in-out 0.2s, top 0.2s ease-in-out, opacity 0.2s ease-in-out;
transition: transform 0.2s ease-in-out 0.2s, top 0.2s ease-in-out,     opacity 0.2s ease-in-out;
}
#toggle-menu.menu-is-active span.top, #toggle-menu.menu-is-active     span.middle {
top: 6px;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
}
#toggle-menu.menu-is-active span.middle {
opacity: 0;
}
#toggle-menu.menu-is-active span.bottom {
top: 6px;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
transform: rotate(-45deg);
}
/*----/----navigation icon animation*/

/*Nav Bar*/
 .header {
/*border-bottom: 0.5px solid rgba(0,0,0,.2);*/
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 99999;
background-color: rgb(184, 184, 184);
/*background: none;*/
}
.nav {
display: none;
list-style-type: none;
position: fixed;
width: 100%;
text-align: center;
left:0;
top: 55px;
border-bottom: 0.5px solid rgba(0, 0, 0, .2);
border-top: 0.5px solid rgba(0, 0, 0, .2);
cursor: pointer;
color: #545454;
font-size:.8em;
letter-spacing:0.05em;
}
.nav li {
padding-left: 30px;
padding-right: 30px;
background: #fff;
margin-top: 50px;
margin-bottom: 50px;
}

.seemore {
display: inline-block;
padding-right: 20px;
}
#navpromo {
border-top: 0.5px solid rgba(0, 0, 0, .2);
padding-bottom: 10px;
background: #ffffff;
}
.instbtn-cont {
  display: inline-block;
  text-align: center;
  margin-top: 10px;
  padding-bottom: 30px;
}
.instbtn-cont .instbtn {
  position: relative;
  padding: 15px 20px;
  border: 1px solid grey;
  color: grey;
  -webkit-font-smoothing: antialiased;
}
.instbtn-cont .instbtn:hover {
  border: none;
}
.instbtn-cont .instbtn:hover .line-1 {
  -webkit-animation: move1 1500ms infinite ease;
      animation: move1 1500ms infinite ease;
}
.instbtn-cont .instbtn:hover .line-2 {
  -webkit-animation: move2 1500ms infinite ease;
      animation: move2 1500ms infinite ease;
}
.instbtn-cont .instbtn:hover .line-3 {
  -webkit-animation: move3 1500ms infinite ease;
      animation: move3 1500ms infinite ease;
}
.instbtn-cont .instbtn:hover .line-4 {
  -webkit-animation: move4 1500ms infinite ease;
      animation: move4 1500ms infinite ease;
}
.instbtn-cont .line-1 {
  content: "";
  display: block;
  position: absolute;
  width: 1px;
  background-color: grey;
  left: 0;
  bottom: 0;
}
.instbtn-cont .line-2 {
  content: "";
  display: block;
  position: absolute;
  height: 1px;
  background-color: grey;
  left: 0;
  top: 0;
}
.instbtn-cont .line-3 {
  content: "";
  display: block;
  position: absolute;
  width: 1px;
  background-color: grey;
  right: 0;
  top: 0;
}
.instbtn-cont .line-4 {
content: "";
display: block;
position: absolute;
height: 1px;
background-color: grey;
right: 0;
bottom: 0;
}

@-webkit-keyframes move1 {
  0% {
height: 100%;
bottom: 0;
  }
  54% {
height: 0;
bottom: 100%;
  }
  55% {
height: 0;
bottom: 0;
  }
  100% {
height: 100%;
bottom: 0;
  }
}

@keyframes move1 {
  0% {
height: 100%;
bottom: 0;
  }
  54% {
height: 0;
bottom: 100%;
  }
  55% {
height: 0;
bottom: 0;
  }
  100% {
height: 100%;
bottom: 0;
  }
}
@-webkit-keyframes move2 {
  0% {
width: 0;
left: 0;
  }
  50% {
width: 100%;
left: 0;
  }
  100% {
width: 0;
left: 100%;
  }
}
@keyframes move2 {
  0% {
width: 0;
left: 0;
  }
  50% {
width: 100%;
left: 0;
  }
  100% {
width: 0;
left: 100%;
  }
}
@-webkit-keyframes move3 {
  0% {
height: 100%;
top: 0;
  }
  54% {
height: 0;
top: 100%;
  }
  55% {
height: 0;
top: 0;
  }
  100% {
height: 100%;
top: 0;
  }
}
@keyframes move3 {
  0% {
height: 100%;
top: 0;
  }
  54% {
height: 0;
top: 100%;
  }
  55% {
height: 0;
top: 0;
  }
  100% {
height: 100%;
top: 0;
  }
}
@-webkit-keyframes move4 {
0% {
width: 0;
right: 0;
}
55% {
width: 100%;
right: 0;
  }
  100% {
width: 0;
right: 100%;
  }
}
@keyframes move4 {
  0% {
width: 0;
right: 0;
  }
  55% {
width: 100%;
right: 0;
  }
  100% {
width: 0;
right: 100%;
  }
}


/*----/----Nav Bar*/

/*in*/
 .in {
float:left;
display: inline-block;
width: 25px;
height: 25px;
padding: 15px;
cursor: pointer;
color: #fff;
font-size:.8em;
letter-spacing:0.05em;
border-top: 0.5px solid rgba(0, 0, 0, .2);
}
/*----/----in*/

/*logo*/
 .logo {
position: absolute;
left: 47%;
display: inline-block;
width: 15px;
height: 15px;
padding: 18px;
cursor: pointer;
color: #fff;
font-size:.8em;
letter-spacing:0.05em;
}
/*----/----logo*/

/****landscape****/
@media only screen and (max-width: 555px) { 
.nav li{
display: block;
padding: 30px;
}
}
/*----/----Landscape*/

HTML:

/*navigation icon animation*/
var trigger = 'X';

jQuery(document).ready(function () {
$('#toggle-menu').click(function () {
    trigger = 'X';
    $(this).toggleClass('menu-is-active')

});

/* click outside of nav to trigger navigation icon animation*/
$(document).click(function () {


    if (trigger == 'X') {
        $("#toggle-menu").toggleClass();
        trigger = 'ham';
    }



});
$("nav").click(function (e) {
    e.stopPropagation();
    return false;
});

/*----/----navigation icon animation*/

/*toggle menu*/
jQuery("#toggle-menu").click(function () {
    jQuery(".nav").slideToggle();

});
/* click outside of nav to close toggle*/
$(document).click(function () {
    $(".nav").hide();
});
$("#toggle-menu").click(function (e) {
    e.stopPropagation();
    return false;
});
/*----/----toggle menu*/

/*navbar toggle color*/
 $("#toggle-menu").click(function (e) {
  $(".nav").addClass(".whitenavbar");
  $('.nav').addClass('navfade');
    }
    else {
        $('.nav').removeClass(".whitenavbar");
    }
});
/*----/----navbar toggle color*/

/*Jump Scroll*/
$(function () {
    var $window = $(window),
        $document = $(document),
        transitionSupported = typeof document.body.style.transitionProperty === "string",
        scrollTime = 1; // scroll time in seconds

    $(document).on("click", "a[href*=#]:not([href=#])", function (e) {
        var target, avail, scroll, deltaScroll;

        if (location.pathname.replace(/^\//, "") ==     this.pathname.replace(/^\//, "") && location.hostname == this.hostname) {
            target = $(this.hash);
            target = target.length ? target : $("[id=" +     this.hash.slice(1) + "]");

            if (target.length) {
                avail = $document.height() - $window.height();

                if (avail > 0) {
                    scroll = target.offset().top;

                    if (scroll > avail) {
                        scroll = avail;
                    }
                } else {
                    scroll = 0;
                }

                deltaScroll = $window.scrollTop() - scroll;

                if (!deltaScroll) {
                    return; // do nothing
                }
                e.preventDefault();

                if (transitionSupported) {
                    $("html").css({
                        "margin-top": deltaScroll + "px",
                            "transition": scrollTime + "s ease-in-out"
                    }).data("transitioning", scroll);
                } else {

                    $("html, body").stop(true, true)
                        .animate({
                        scrollTop: scroll + "px"
                    }, scrollTime * 1000);

                    return;
                }
            }
        }
    });

    if (transitionSupported) {
        $("html").on("transitionend webkitTransitionEnd msTransitionEnd     oTransitionEnd", function (e) {
            var $this = $(this),
                scroll = $this.data("transitioning");

            if (e.target === e.currentTarget && scroll) {
                $this.removeAttr("style").removeData("transitioning");

                $("html, body").scrollTop(scroll);
            }
        });
    }
});
/*----/----Jump Scroll*/

/*contact let me know toggle*/ 
$("#container").hide();

$("#button").on('click', function() {

    if($("#button").val() == 'thank you')
        return false;

if($("#container").is(":visible"))
return false;

$("#container").toggle();
this.value = this.value == 'send me a message' ? 'contact' : 'send me a     message';
});

 $("#button2").on('click', function(e) {
    $("#container").toggle();
    $("#button").val('thank you');
 });     
/*----/-----contact let me know toggle*/

/*navigation background fade in fade out */
$(window).scroll(function () {
    var dist = $('#panel2').offset().top;
    console.log(dist);
    if ($(window).scrollTop() > dist) {
        $('.header').addClass('bg');
        $('.header').addClass('navfade');
    }
    else {
        $('.header').removeClass('bg');
    }


});


$('.scroll').on('click', function (e) {
    e.preventDefault()

    $('html, body').animate({
        scrollTop: $(this.hash).offset().top
    }, 1500);
});

/*----/-----navigation background fade in fade out */

});
</script>

2 个答案:

答案 0 :(得分:1)

我不确定,但我认为可能之所以没有切换是因为其他声明,因为它看起来就像是在点击你添加这些类时,如果没有点击,你就会删除它们,但如果没有点击它们,你就不会在第一时间添加它们(如果这有意义的话),也许这就是为什么它不会切换。

当我尝试运行JSFiddle时遇到错误(我可能只是点击了错误的东西)但是如果你想让导航栏在点击时变白,你可以改变它CSS,并且因为更改CSS是一个更简单的解决方案,它可能不太可能首先导致切换问题。

$(".nav").css("background", "white");

答案 1 :(得分:1)

在为元素定义的css之后,尝试以下定义你的whitenavbar css。它适用于header。如果要应用于其他元素,则相应地进行更改。如果你更改.nav背景,它将不可见。下一个选项是更改.navigation

/*toggle menu*/

jQuery("#toggle-menu").click(function () {
    jQuery(".nav").slideToggle();
    if($("div.header").hasClass("whitenavbar") == false){
        $("div.header").addClass("whitenavbar");
    }else{
    $("div.header").removeClass("whitenavbar");
    }

});

css注意它是在.header {} css

之后
    /*Nav Bar*/
     .header {
        /*border-bottom: 0.5px solid rgba(0,0,0,.2);*/
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 99999;
        background-color: rgb(184, 184, 184);
        /*background: none;*/
    }
    .whitenavbar {

        background: teal;
    }