Fixed header whilst scrolling

2019-06-12 21:09发布

I am using a jquery scripts to cause my header to remain fixed to the top whilst scrolling. But at the moment, it's animated and that's quite a distraction. Is there a way I can modify the script so that it doesn't animate?

JQuery;

<script type="text/javascript">
$().ready(function() {

        var $scrollingDiv = $("#header");

        $(window).scroll(function(){            
            $scrollingDiv.stop().animate({"marginTop": ($(window).scrollTop() + 0) + "px"}, "fixed" );          
        });
    });


</script>

Html;

<div id="header">Content</div>

2条回答
狗以群分
2楼-- · 2019-06-12 21:41

Add to the style of the header position:fixed;.

查看更多
贼婆χ
3楼-- · 2019-06-12 21:45

Take a look at the link below as a simple solution to your problem.

http://www.cssplay.co.uk/layouts/basics2.html

EDIT: Also the solution from the above link should be cross-browser compatible

查看更多
登录 后发表回答