ManuManfred
ManuManfred

Reputation: 113

Material design App bar with CSS

I need to style a Navigation bar like the App bar on material design apps with CSS. For example Google-Keep app or Playstore app. In this Navigation bar are a title and a simple menu.

What i have is a content div and two fixed div on the top. One of them is always fixed and the other one has a negative margin-top on scroll. But, if i scroll down, there is a short "blink"-effect and i don't know why.

$(document).ready( function() {
		
    var lastScrollY = 0;
    var lastScrollDownY = 0;
    var lastScrollUpY = 0;
    
    $(window).scroll(function() {
    		var scrollY = $(this).scrollTop();
        
        // detect scroll down / up
        if(scrollY < lastScrollY)
        {
        	var diffScroll = lastScrollDownY - scrollY;
            if(diffScroll > 105)
            {
                diffScroll = 105;
            }
            var navbarY = -105+diffScroll;
            $("#navbar-bottom").css({
                "margin-top":navbarY+"px"
            });
            lastScrollUpY = scrollY;
        } else {
        		var diffScroll2 = lastScrollDownY - lastScrollUpY;
            if(diffScroll2 > 105)
            {
                diffScroll2 = 105;
            }
            $("#navbar-bottom").css({
                "margin-top":"-"+diffScroll2+"px"
            });
            lastScrollDownY = scrollY;
        }
        
        lastScrollY = scrollY;
    });
    
});
body {
  height:100%;
}
#header {
  width:100%;
}
#navbar-top {
  width:100%;
  position:fixed;
  top:0;
  z-index:1000;
  height:35px;
  background-color:#1976D2;
}
#navbar-bottom {
  width: 100%;
	position:fixed;
	top:0;
	z-index: 999;
  height:105px;
  background-color: #2196F3;
}
#navbar-bottom .navbar {
  padding-top: 35px;
}
#wrapper {
  padding-top: 105px;
  position: relative;
  height:2000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <div id="header">
        <div id="navbar-top">
            ActionBar (always fixed)
        </div>
        <div id="navbar-bottom">
            <div class="navbar">
                Menu links here
            </div>
        </div>
    </div>
    <div id="wrapper">
        <div id="content">
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem 
        </div>
    </div>
</body>

Thanks for any help!

Upvotes: 4

Views: 1202

Answers (2)

user6633393
user6633393

Reputation:

Add a smooth animation for the submenu:

#navbar-bottom {
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 999;
    height: 105px;
    background-color: #2196F3;
    transition: all 500ms;
}

$(document).ready(function() {

  var lastScrollY = 0;
  var lastScrollDownY = 0;
  var lastScrollUpY = 0;

  $(window).scroll(function() {
    var scrollY = $(this).scrollTop();

    // detect scroll down / up
    if (scrollY < lastScrollY) {
      var diffScroll = lastScrollDownY - scrollY;
      if (diffScroll > 105) {
        diffScroll = 105;
      }
      var navbarY = -105 + diffScroll;
      $("#navbar-bottom").css({
        "margin-top": navbarY + "px"
      });
      lastScrollUpY = scrollY;
    } else {
      var diffScroll2 = lastScrollDownY - lastScrollUpY;
      if (diffScroll2 > 105) {
        diffScroll2 = 105;
      }
      $("#navbar-bottom").css({
        "margin-top": "-" + diffScroll2 + "px"
      });
      lastScrollDownY = scrollY;
    }

    lastScrollY = scrollY;
  });

});
body {
  height: 100%;
}
#header {
  width: 100%;
}
#navbar-top {
  width: 100%;
  position: fixed;
  top: 0;
  z-index: 1000;
  height: 35px;
  background-color: #1976D2;
}
#navbar-bottom {
  width: 100%;
  position: fixed;
  top: 0;
  z-index: 999;
  height: 105px;
  background-color: #2196F3;
  transition: all 500ms;
}
#navbar-bottom .navbar {
  padding-top: 35px;
}
#wrapper {
  padding-top: 105px;
  position: relative;
  height: 2000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div id="header">
    <div id="navbar-top">
      ActionBar (always fixed)
    </div>
    <div id="navbar-bottom">
      <div class="navbar">
        Menu links here
      </div>
    </div>
  </div>
  <div id="wrapper">
    <div id="content">
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
      sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
      Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem
    </div>
  </div>
</body>

Upvotes: 0

kind user
kind user

Reputation: 41893

Here you go. Added transform: translateY(), which is much more dynamic then just changing the margin-top property. Check it out and tell me if it does satisfy you, if not - let me know and I will try to improve it.

https://jsfiddle.net/pfj9j8md/

var lastScrollY = 0;
	var lastScrollDownY = 0;
	var lastScrollUpY = 0;

	$(window).scroll(function() {
	  var scrollY = $(this).scrollTop();

	  // detect scroll down / up
	  if (scrollY < lastScrollY) {
	    var diffScroll = lastScrollDownY - scrollY;
	    if (diffScroll > 105) {
	      diffScroll = 105;
	    }
	    var navbarY = -105 + diffScroll;
	    $("#navbar-bottom").css({
	      'transform': 'translateY(0)'
	    });
	    lastScrollUpY = scrollY;
	  } else {
	    var diffScroll2 = lastScrollDownY - lastScrollUpY;
	    if (diffScroll2 > 105) {
	      diffScroll2 = 105;
	    }
	    $("#navbar-bottom").css({
	      'transform': 'translateY(-200px)'
	    });
	    lastScrollDownY = scrollY;
	  }

	  lastScrollY = scrollY;
	});
body {
  height: 100%;
}
#header {
  width: 100%;
}
#navbar-top {
  width: 100%;
  position: fixed;
  top: 0;
  z-index: 1000;
  height: 35px;
  background-color: #1976D2;
}
#navbar-bottom {
  width: 100%;
  position: fixed;
  top: 0;
  z-index: 999;
  height: 105px;
  background-color: #2196F3;
  transition: all .5s ease;
}
#navbar-bottom .navbar {
  padding-top: 35px;
}
#wrapper {
  padding-top: 105px;
  position: relative;
  height: 2000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div id="header">
    <div id="navbar-top">
      ActionBar (always fixed)
    </div>
    <div id="navbar-bottom">
      <div class="navbar">
        Menu links here
      </div>
    </div>
  </div>
  <div id="wrapper">
    <div id="content">
      Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
      sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
      Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem
    </div>
  </div>
</body>

Upvotes: 1

Related Questions