Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
iuscare

Cannot tween a null target. ES6 Module

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi there,

 

I know this might not be a pure gsap related issue, but I hope someone can point me into the right direction. I want to avoid the use of jQuery and instead want to use pure javascript with es6 syntax. Therefore I created a button.js file, in which I want to create the module for my button animation powered by gsap. The jQuery version already works, so I simply have to translate this version into an es6 module but I am failing and keep getting "Cannot tween a null target". Obviously I am doing something wrong in the es6 syntax, but I can't figured it out :( 

 

Here is my button.js – codepen throws the same error:

 

import TweenMax from 'gsap';

export default class Button {
  constructor() {
    this.btns = document.querySelectorAll('.tg-btn');

    this.init();
  }

  init() {
    for(let i = 0, max = this.btns.length; i < max; i++) {
        let el = this.btns[i];
        console.log(el);
        console.log(i);
        let a = el.querySelector('.inner1');
        let b = el.querySelector('.inner2');
        let c = el.querySelector('.inner3');
        let d = el.querySelector('.inner4');
        let e = el.querySelector('.inner5');
        let m = 0;
        let n = 50;
        let o = "center";
        let p = null;
        let tl = new TimelineMax({paused: true});
        var t = tl
            .set(el,{willChange:"transform"})
            .set([e[i], d[i]], {opacity: 0,width: 1,right: "center" === o ? -n / 2 : -n - 20})
            .set(e[i], {rotationZ: "45deg"})
            .set(d[i], {rotationZ: "-45deg"})
            .to(a[i], .2, {opacity: 0,left: 15,ease: Sine.easeIn})
            .to(c[i], .3, {right: "center" === o ? -n / 2 : -n - 20,ease: Expo.easeOut}, .1)
            .to(c[i], .5, {width: n,ease: Expo.easeOut}, .3)
            .to([e[i], d[i]], .2, {opacity: 1,ease: Sine.easeOut}, .35)
            .to(e[i], .3, {width: 8,ease: Quart.easeOut,transformOrigin: "100% 50%"}, .45)
            .to(d[i], .3, {width: 8,ease: Quart.easeOut,transformOrigin: "100% 50%"}, .45)
            // to right animation            
            tl.addLabel("midpoint", .8),
            tl.add(function() {
                tl.stop()
            }, "midpoint"),
            tl.set(a[i], {left: -15}, "midpoint0.31"),
            tl.to([c[i], d[i], e[i]], .3, {right: -600,ease: Expo.easeIn}, "midpoint0.31"),
            tl.to(a[i], .3, {opacity: 1,ease: Sine.easeOut}, "midpoint0.5"),
            tl.to(a[i], .3, {left: 0,ease: Sine.easeOut}, "midpoint0.5"),
            tl.stop();

            //el.animation = t;
            el.data('someTimeline', t);

            //create the event handler
            el.addEventListener('mouseover', function(){   
                alert("trigger");
                t.play();
            });

            el.addEventListener('mouseout', function(){
                t.reverse();
            });
    }
  }

  destroy() {
    window.removeEventListener('mouseover');
  }
}

new Button();

 

See the Pen rryNBy by iuscare (@iuscare) on CodePen

Link to comment
Share on other sites

Hi @iuscare :)

 

The target of the tweens is undefined the way you have it written.
 

// your target is a
let a = el.querySelector('.inner1');

// but your tween is targeting a[0] which doesn't exist
.to(a[i], .2, {opacity: 0,left: 15,ease: Sine.easeIn})

 

GSAP also does not have a stop method so I commented out the lines with that code. I assume you were probably looking for the pause() method?

 

Here's fork of your pen.

See the Pen xJqGgE by PointC (@PointC) on CodePen

Hopefully that helps Happy tweening.

:)

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hey @PointC always to the rescue,

 

thank you so much for giving me this input. I got it working. One problem persisted, because vanilla mouseover and mouseleave events also fire when hovering over a child element of the button, which is not intended, of course. I managed to get around by setting the pointer-events to none for those child elements and hope I won't cross browser incompatibility issues.

 

Anyway thank you so much for your help! :) 

 

 

  • Like 1
Link to comment
Share on other sites

@iuscare Actually mouseover and mouseout bubbles so every time you mouse over or out on child elements it will trigger your event listener on parent. Instead mouseenter and mouseleave do not bubble so if you move your mouse on child elements, it won't trigger the event.

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Hello @Sahil,

 

many many apologizes for my late reply. I was not at home the last few days. You are right, I looked up the docs. I did not use mouseenter and mouseleave cause I thought they were pure jQuery related, but they are not obviously. Thank you so much for that hint. So they are safe and ready for use in production just because of you two guys :-)

 

Thanks!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×