简体   繁体   English

发生滚动后,如何固定div(菜单)的位置?

[英]How do I make a div (menu) fixed positioned after some scrolling has happened?

I'm making a theme for a website and I was given permission to play with the HTML files. 我正在为网站制作主题,并获得了播放HTML文件的权限。 The thing is I'm just familiarised with CSS and HTML, but I'm a complete noob when it comes to Javascript, JQuery, etc. and, sadly, it seems I need those to add some features I want. 事情是,我刚刚熟悉CSS和HTML,但是当涉及Javascript,JQuery等时,我是一个完全菜鸟,可悲的是,似乎我需要那些来添加我想要的功能。

I'd like a menu that works like this one: https://www.planetside2.com/news 我想要一个像这样的菜单: https : //www.planetside2.com/news

The HTML is basically this: HTML基本上是这样的:

<div id="wrap">
 <div id="page-header">
  <div class="headerbar">
 </div>
</div>

And plenty stuff inside, but I hope the containers are enough 里面有很多东西,但我希望这些容器足够

This is the CSS: 这是CSS:

#wrap {
    margin: 0 auto;
    width: 1024px;
}

.headerbar {
    background: url("{T_THEME_PATH}/images/headerbarbg.png") no-repeat ;
    background-position: center;
    width: 1054px;
    margin:0 -15px;
    margin-top:3px;
    height: 120px;
    position: relative;
    padding-top: 70px;
}

I've checked out some other solutions, but, as I said, I'm a complete noob at scripting and when I try to apply such scripts to the theme, they don't work, most likely because there are some things that I should change that I don't know (except classes and IDs, of course). 我已经检查了其他一些解决方案,但是,正如我说的那样,我是脚本编写的完全菜鸟,当我尝试将此类脚本应用于主题时,它们将无法工作,很可能是因为我有些事情应该更改我不知道的更改(当然,除了类和ID外)。

I hope someone can help me. 我希望有一个人可以帮助我。

First of all you need to detect scrolling and window resize 首先,您需要检测滚动和窗口大小调整

$(document).ready(function(){

    $(window).on("scroll resize", function(e){

        var elem = $(".headerbar");

        // check if your header is visible by subtracting
        // the top offset of your div from the scrolltop distance
        if ((elem.offset().top - $(window).scrollTop()) <= 0 && elem.css("position") !== "fixed") {
            console.log("not visible");
            elem.css({
                position:"fixed",
                "z-index":"9999",
                top:"0px"
            });
        // check if your header height is greater or equal to the scrolltop distance
        } else if (elem.height() >= $(window).scrollTop()) {
            console.log("visible");
            elem.css({
                position:"relative"
            });
        }

    });

})

And here is a simple demo: JSFIDDLE 这是一个简单的演示: JSFIDDLE

Some thing i have done previously in fiddle just check this one out will help you to have a scratch. 我以前在小提琴中做过的一些事情,只是检查一下就可以了,这将有助于您抓痒。

$(document).ready(function () {
    var top_fixed;
    if ($('#header-con').length > 0)
    {
        top_fixed = $('#header-con').offset().top;
    }
    $(window).scroll(function () {
        if ($('#header-con').length > 0)
        {
            $('#header-con').toggleClass('fixed', $(window).scrollTop() > top_fixed);

        }
    });
});

DEMO 演示

This short jquery code is what you need. 这个简短的jquery代码就是您所需要的。 Tested and was working in a website i had develop lately. 经过测试,并正在我最近开发的网站中工作。

$(window).scroll(function() {
    if($(window).scrollTop() != 0) {
        $('#fixed-menu-top').css('position', 'fixed').fadeIn();
    } else {
        $('#fixed-menu-top').css('position', 'absolute').fadeOut();
    }
});

Just edit the ids to yours ex #fixed-menu-top to #wrap or any ids in ypur mark up 只需将ids修改到您的#fixed-menu-top即可#wrap#wrap任何ids标记

See website 查看网站

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

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