Jump to content
Search Community

TypeError: $el[0] is undefined

martin_cnd test
Moderator Tag

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

*****EDIT: SOLVED***** (was one tiny typo where "g" should have been a "G"

 

 

 

Hi, I have been trying to get this to work for the last few days but it just won't..

 

When using the following code to animate only one single svg it works just fine:

function pathPrepare ($el) {
		var lineLength = $el[0].getTotalLength();
		$el.css("stroke-dasharray", lineLength);
		$el.css("stroke-dashoffset", lineLength);
	}

var $bulb = jQuery("path#light-bulb");
var $body = jQuery("path#body");
var $dashone = jQuery("path#dash-one");
var $dashtwo = jQuery("path#dash-two");
var $dashthree = jQuery("path#dash-three");
var $dashfour = jQuery("path#dash-four");
var $dashfive = jQuery("path#dash-five");
var $dashsix = jQuery("path#dash-six");
var $dashseven = jQuery("path#dash-seven");

// prepare SVG
pathPrepare($bulb);
pathPrepare($body);
pathPrepare($dashone);
pathPrepare($dashtwo);
pathPrepare($dashthree);
pathPrepare($dashfour);
pathPrepare($dashfive);
pathPrepare($dashsix);
pathPrepare($dashseven);

	// init controller
	var controller = new ScrollMagic.Controller();

	// build tween
var tween = new TimelineMax()
.add(TweenMax.to($bulb, 0.5, {strokeDashoffset: 0, ease:Linear.easeNone})) 
.add(TweenMax.to($body, 0.5, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashone, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashtwo, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashthree, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashfour, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashfive, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashsix, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to($dashseven, 0.15, {strokeDashoffset: 0, ease:Linear.easeNone}))
.add(TweenMax.to("path", 1, {stroke: "#F4BD89", ease:Linear.easeNone}), 0);		

	// build scene
	var scene = new ScrollMagic.Scene({triggerElement: "#svg-trigger", triggerHook: 'onEnter', duration: '50%', offset: 45, tweenChanges: true})
					.setTween(tween)
					.addTo(controller);

Once I had that working I started on trying to do the same for other SVGs on my page and optimizing the code a little following other topics in this forum. Now no matter what I do, I get the error "TypeError: $el[0] is undefined". I triple checked all the ids to match my svg and everything but just can't get it to work :/ am I overlooking something here (quite the newbie here;) )

 

Here is the code that throws the error:

function pathPrepare ($el){
var lineLength = $el[0].getTotalLength();
$el.css("stroke-dasharray", lineLength);
$el.css("stroke-dashoffset", lineLength);
}

var $bulb = jQuery("path#light-bulb");
var $body = jQuery("path#body");
var $dashone = jQuery("path#dash-one");
var $dashtwo = jQuery("path#dash-two");
var $dashthree = jQuery("path#dash-three");
var $dashfour = jQuery("path#dash-four");
var $dashfive = jQuery("path#dash-five");
var $dashsix = jQuery("path#dash-six");
var $dashseven = jQuery("path#dash-seven");
var $innovativehead = jQuery("path#head");
var $biggearinner = jQuery("path#innerGear");
var $biggearouter = jQuery("path#outerGear");
var $mediumgearinner = jQuery("path#outerGearTwo");
var $mediumgearouter = jQuery("path#outerGearTwo");
var $smallgearinner = jQuery("path#innergearThree");
var $smallgearouter = jQuery("path#outergearThree");


pathPrepare($bulb);
pathPrepare($body);
pathPrepare($dashone);
pathPrepare($dashtwo);
pathPrepare($dashthree);
pathPrepare($dashfour);
pathPrepare($dashfive);
pathPrepare($dashsix);
pathPrepare($dashseven);
pathPrepare($innovativehead);
pathPrepare($biggearinner);
pathPrepare($biggearouter);
pathPrepare($mediumgearinner);
pathPrepare($mediumgearouter);
pathPrepare($smallgearinner);
pathPrepare($smallgearouter);

var controllerIcon = new ScrollMagic.Controller();


var tweenCreative = new TimelineMax();
tweenCreative.to([ $bulb, $body, $dashone, $dashtwo, $dashthree, $dashfour, $dashfive, $dashsix, $dashseven ], 1, {strokeDashoffset: 0, ease:Linear.easeNone});

var tweenInnovative = new TimelineMax();
tweenInnovative.to([ $innovativehead, $biggearinner, $biggearouter, $mediumgearinner, $mediumgearouter, $smallgearinner, $smallgearouter ], 1, {strokeDashoffset: 0, ease:Linear.easeNone});


var sceneCreative = new ScrollMagic.Scene({
triggerElement: '#svg-trigger',
duration: '50%',
triggerHook: 'onEnter',
offset: 45, 
tweenChanges: true
})
.setTween(tweenCreative)
.addTo(controller);

var sceneInnovative = new ScrollMagic.Scene({
triggerElement: '#innovative-trigger',
duration: '50%',
triggerHook: 'onEnter',
offset: 45, 
tweenChanges: true
})
.setTween(tweenInnovative)
.addTo(controllerIcon);

I would really appreciate some help on this since it got me pulling out my hair for a few days now.

 

Thanks a lot in advance!

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.
×
×
  • Create New...