When the page is reloaded, the computed property for window.scrollY will always be 0, but it functions properly with

<div id="component-navbar" :class="hasBackground">
computed: {
  hasBackground() {
    if (window.scrollY > 0) {
      return 'has-background'
    }
  }
}

I am facing an issue with my sticky nav bar where I want to apply a background when the page scroll is greater than 0. The challenge lies in the fact that hasBackground.

  1. does not update as the user scrolls
  2. shows window.scrollY as zero on page reload even after scrolling
  3. only responds to changes when a file modification triggers hot reload.

How can I resolve this problem?

Answer №1

According to @obermillerk:

The window object does not work properly with Vue.js.

Here are two possible solutions:

  1. Retrieve window.scrollY when your component is first initialized.
  2. Add
    window.addEventListener('scroll', ..., { passive: true })
    .

Retrieve window.scrollY when your component is initialized

Vue.component('component-navbar', {
  computed: {
    hasBackground () {
      console.log('window.scrollY', window.scrollY)
      if (window.scrollY > 0) {
        return 'has-background'
      }
    }
  },
  template: '<nav :class="hasBackground"></nav>'
})

const app = new Vue({
  el: '#app'
})
#app {
  min-height: 110vh;
}
nav {
  min-height: 50vh;
}
nav.has-background {
  background: linear-gradient(red, orange);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<article id="app">
  <component-navbar></component-navbar>
</article>

Add
window.addEventListener('scroll', ..., { passive: true })

Vue.component('component-navbar', {
  data () {
    return {
      isScrolled: false
    }
  },

  computed: {
    hasBackground () {
      if (this.isScrolled) {
        return 'has-background'
      }
    }
  },

  template: '<nav :class="hasBackground"></nav>',

  mounted () {
    window.addEventListener('scroll', this.setIsScrolled, { passive: true })
    this.setIsScrolled()
  },

  destroyed () {
    window.removeEventListener('scroll', this.setIsScrolled, { passive: true })
  },

  methods: {
    setIsScrolled () {
      this.isScrolled = window.scrollY > 0
    }
  },
})

const app = new Vue({
  el: '#app'
})
#app {
  min-height: 110vh;
}
nav {
  min-height: 50vh;
}
nav.has-background {
  background-image: linear-gradient(red, orange);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<article id="app">
  <component-navbar></component-navbar>
</article>

Answer №2

Observing the window object in Vue can be challenging. For more information on Vue's reactivity, check out this link: https://v2.vuejs.org/v2/guide/reactivity.html

I initially considered using a proxy for the window object within Vue data, but it seems that this approach creates a new object with getters and setters for existing properties. As a result, any new data created this way would not react to changes in the original window object. Unfortunately, the best option is to listen for events instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can the Vue input tag element be properly populated with tags?

Currently, I am utilizing the vue element <input-tag> and I am in the process of creating an edit page for a specific entity that contains tags. The goal is to populate the <input-tag> field based on the existing values within the entity. As of ...

Child component in Angular fails to detect input changes

Hey there! I'm currently utilizing parent-child communication in my Angular project. In the parent component, I have an array that represents graph data. If you'd like to check out a demo of what I'm working on, feel free to click here. The ...

How can I use `app.js` in Zendesk to connect to an external API using the complete URL

As someone new to developing Zendesk apps, I've been following the step-by-step guide available here. To Summarize I'm facing an issue with passing external API URLs to the AJAX call syntax within Zendesk's app.js file. You can find my sim ...

Retrieve the image by its unique identifier while viewing a preview of the image before it is uploaded

Below is the script I am using to preview an image before it is uploaded. The HTML structure looks like this: <div> <img id="image" src="#"> </div> <input type="file" accept="image/gif, image/jpeg, image/png" onchange="readURL(th ...

Enhance the performance of page loading and implement a consistent spinner feature to ensure smooth transitions for users in Next.js version 13

I am currently working on a project using Next.js 13, and I am encountering issues with slow loading times and an unstable spinner when navigating between pages. Specifically, when transitioning from the home page to the /example page, the experience is n ...

When working with Vue JS, which is more performant and preferable to use: making direct changes to state in Pinia or using $patch()?

Hey guys, I've been working with Pinia and Vuejs 3, and I'm curious about the performance impact of using $patch(). Can anyone shed some light on this? What is considered the best practice when it comes to this situation? For instance, take a l ...

What about dynamically generating content with WebUserControl?

I have created a custom WebUserControl with three HTML controls, two buttons, and one input text. Each control has a static ID, which I believe is not incorrect. The control events are set up using JavaScript. After delivering the control to the client, th ...

Tips for refreshing many dashboard elements with a single click using Vue.js

Struggling to find online help that fits my specific situation, I need guidance to know if I'm on the right path with my Vue app. Each row on the right side of the app is a component, while the three datasets on the left contain values for the fields ...

I'm experiencing a flickering issue with my carousel when it reaches over 10,000 pixels during animation. Could this be related to a jQuery problem

After my carousel moves past 10000 pixels, it starts flickering through multiple elements. I'm utilizing jCarousel Lite: Could this be a jQuery-related issue? My initial assumption is that it may be specific to jCarousel Lite, but there doesn't ...

Exploring ways to pass props in functional components in React Native

I am currently exploring how to create an API in React Native with TypeScript without using the class extends component. However, I am struggling to figure out how to access and send props from one const view to another function: App.tsx import React, {us ...

Ways to resolve the issue of the experimental syntax 'jsx' not being enabled

I encountered an issue while trying to implement react-fancybox and received an error. https://i.sstatic.net/vgFha.png To resolve the error, I executed the following commands: npm install --save-dev @babel/preset-react, npm install --save-dev @babel/plugi ...

What causes the strings in an array to be split into individual characters when using the jquery each() function?

After successfully loading jquery, I have initialized an array (object) with various strings: window.stack = [ "header", "nav", ".content", "footer" ]; My intention was to loop through this array using jquery's each() function to retrieve ea ...

Steps to retrieve the latest value of a specific cell within the Material UI Data Grid

After updating the cell within the data grid, I encountered an issue where I could retrieve the ID and field using the prop selectedCellParams, but retrieving the modified value was proving to be challenging. In order to successfully execute the PUT reque ...

Nested AJAX call yields undefined value

In my Test.vue component, I have a method that is imported into my main.js. I can call its methods like this: this.$refs.test.testMethod(). There is another method in Test.vue called ajaxMethod(), which is defined as follows: function ajaxMethod(){ t ...

When the jQuery keyup event is triggered, the "function" will be incremented to 0

There are three input fields to search a JSON tree. When all three fields are filled correctly, the data from the next level of the JSON tree is retrieved. A number is incremented through the keyup event to access the next data of the JSON tree. However, ...

Can the lexical scope of a function in JS be maintained while overriding it?

When faced with the task of modifying a function provided by a third-party module, I considered simply copying the function and making changes to it. However, the issue lies in the fact that this function relies on other functions and variables within its ...

How to update MongoDB documents with referenced objects using Mongoose?

Apologies for any language barriers. I am using node.js + express.js + mongoose.js Here is my schema in mongoose for groups: var groupSchema = new mongoose.Schema({ name: String, users: [{type: mongoose.Schema.ObjectId, ref: 'User'}] ...

Are there any specific events in PrimeNg's p-calendar that trigger when the time is changed?

In my project, I utilized PrimeNg v5.2.7 for the From Date and To Date fields. I implemented minDate validation on the To Date field. However, I encountered a scenario where if I select 30th Jan 2021 as the From Date and only adjust the time in the To Da ...

What is the best way to link CSS files from libraries that are downloaded using npm?

For instance, let's say I installed a package: npm install --save package and it gets saved in ./node_modules/package/ Inside that folder, there might be a directory named styles and within that directory, you could find style.css. How can I link ...

Receiving unique socket IDs for two different socket.on events

Currently, I am in the process of developing a socket-based application using React and Node/Express. As part of this project, there is an event listener set up for when a user connects with io.on('connection'). Upon connection, the user is added ...