gpt4 book ai didi

javascript - 带有 CSS 的 Material 设计应用栏

转载 作者:搜寻专家 更新时间:2023-10-31 08:52:04 24 4
gpt4 key购买 nike

我需要使用 CSS 为 Material Design 应用程序中的应用栏设计导航栏样式。例如 Google-Keep 应用或 Playstore 应用。在这个导航栏中有一个标题和一个简单的菜单。

我有一个内容 div 和顶部的两个固定 div。其中一个始终是固定的,另一个在滚动时具有负边距顶部。但是,如果我向下滚动,会出现短暂的“闪烁”效果,我不知道为什么。

$(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>

感谢您的帮助!

最佳答案

给你。添加了 transform: translateY(),它比仅更改 margin-top 属性更具动态性。检查一下并告诉我它是否让您满意,如果不满意 - 让我知道,我会努力改进它。

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>

关于javascript - 带有 CSS 的 Material 设计应用栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044225/

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