jQuery 具有固定位置的平滑滚动标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41678751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Smooth scroll header with fixed position
提问by Palaniichuk Dmytro
How to create smooth scroll when I change the position to fixed. I try to add the animation but it does not work. Better use jquery animation();
当我将位置更改为固定时如何创建平滑滚动。我尝试添加动画但它不起作用。最好使用jquery animation();
$(window).scroll(function() {
var sticky = $('.mobile-menu'),
scroll = $(window).scrollTop();
if (scroll >= 40) sticky.addClass('fixed');
else sticky.removeClass('fixed');
});
header {
padding: 20px 40px;
background: gray;
width: 100%;
-webkit-transition: position 10s;
-moz-transition: position 10s;
-ms-transition: position 10s;
-o-transition: position 10s;
transition: position 10s;
}
section {
height: 150vh;
}
.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="mobile-menu">Text here</header>
<section>Sugar plum muffin cookie pastry oat cake icing candy canes chocolate. Gummi bears chupa chups fruitcake dessert jelly. Muffin cookie ice cream soufflé pastry lollipop gingerbread sweet. Unerdwear.com bonbon candy marzipan bonbon gummies chocolate cake
gummi bears powder. Unerdwear.com tart halvah chocolate cake dragée liquorice. Sugar plum chocolate bar pastry liquorice dragée jelly powder. Jelly tootsie roll applicake caramels. Marzipan candy tootsie roll donut. Gummies ice cream macaroon applicake.</section>
回答by Yonas Hailu
You can use @keyframes
to add some transition effects to the scroll.
您可以使用@keyframes
向滚动添加一些过渡效果。
$(window).scroll(function() {
var sticky = $('.mobile-menu'),
scroll = $(window).scrollTop();
if (scroll >= 40) {
sticky.addClass('fixed'); }
else {
sticky.removeClass('fixed');
}
});
header {
padding: 20px 40px;
background: gray;
width: 100%;
-webkit-transition: all 0.5s ease;
-moz-transition: position 10s;
-ms-transition: position 10s;
-o-transition: position 10s;
transition: all 0.5s ease;
}
section {
height: 150vh;
}
.fixed {
position: fixed;
top: 0;
left: 0;
animation: smoothScroll 1s forwards;
}
@keyframes smoothScroll {
0% {
transform: translateY(-40px);
}
100% {
transform: translateY(0px);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="mobile-menu">Text here</header>
<section>Sugar plum muffin cookie pastry oat cake icing candy canes chocolate. Gummi bears chupa chups fruitcake dessert jelly. Muffin cookie ice cream soufflé pastry lollipop gingerbread sweet. Unerdwear.com bonbon candy marzipan bonbon gummies chocolate cake
gummi bears powder. Unerdwear.com tart halvah chocolate cake dragée liquorice. Sugar plum chocolate bar pastry liquorice dragée jelly powder. Jelly tootsie roll applicake caramels. Marzipan candy tootsie roll donut. Gummies ice cream macaroon applicake.</section>
回答by Syden
EDITED based on comment.
根据评论编辑。
If you change position
to fixed
while scrolling, it will generate undesired jump behavior.
如果在滚动时更改position
为fixed
,则会产生不希望的跳转行为。
Your best bet would be to prevent positioning while scrolling, setting fixed
after 40px
or from the start is pretty much the same, so I'd suggest you remove this part on your jQuery, and make your header
fixed
from the start:
你最好的办法是在滚动时防止定位,在开始fixed
之后40px
或从开始设置几乎相同,所以我建议你在你的 jQuery 上删除这部分,并header
fixed
从头开始:
//if (scroll >= 40) sticky.addClass('fixed');
//else sticky.removeClass('fixed');
Snippet below:
下面的片段:
$(window).scroll(function() {
var sticky = $('.mobile-menu'),
scroll = $(window).scrollTop();
});
body {
position: relative;
}
header {
padding: 20px 40px;
background: gray;
width: 100%;
-webkit-transition: position 10s;
-moz-transition: position 10s;
-ms-transition: position 10s;
-o-transition: position 10s;
transition: position 10s;
}
section {
height: 150vh;
position: relative;
top: 60px;
}
.fixed {
position: fixed;
z-index: 1;
top: 0;
left: 0;
right: 0;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="mobile-menu fixed">Text here</header>
<section>Sugar plum muffin cookie pastry oat cake icing candy canes chocolate. Gummi bears chupa chups fruitcake dessert jelly. Muffin cookie ice cream soufflé pastry lollipop gingerbread sweet. Unerdwear.com bonbon candy marzipan bonbon gummies chocolate cake
gummi bears powder. Unerdwear.com tart halvah chocolate cake dragée liquorice. Sugar plum chocolate bar pastry liquorice dragée jelly powder. Jelly tootsie roll applicake caramels. Marzipan candy tootsie roll donut. Gummies ice cream macaroon applicake.</section>