Intestazione di scorrimento uniforms con posizione fissa

Come creare una scroll scorrevole quando cambio la posizione in fixed. Provo ad aggiungere l’animazione ma non funziona. Meglio usare 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%; } 
  
Text here
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.

Puoi usare @keyframes per aggiungere alcuni effetti di transizione allo scroll.

 $(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); } } 
  
Text here
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.

MODIFICATO in base al commento.

Se cambi la position in modo fixed durante lo scorrimento, genererà un comportamento di salto indesiderato.

La soluzione migliore sarebbe quella di impedire il posizionamento durante lo scorrimento, l’impostazione fixed dopo 40px o dall’inizio è praticamente la stessa, quindi ti suggerisco di rimuovere questa parte sul tuo jQuery e di rendere l’ header fixed dall’inizio:

 //if (scroll >= 40) sticky.addClass('fixed'); //else sticky.removeClass('fixed'); 

Snippet di seguito:

 $(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%; } 
  
Text here
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.

Se stai cercando un modo semplice ed efficace per implementare un’intestazione appiccicosa suggerirei di usare Bootstrap. È facile andare avanti e tutto il lavoro pesante è già stato fatto per te.

Segui la guida rapida

https://v4-alpha.getbootstrap.com/getting-started/introduction/#quick-start

Aggiungi il nome della class “navbar-fixed-top” al tuo nav e questo è tutto. Da lì hai un navigatore app simile con start up semplice.