繁体   English   中英

如果滚动,更改 div 的固定位置

[英]Change position fixed of div if scroll

我试图在 vuejs 上滚动后修复一个 div; 我的代码是这样的:

...
async created() {
    window.addEventListener('scroll', this.calendarScroll);
}
methods: {
    calendarScroll() {
    console.log('Scroll');
    const dateHeader = document.querySelector('.dates-header');
    if (dateHeader) {
      if (window.scrollTop() >= 200) {
        dateHeader.style.top = 0;
        dateHeader.style.position = 'fixed';
      }
    }
  },
}

错误是: Calendar.vue?404a:681 Uncaught TypeError: window.scrollTop is not a function

您可以在挂载之前将scrollTo添加到 Element 原型:

Element.prototype.scrollTo = () => {} 

添加此代码后创建的所有元素都将具有scrollTo方法。 有关更多详细信息,您可以查看: https ://github.com/vuejs/vue-test-utils/issues/319#issuecomment-354667621

你可以只用jquery scroll

$(window).scroll(function() {
   this.calendarScroll();
});

暂无
暂无

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

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