简体   繁体   English

如何在Wordpress中使标题粘滞

[英]How can make header sticky in wordpress

This is my the link of website http://www.expresskerala.com . 这是我网站http://www.expresskerala.com的链接。 In this website i need to make header sticky. 在此网站中,我需要使页眉变粘。 Is there any way to make that sticky.I had tried this code. 有没有办法使它变得粘滞。我尝试了这段代码。

.site-header {
    background: #e5e5e5 none repeat scroll 0 0;
    position: fixed;
    width: 100%;
    z-index: 99 !important;
}

But when i use this code, the other content will go down. 但是,当我使用此代码时,其他内容将下降。 Is there any solution for this. 有什么解决办法吗? This should be also responsive. 这也应该有所响应。

Since a position:fixed element is taken out of the document flow, you need to add a top margin equal to the height of the .site-header to the next element. 由于position:fixed元素.site-header文档流中删除,因此需要在下一个元素上添加等于.site-header高度的上.site-header You also need to add top:0 to fix the .site-header to the top of the document. 您还需要添加top:0来将.site-header修复到文档的顶部。

.site-header {
    background: #e5e5e5 none repeat scroll 0 0;
    position: fixed;
    width: 100%;
    z-index: 99 !important;
    top: 0;
}
.site-header + * {
    margin-top: 240px; 
    // you should change this using media queries if the site-header height changes
}

Something like this: 像这样:

@media only screen and (min-width: 1024px) {
    .site-header {
        position: fixed;
        z-index: 1;
    }

    #main > .container {
        padding-top: 250px;
    }
}

you can add these css to get better result 您可以添加这些CSS以获得更好的结果

body {
    padding-top: 240px;
}


.site-header {
    background: #e5e5e5 none repeat scroll 0 0;
    position: fixed;
    width: 100%;
    top: 0;
    z-index: 999;
}

add this additional css in your file 在文件中添加此额外的CSS

.fixed-header {
  position: fixed;
  top:0;
}

than use also this script: 比也使用此脚本:

$(window).scroll(function(){
    if ($(window).scrollTop() >= 140) {
       $('nav').addClass('fixed-header');
    }
    else {
       $('nav').removeClass('fixed-header');
    }
});

/* scrollTop() >= 140
   Should be equal the height of the header
 */

Try this one: 试试这个:

JS JS

$(window).scroll(function(){
    if ($(window).scrollTop() >= 240) {
       $('body').addClass('sticky-header');
    }
    else {
       $('body').removeClass('sticky-header');
    }
});

CSS 的CSS

 body.sticky-header {
        padding-top: 239px;
    }
    body.sticky-header header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 999
    }
    header {
        transition:all ease .3s
    }

Hope this may help you. 希望这对您有帮助。

使用粘性位置W3school或固定使用jQuery

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

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