Is it possible to use Vue computed or watch to determine the scrollHeight of the page body?

I'm attempting to utilize computed or watch to identify changes in the body's scrollHeight, but for some reason it is not functioning properly.

Below is my code snippet:

computed: {
    bodyScrollHeight() {
        return document.body.scrollHeight;
    }
},

watch:{
    bodyScrollHeight: function(newValue) {
        this.watchScrollHeight = newValue;
        this.myFunction(newValue);
    }
},

Link to CodePen Demo: https://codepen.io/chhoher/pen/wvMoLOg

Answer №1

If you want a computed property to be reactive while returning document.body.scrollHeight, simply returning it won't achieve that. You need to find another way to listen for changes and notify Vue accordingly.

One possible method is to continuously check the scrollHeight value by polling it. Here's an example implementation:

new Vue({
  data: () => ({
    scrollHeight: 0,
    interval: null,
  }),

  mounted() {
    this.interval = setInterval(() => {
      this.scrollHeight = document.body.scrollHeight;
    }, 100);
  },

  destroyed() {
    clearInterval(this.interval);
  },

  watch: {
    scrollHeight() {
      // This function will be triggered when the poll detects a change in value
    }
  },

  computed: {
    doubleScrollHeight() {
      // You can use this computed property which will reactively update with scrollHeight
      return this.scrollHeight * 2;
    }
  }
})

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 we prevent and remove extra whitespace characters such as new lines and line feeds in responseText?

I am troubleshooting an ajax script that is calling a php file. The php file is supposed to echo either "yes" or "no", which I intend to use for logical comparisons. However, when trying to compare the responseText in javascript to check if it matches "y ...

Verification of radio selections

I have successfully implemented validation for the radio button. Now, I would like to add an alert box that asks "Are you sure you want to pick 'x'?" before proceeding with the code. If the user clicks cancel, they should return to the webpage. B ...

Adjust the property to be optional or required depending on the condition using a generic type

const controlConfig = >T extends 'input' | 'button'(config: Config<T>): Config<T> => config; interface Config<TYPE extends 'input' | 'button'> { type: TYPE; label: string; ...

PHP: Link to logo in different folder by including 'nav.php'

I am facing an issue with my nav.php file: <div> <!-- there's a lot of code here so I want to write it once and include it in all pages within the main folder as well as subfolders <img src="logo.png"> </div> The structur ...

Encase all jQuery functionalities within a custom class

I am looking to create a JavaScript class that encapsulates jQuery's DOM functions, but I want these functions to only interact with a single property of that class. class Foo { constructor() { this.$wrapper = $('<div>wrapper</div ...

What is the technique for submitting a form's data into a table upon clicking a button with Vuex?

Having trouble transferring data from Form.vue to Table.vue upon form submission. Realized there might be a mistake in the code and seeking assistance. With limited experience in vue, please help guide me in the right direction. Interested in feedback a ...

What is the proper procedure for configuring Babel and executing "npm run dev" in the terminal without encountering the "ERROR in ./src/js/index.js" message?

My goal is to include the babel/polyfill with the index.js files using webpack. After completing the setup, I tried running "npm run dev" in the command prompt but encountered an error. The initial line of the error message said "ERROR in ./src/js/index.js ...

Using npm link with Webpack results in eslint errors

I have a multi-package project setup, where I have one JavaScript package that depends on a TypeScript library. Initially, I was using Sinopia and had to reinstall the TypeScript library every time I made changes to it. Then I discovered npm link and thoug ...

Encountering a 404 error while attempting to access a static resource using (fs) in nodeJS?

I'm facing an issue in generating a URL using nodeJS that directs to a static resource, specifically a JS file. I keep receiving a 404 error, indicating the resource is Not Found. Initially, I launch the node server by executing node index.js The co ...

The reference Ref.current does not exist

I'm facing a challenge while trying to troubleshoot an issue, as I am unable to comprehend the exact error. Upon investigation, I discovered that a null in a ref can arise due to the object not being mounted. Despite placing the function in useEffect, ...

I'm curious why the manual ES6 class mock in Jest isn't working as expected and I want to figure

I seem to be facing issues with implementing Jest manual mocks (specifically the one located in a separate __mocks__ directory) within my project. I believe I have a good grasp on how to utilize it, and everything seems to work smoothly if I eliminate a s ...

Bootstrap 4 Multilevel Navbar with Bootswatch design positioned on the right side

How can I modify a multilevel Navbar in Bootswatch 4 (Bootstrap 4) to make the Submenus pop open to the left side? The "dropdown-menu-right" class doesn't achieve the desired result. Can someone provide guidance on how the CSS / Javascript should be a ...

Attempting to display an image based on the outcome of a function that was executed earlier

As a beginner in the world of JavaScript, I kindly ask for your patience and detailed explanations in helping me understand the concepts. My current project involves creating a button that randomly selects a name from a list and displays it along with an ...

JavaScript - Assigning a class to an element based on an array

I am facing a challenge where I need to assign classes from an array to an element in sequential order. The issue is that once I reach the end of the array, I do not know how to loop back to the beginning and start over. Here is my current code: var bac ...

Incorporate an Ajax response script with a Django HttpResponse

How can I pass the ajax response obtained from the view to the template using HttpResponse? I'm unsure of the process. view.py analyzer = SentimentIntensityAnalyzer() def index(request): return render(request, "gui/index.html") @csrf_exempt d ...

Vuetify Dropdown Widget not Displaying Identifier in place of Text Value

I have created a form using <v-combobox> for autocompletion, which is working correctly. However, I am encountering an issue where the form submits the ID from the options array instead of the selected value. Despite setting the item-value="key" to m ...

How to locate the cell with a specific class using JavaScript/jQuery

I need help with the following code snippet: var output = '<tr>'+ '<td class="class1">One</td>'+ '<td class="selected">Two</td>'+ '<td>< ...

Is your Javascript navigation functioning properly on some submenus while encountering issues on others?

If you visit , you'll notice that it's a visually appealing site. The navigation submenus seem to be functioning properly, HOWEVER upon inspecting the element, you'll notice that under the PRICING tab, there are submenus that have the same c ...

Learn how to display a loading message instead of a blank page while your Vue.js application is loading

Whenever a new Vue.js page is accessed, there is a brief moment where a blank, white page is displayed until the entire application finishes loading. I have attempted multiple times to display a loading message first using the traditional method, but eve ...

Maximizing Efficiency: Top Techniques for Emphasizing Grid Rows with jQuery

Currently, I am facing an issue with the jQuery code I am using to highlight the selected row in my grid. It seems to be taking longer than anticipated to select the row. Does anyone have suggestions on how I can optimize this code for better performance? ...