GSAP ScrollTrigger is activated upon loading, rather than with continuous scrolling

I created an animation with a scrolltrigger following the example provided in this codepen demo.

The issue I'm facing is that the animation starts playing as soon as the page loads instead of triggering when I scroll past the specified point.

Any ideas on why this might be happening? I have the trigger method called in the mounted section, which I suspect is causing the problem. However, that's how it was done in the codepen example and the animation did not autoplay there. Having the method called in mounted should only set up the trigger on the page.

I also tried removing the call to this.animation() within the scrollTransition method thinking it might be triggering the animation. But omitting the parentheses makes the scroll trigger markers (and probably the trigger itself) disappear from the screen entirely.

Here's the snippet of code:

// HelloWorld.vue
<template>
  <div class="empty"></div>
  <div class="hello" ref="hello">
    // content
  </div>
  <div class="empty"></div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { gsap, Power3 } from 'gsap';
  import { scrollTransition } from '../utils/transitions/scrollTransition';

  export default defineComponent({
    name: 'HelloWorld',
    props: {
      msg: String,
    },
    methods: {
      animation() {
        gsap.from(this.$refs.hello, {
          autoAlpha: 0,
          duration: 1.2,
          ease: Power3.easeOut,
          y: 400,
        });
      },
      scroll() {
        scrollTransition(this.$refs.hello, this.animation());
      },
    },
    mounted: function () {
      this.scroll();
    },
  });
</script>


// scrollTransition.ts
import { gsap } from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);

export const scrollTransition = (
  trigger: HTMLElement,
  animation: gsap.core.Animation,
  props?: Object,
): gsap.plugins.ScrollTriggerInstance =>
  ScrollTrigger.create({
    trigger,
    animation,
    markers: true,
    start: 'top 50%',
    toggleActions: 'play complete none none',
    ...props,
  });

Answer №1

It was really frustrating for me because I forgot to include a return statement in my animation() function.

The solution is simple - just remember to add the return statement like this:

animation() {
  return gsap.from(this.$refs.hello, {
    autoAlpha: 0,
    duration: 1.2,
    ease: Power3.easeOut,
    y: 400,
  });
},

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

Triggering AJAX call from several buttons within a single page in Django

Hey there! I'm currently working on implementing a voting feature using Ajax in my Django-based website. The issue I'm facing is that users can only vote on the first entry, but I want them to be able to vote on all entries. Can you assist me wit ...

The challenge of reading a local JSON file in Angular JS

I previously had success with my query in a one-page sample project (index.html), but now I am working on a project with tabs (I created the project using Ionic Lab with tabs). I need to display the output of a JSON file on the tab-home.html page, which is ...

Material-UI organizes its Grid Items vertically, creating a column layout rather than a horizontal row

I'm struggling to create a grid with 3 items in each row, but my current grid layout only displays one item per row. Each grid item also contains an image. How can I modify the code to achieve a grid with 3 items in a row? Here's the code snippet ...

How can I interact with objects in a Three.js scene? Is document.getElementById() the appropriate method to use?

for ( var j = 0; j < 100; j ++ ) { var particles = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0x666666, program: programStroke } ) ); particles.position.x = Math.random() * 800 - 400; particles.position.y = Math.random() ...

"Enable Vue to bind to a dynamically generated JSON node, even creating it if it does

I want to use v-model to connect to a dynamic node in a JSON object and create the node if it doesn't exist. The node we need will be provided as part of a config. For example: <v-text-field :="responseNode"></v-text-field> He ...

Tips for applying styles to the parent item when the sub item is selected in a Kendo menu

Currently, I am utilizing the Kendo Menu feature to create a customized menu. To style the selected item in the menu, I have incorporated the following code: $("#menu").kendoMenu({ select: function (e) { // Remove previously selected options f ...

Using array.map() method in React to assign unique keys to sub-children

In my latest project, I created a React component that pulls data from an Azure Cosmos database in the form of a JSON object and presents it in a card layout on a web application. The JSON structure includes multiple key/value pairs at the top level, foll ...

What is the best way to use JavaScript to emphasize the searched text in a textbox?

My application uses JavaScript and I am looking to both highlight the search element and keep the cursor in the textbox positioned at the search item. Can anyone suggest a way to accomplish this using JavaScript? ...

Utilizing .env Variables in NestJS Main App Module for Establishing Database Connection

I recently embarked on my first NestJS project, where I initially had a hardcoded database connection string in app.module.ts that worked perfectly fine. However, our project requirements called for fetching the database configuration values from environm ...

Angular 2 wrap-up: How to seamlessly transfer filter data from Filter Component to App Component

A filtering app has been created successfully, but there is a desire to separate the filtering functionality into its own component (filtering.component.ts) and pass the selected values back to the listing component (app.ts) using @Input and @Output functi ...

Tips for refreshing the value of a dependency injection token

When using Angular dependency injection, you have the ability to inject a string, function, or object by using a token instead of a service class. To declare it in my module, I do this: providers: [{ provide: MyValueToken, useValue: 'my title value& ...

Setting a variable within an ng-repeat using ng-click

Struggling to create a pagination system and encountering issues with updating the CurrentPage Variable. When trying to update it using <li ng-click="currentPage"...., the variable does not update. Here is the problematic fragment: http://jsfiddle.net/f ...

Using Vue.js - Incorporate filtering functionality within the v-for loop

I've successfully implemented a Vue filter that restricts the length of an array to n elements. It functions perfectly when used like this: {{ array | limitArray(2) }} Now, I'm attempting to utilize it within a v-for loop as follows: <li v- ...

Executing a Code-Behind function through AJAX without triggering a page refresh

I am looking for a way to directly call a code behind function without going through the PageLoad event in my ajax.aspx.vb file. Currently, I can call the function from the PageLoad event and pass the response using a variable located on the ajax.aspx page ...

In Node.js, the module.exports function does not return anything

After creating a custom function called module.exports, const mongoose = require("mongoose"); const Loan = require("../models/loan_model"); function unpaidList() { Loan.aggregate([ { $group: { _id: "$ePaidunpaid", data: { $pus ...

Even though the Spotify API JSON response may be undefined, I am still able to log it using console.log()

Trying to utilize Spotify's Web Player API in order to retrieve the 'device_id' value has been a challenge. The documentation states that the server-side API call I am supposed to make should result in a 'json payload containing device ...

Utilize ES6 syntax to bring in a package as an extension of another package

To expand map projections in D3, it is recommended to import the necessary packages like so: const d3 = require("d3") require("d3-geo-projection")(d3) This allows you to access methods such as d3-geo-projection's geoAiry method fr ...

Preventing Scope Problems in AngularJS

Spending more than 4 hours trying to figure out why my ng-model directive was not correctly binding with ng-options within my controller was frustrating. The <select> element seemed to be initialized properly, receiving a value from the parent scope, ...

Embedding PayPal buttons within a Vue.js component

I am in the process of integrating PayPal order buttons into my Vue.js component. I have been referencing the official documentation which outlines three key steps: Import the PayPal SDK script Create a <div> element where the buttons will be displ ...

Issues with Bootstrap 4 accordions failing to expand on mobile devices

I'm facing a strange issue with my Bootstrap 4 accordions. They work fine in responsive mode on Chrome, but when I try it on an Android phone or iPhone, they refuse to open. I'm really puzzled by this. Any thoughts on what might be causing this? ...