What is the best way to activate a function within an npm package in a Vue application?

I'm just starting out with Vuejs and I've recently installed the vue-countup-v2 npm package. I successfully imported it into my Vue component and noticed that it works perfectly when the page loads. However, I am interested in triggering the CountUp component when the user scrolls down the screen, rather than upon page load. The vue-countup-v2 package includes a method called start() which can be triggered for this purpose, but I'm unsure of how to do so.

Below is the code snippet:


  <template>
  <div id="main-wrapper" class="flex justify-around w-full p-12 my-12">
      <CountUp
           id="count-up"
           :delay="delay"
           :endVal="endVal[index]"
           :options="options"
      />
  </div>
  </template>

<script>
import CountUp from 'vue-countup-v2';
import inViewport from 'in-viewport';

export default {
  name: "NumberAnime",
  components: {
    CountUp,
  },

  data() {
    return {
      delay: 1000,
      endVal: [40, 300000, 25000, 350],
      options: {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        prefix: '',
        suffix: '',
      }
    }
  },

  mounted() {
    inViewport('elementId', 'the function I want to callback')
  },
}
</script>

Answer №1

For those looking for a solution in the future, here is how I resolved the issue:

To control the execution of code when scrolling down, set a v-if condition to false within the count tag, and then change it to true during scrolling to initiate the process.

<div id="before_count_div" class="text-4xl flex items-end">
            <CountUp
                v-if="options.begin == true"
                id="count"
                :delay="delay"
                :endVal="endVal[index]"
                :options="options"
            />
          </div>

In the data:

options: {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        prefix: '',
        suffix: '',
        begin: false,
      }

In the mounted:

mounted() {
    const el = document.getElementById('before_count_div');
    inViewport(el, () => {
      this.options.begin = true;
    })
  },

Answer №2

Based on my understanding, you can make use of the "ref" attribute with the CountUp component

<CountUp
       ref="counterRef"
/>

this.$refs.counterRef.start()

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

JavaScript: abbreviated way to selectively append an element to an array

Currently, I am in the process of creating a Mocha test for a server at my workplace. When dealing with customer information, I receive two potential phone numbers, with at least one being defined. var homePhone = result.homePhone; var altPhone = ...

The filter function in Material UI data grid does not seem to be functioning properly when using the renderCell method

I'm currently working on a react project that includes a Data Grid. While the filter functionality works well with most fields, it seems to be having issues with the field that utilizes renderCell. Is there a way to enable filtering for the movie titl ...

What is it about the setTimeout function that allows it to not block other

Why is setTimeout considered non-blocking even though it is synchronous? And on which thread does it run if not the main thread? ...

What is the best way to display a child component inside an iframe using Vue.js?

Looking to provide a live preview of an email before sending it out, I've opted to use an iframe to contain the preview and prevent style leaks. The goal is for the preview to update dynamically as the user fills out form details. How can I display a ...

Using Angular JS to submit forms on a regular basis

My HTML form is set up within an Angular controller with inputs, action, and other elements already defined. The only issue I'm facing is that the form does not have a traditional submit button. Instead, there is a separate button on the page outside ...

Encountering an issue with Material-UI and Next.js: "TypeError: theme.spacing is not a function

Encountering an issue after modifying _app.js to dynamically generate a material UI theme. I've been following the implementation example provided by the material-ui team at: https://github.com/mui-org/material-ui/tree/master/examples/nextjs. To summ ...

JQuery class for swapping elements upon scrolling

I am currently working on a navigation bar that changes classes to create a fading effect for the background. The approach I have taken involves targeting the window itself and monitoring the scroll position of the user. If the user scrolls down more than ...

Having trouble with adding an event listener on scroll in React JS. Need assistance in resolving this issue

I'm having trouble adding an event listener for when a user scrolls in my web app. componentDidMount = () => { let scrollPosition = window.scrollY; let header = document.getElementById("topBar"); window.addEventListener(&ap ...

Ways to forward a parameter to a different web address?

Is there a way to properly redirect a parameter such as http://example.com/?link=https://google.com to its destination at ? ...

Enhancing Front-end Development with Node NPM Modules and managing multiple downloads of shared dependencies

As a newcomer to Node/NPM, I have a question as a front-end developer. One of the key benefits of NPM modules is that their dependencies are installed within themselves in node_modules. This means that modules always contain the code they need, eliminating ...

Float over a specific line in a drawing

I am looking to develop a unique rating system using css, html, and potentially js : My goal is for the user to hover over a specific section of a circular stroke and have it fill with a particular color, all while maintaining functionality. So far, I hav ...

Issues with Angular Structural Directives arising from NPM installation concerns are causing problems

I have developed an npm package called sezam-shareds To integrate the package into a new project, you need to follow these steps: Add the following component from the package: <sezam-overflow [show]="true"></sezam-overflow> to a compo ...

Is it possible for pdfjs-dist to be used with Typescript?

Is there a way to preview a PDF as a canvas without importing pdfjs-dist into my project? I have already used the command $yarn add pdfjs-dist to install pdfjs-dist. Do I need to include any additional imports? import pdfjsLib from "pdfjs-dist/build ...

"Patience is key when waiting for the alert dialog response in Vuetify

I currently have a reusable component called Alert.vue. <v-dialog v-if="alertDict" v-model="alertDict.showDialog" max-width="460"> <v-card> <v-card-title>Title</v-card-title> & ...

Error message: A syntax error has occurred due to a type error, which prevents the program from reading properties that are undefined, specifically 'spaces'. To

Hey, I'm encountering a peculiar issue while running $npm run serve https://i.stack.imgur.com/mPzw7.png Check out my page layout below: <template> <div> ... </div> </template> <script> export default { name: ...

send back the result to the primary function

I need help with this code. I'm trying to return the budget from callbacks in the main function. How can I return a value from the main function? // This method returns the current budget of the user getCurrentBudget: function (req) { var reqTok ...

Setting up the Font Awesome Pro version in Angular using the Font-Awesome package

The process of installing the pro version of Angular Font-awesome began with setting up my registry using these commands: npm config set "@fortawesome:registry" https://npm.fontawesome.com/ && \ npm config set "//npm.fontawesome.com/:_authTo ...

Using dangerouslySetInnerHTML in React within a Fragment

In my current project, I have a specific requirement where I need to format text in React and also include HTML rendering. Here's an example of what I'm trying to accomplish: import React, {Fragment} from "react"; import {renderToString} from " ...

The Node Security service has been halted due to an error: getaddrinfo ENOTFOUND api.nodesecurity.io

The Node Security Platform service was officially deprecated on September 30th of this year. Despite this, I continued using the NSP package until it stopped working today. I wanted to share the error publicly so others can reference it in the future. The ...

How can one easily retrieve the callback function arguments from outside the function?

Here is a snippet of my code: var jenkins = require('jenkins')('http://192.168.1.5:8080'); var job_name = undefined; jenkins.job.list(function doneGetting(err, list) { if (err) throw err; job_name = list[0].name; }); jenkins. ...