简体   繁体   English

菜单栏,它不在顶部,但是一旦向下滑动,菜单栏就会粘在顶部

[英]Menu Bar, which is not at the top, but once you slide down the menu bar sticks to the top

I need to make a menu bar like the one in http://showbic.com/sports/adam-milne-vs-west-indies/ 我需要制作一个菜单栏,例如http://showbic.com/sports/adam-milne-vs-west-indies/中的菜单栏

In this website the menu_bar is not on the top, but when you scroll down the menu bar doesn't go up with the rest of the content, but after touching the top it stays at the top. 在此网站中,menu_bar不在顶部,但是当您向下滚动时,菜单栏不会与其余内容一起显示,但是在触摸顶部后,它将停留在顶部。

I know some JavaScript is used combined with the CSS, but how I don't know, please someone help me. 我知道一些JavaScript与CSS结合使用,但是我不知道如何,请有人帮助我。

Thank You in Advance. 先感谢您。

I would advise trying something with onscroll in Javascript and then keeping the header at the top you can use position:fixed; 我建议尝试使用Java的onscroll进行操作,然后将标题保留在顶部,然后使用position:fixed; in the container's CSS. 在容器的CSS中。 (you might want to play around with the top placement or something else to keep it at the very top and in your preferred spot when not needed at the top) (您可能需要尝试使用顶部位置或其他位置,以将其保持在顶部位置,并且在不需要顶部时将其放置在您的首选位置)

See for example: 参见例如:

<script type="text/javascript">
var fixed = false;

onscroll = function() 
{
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    if (scrollTop > 200) 
    {
        if (!fixed) 
        {
            $('.navbar-wrapper').css({ position: 'fixed', top : 0 });
            fixed = true;
            }
    } 
    else 
    {
        if (fixed)  
        {
            $('.navbar-wrapper').css({ position: 'relative', top : 200 });
              fixed = false;
        }
    }
}
</script>

When looking into the source code, you can view the javascript part that is controlling this bar. 查看源代码时,您可以查看控制此栏的javascript部分。 http://showbic.com/wp-content/plugins/seo-alrp/js/slidebox.js?ver=3.8 . http://showbic.com/wp-content/plugins/seo-alrp/js/slidebox.js?ver=3.8 Instead of : 代替 :

$('#alrp-slidebox').animate({'right':'0px'},300);

Put: 放:

$('#yourContent').animate({'top':'0px'},300);

And for (we suppose that the height of the box is 300px): 对于(我们假设框的高度为300px):

$('#alrp-slidebox').stop(true).animate({'right':'-430px'},100);

Put: 放:

$('#yourContent').stop(true).animate({'top':'-300px'},100);

This can be your css 这可以是你的css

body{
    height:1000px;
}

div{
width:200px;
    height:100px;
    background:red;
    position:relative;
    top:200px;
}

.fixedClass{
position:fixed;
top:0;
}

the jquery code jquery代码

$(window).scroll(function(){
if($(window).scrollTop() > 200){ // position of menu from the top 
$('div').addClass('fixedClass');
}
else{
$('div').removeClass('fixedClass');
}
})

the Html :P Html :P

<div>
</div>

the working fiddle 工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM