简体   繁体   English

Bootstrap3 Affix插件在VueJS2中不起作用?

[英]Bootstrap3 Affix plugin doesn't work in VueJS2?

I am trying to add the Bootstrap "affix" plugin to my demo Vue application, but it's not working. 我试图将Bootstrap“ affix”插件添加到我的演示Vue应用程序中,但是它不起作用。 Can anyone help? 有人可以帮忙吗?

According to the docs, I just needed to add the following code to my JavaScript: 根据文档,我只需要在JavaScript中添加以下代码:

$('#results').affix({
  offset: {
  top: 0
 }
});

Live JSFIDDLE here . 在此处直播JSFIDDLE

Unfortunatley, it won't affix to the page on page scroll. 不幸的是,它不会粘贴到页面滚动页面上。 Any ideas? 有任何想法吗?

The problem is that you are executing the affix before the section is rendered. 问题在于您正在渲染该节之前执行词缀。 so $('#results') actually has a length 0 during that phase, it doesn't exist yet in the DOM. 因此$('#results')在该阶段实际上的长度为0,但在DOM中尚不存在。

Your result section is part of your productType section, which is only rendered when selectedOffice !== '' 结果部分是productType部分的一部分,该部分仅在selectedOffice!==''时显示。

So one thing you can do is add a watch to that variable and force the affix after the render... thus you'll need the nextTick 因此,您可以做的一件事是在该变量中添加一个手表,并在渲染之后强制使用词缀...因此,您需要nextTick

watch: {
  selectedOffice(newValue) {
    this.$nextTick(() => {
      $('#results').affix({
        offset: {
          top: 0
        }
      });
    });
  }
},

Look at the modified fiddler 看改良的提琴手

... But another small problem you have is that if the screen is not wide enough and you scroll up your affix wont show up.. that is some css problem you could fix with media queries ...但是另一个小问题是,如果屏幕不够宽并且向上滚动后缀不会显示..那是一些CSS问题,可以通过媒体查询解决

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

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