Smooth scroll header with fixed position

前端 未结 2 502
名媛妹妹
名媛妹妹 2021-02-06 07:04

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();

2条回答
  •  难免孤独
    2021-02-06 07:38

    EDITED based on comment.

    If you change position to fixed while scrolling, it will generate undesired jump behavior.

    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:

    //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%;
    }
    
    
    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.

提交回复
热议问题