gpt4 book ai didi

jquery - 粘性标题上的动画

转载 作者:太空宇宙 更新时间:2023-11-04 07:58:57 24 4
gpt4 key购买 nike

我发现这段代码可以让我的网站菜单在滚动时保持在顶部。

有效,但我想添加一个文本动画,类似于此站点:http://somlaweb.com/

我的意思是用动画让菜单的高度变小。我怎样才能得到这个?

$(window).scroll(function(){
if ($(window).scrollTop() >= 300) {
$('nav').addClass('fixed-header');
}
else {
$('nav').removeClass('fixed-header');
}
});

html

<header>
<div class="header-banner">
<a href="/" class="logo"></a>

<h1>Visit Finland</h1>

</div>
<div class="clear"></div>
<nav>
<ul>
<li><a href="/archive">Archive</a></li>
<li><a href="/events">Events</a></li>
<li><a href="/contact">Contact</a></li>
<ul>
</nav>
</header>

CSS

.fixed-header {
position: fixed;
top:0; left:0;
width: 100%;
}

最佳答案

你可以试试这样的:

$(window).scroll(function(){
if ($(window).scrollTop() >= 300) {
$('header').addClass('fixed-header');
}
else {
$('header').removeClass('fixed-header');
}
});
body{
height: 400vh;
font-family: arial;
}

header {
display: flex;
align-items: center;
background: #eee;
box-sizing: border-box;
padding: 10px;
transition: .3s;
position: fixed;
width: 100%;
}

header .header-banner{
flex: 1;
}

header h1{
font-size: 18px;
}

header ul li{
display: inline;
}

header ul li a{
text-decoration: none;
font-size: 12px;
color: #777;
}

header.fixed-header{
padding: 0px 10px;
}

header.fixed-header h1{
font-size: 14px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="header-banner">
<a href="/" class="logo"></a>

<h1>Visit Finland</h1>

</div>
<div class="clear"></div>
<nav>
<ul>
<li><a href="/archive">Archive</a></li>
<li><a href="/events">Events</a></li>
<li><a href="/contact">Contact</a></li>
<ul>
</nav>
</header>

关于jquery - 粘性标题上的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47139190/

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