简体   繁体   English

词缀在 Bootstrap 4 (alpha) 中不起作用

[英]Affix is not working in Bootstrap 4 (alpha)

According to Bootstrap 3 docs I have added following attributes to a navbar:根据 Bootstrap 3 文档,我向导航栏添加了以下属性:

<nav class="navbar no-margin-bottom" data-spy="affix" data-offset-top="90" >
...
</nav>

After scrolling down the page Bootstrap 4 is not adding class to navbar which is affix.向下滚动页面后,Bootstrap 4 没有向作为词缀的导航栏添加类。 Can anyone tell me how to solve this problem?谁能告诉我如何解决这个问题? Bootstrap.js and jQuery.js are working. Bootstrap.js 和 jQuery.js 正在工作。

Although the affix is removed from Bootstrap in version 4. However, you can achieve your goal through this jQuery Code:尽管在版本 4 中从 Bootstrap 中删除了词缀。但是,您可以通过以下 jQuery 代码实现您的目标:

$(window).on('scroll', function(event) {
    var scrollValue = $(window).scrollTop();
    if (scrollValue == settings.scrollTopPx || scrollValue > 70) {
         $('.navbar').addClass('fixed-top');
    } 
});

Update Bootstrap 4更新引导程序 4

The docs recommend the sticky polyfill, and the recommended ScrollPos-Styler doesn't really help with scroll position (you can easily define an offset).文档推荐粘性 polyfill,推荐的 ScrollPos-Styler 对滚动位置没有真正帮助(您可以轻松定义偏移量)。

I think it's easier to use jQuery to watch the window scroll and change the CSS accordingly to fixed...我认为使用 jQuery 观看窗口滚动并相应地将 CSS 更改为固定更容易...

  var toggleAffix = function(affixElement, wrapper, scrollElement) {

    var height = affixElement.outerHeight(),
        top = wrapper.offset().top;

    if (scrollElement.scrollTop() >= top){
        wrapper.height(height);
        affixElement.addClass("affix");
    }
    else {
        affixElement.removeClass("affix");
        wrapper.height('auto');
    }

  };


  $('[data-toggle="affix"]').each(function() {
    var ele = $(this),
        wrapper = $('<div></div>');

    ele.before(wrapper);
    $(window).on('scroll resize', function() {
        toggleAffix(ele, wrapper, $(this));
    });

    // init
    toggleAffix(ele, wrapper, $(window));
  });

Bootstrap 4 affix (sticky navbar) Bootstrap 4 词缀(粘性导航栏)

EDIT: Another solution is to use this port of the 3.x Affix plugin as a replacement in Bootstrap 4..编辑:另一个解决方案是使用3.x Affix 插件的这个端口作为 Bootstrap 4 中的替代品。

http://www.codeply.com/go/HmY7DLHLkI http://www.codeply.com/go/HmY7DLHLkI


Related: Animate/Shrink NavBar on scroll using Bootstrap 4相关: 使用 Bootstrap 4 滚动动画/缩小导航栏

From the bootstrap v4 documentation:来自 bootstrap v4 文档:

Dropped the Affix jQuery plugin.删除了 Affix jQuery 插件。 We recommend using a position: sticky polyfill instead.我们建议使用position: sticky polyfill 代替。 See the HTML5 Please entry for details and specific polyfill recommendations.有关详细信息和特定的 polyfill 建议,请参阅 HTML5 Please 条目

If you were using Affix to apply additional, non- position styles, the polyfills might not support your use case.如果您使用 Affix 应用其他非position样式,则 polyfill 可能不支持您的用例。 One option for such uses is the third-party ScrollPos-Styler library.此类用途的一种选择是第三方ScrollPos-Styler库。

To build on Anwar Hussain's answer.以安瓦尔侯赛因的回答为基础。 I found success with this:我发现成功:

