Home > OS >  Change position fixed of div if scroll
Change position fixed of div if scroll

Time:07-08

I tried to fix a div after scroll on vuejs; My code like this :

...
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';
      }
    }
  },
}

the error is : Calendar.vue?404a:681 Uncaught TypeError: window.scrollTop is not a function

CodePudding user response:

You can add scrollTo to the Element prototype before you mount:

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

All Elements created after you add this code will have the scrollTo method. For more detail, you can check this: https://github.com/vuejs/vue-test-utils/issues/319#issuecomment-354667621

CodePudding user response:

You can just jquery scroll

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