$(window).on('scroll', function (event) {
    var scrollValue = $(window).scrollTop();
    if (scrollValue > 120) {
        $('.navbar').addClass('affix');
    } else{
        $('.navbar').removeClass('affix');
    }
});

This will apply the class to the navbar when scrolling down, but will also remove the class when scrolling back up.这将在向下滚动时将该类应用于导航栏,但在向上滚动时也会删除该类。 Previously, when scrolling back up, the applied class would stay applied to the navbar.以前,当向上滚动时,应用的类将保持应用于导航栏。

As per Vucko's quote within the Mirgation docs, ScrollPos-Styler library suited me quite well.根据 Vucko 在 Mirgation 文档中的引用, ScrollPos-Styler库非常适合我。

  1. Include the .js ScrollPos-Styler ( scrollPosStyler.js ) file to your page..js ScrollPos-Styler ( scrollPosStyler.js ) 文件包含到您的页面中。
  2. Find the relevant <div> you wish to make 'sticky'找到您希望“粘性”的相关<div>

<nav class="navbar navbar-toggleable-md">

  1. Apply sps sps--abv class应用sps sps--abv

<nav class="navbar navbar-toggleable-md sps sps--abv">

  1. Add .css styles you wish to have triggered once the element has become sticky (As per the demo page .添加您希望在元素变得粘性后触发的.css样式(根据演示页面.

/** 
 * 'Shared Styles' 
**/
.sps {

}

/** 
 * 'Sticky Above' 
 *
 * Styles you wish to apply
 * Once stick has not yet been applied
**/
.sps--abv {
   padding: 10px
}

/** 
 * 'Sticky Below' 
 *
 * Styles you wish to apply
*  Once stick has been applied
**/
.sps--blw {
   padding: 2px
}
$(window).on('scroll', function (event) {
    var scrollValue = $(window).scrollTop();
    var offset = $('[data-spy="affix"]').attr('data-offset-top');
    if (scrollValue > offset) {
        $('[data-spy="affix"]').addClass('affix-top');
        var width = $('[data-spy="affix"]').parent().width();
        $('.affix-top').css('width', width);
    } else{
        $('[data-spy="affix"]').removeClass('affix-top');
    }
}); 

With bootstrap 4 there is special class for that. Bootstrap 4 有专门的类。 Try removing the data-spy="affix" data-offset-top="90" attributes, and just add 'sticky-top'.尝试删除data-spy="affix" data-offset-top="90"属性,然后添加 'sticky-top'。

documentation 文件

After trying every solution I could find (and being aware that affix was removed from bootstrap 4), the only way I could get the desired sticky results I needed was to use sticky-kit .在尝试了我能找到的所有解决方案(并且知道从引导程序 4 中删除了词缀)之后,我可以获得所需的粘性结果的唯一方法是使用sticky-kit

This is s really simple jQuery plugin that was easy to set-up.这是一个非常简单的 jQuery 插件,易于设置。

Just include the code in your project and then assign the element you want to stick with只需在您的项目中包含代码,然后分配您要坚持使用的元素

$("#sidebar").stick_in_parent();

For the users who are looking for an answer in pure Javascript, this is how you can do it by using pure Javascript:对于正在寻找纯 Javascript 答案的用户,这是使用纯 Javascript 的方法:

window.onscroll = (e) => {
    let scrollValue = window.scrollY;
    if (scrollValue > 120) {
        document.querySelector('.navbar').classList.add('affix');
    } else{
        document.querySelector('.navbar').classList.remove('affix');
    }
}

simple use class fixed-top in bootstrap 4 and use addClass and removeClass简单地在引导程序 4 中使用类固定顶部并使用 addClass 和 removeClass

$(window).on('scroll', function(event) {
    var scrollValue = $(window).scrollTop();
    if ( scrollValue > 70) {
         $('.navbar-sticky').addClass('fixed-top');
    }else{
       $('.navbar-sticky').removeClass('fixed-top');
    }
});

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

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