Jump to content
GreenSock

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

Greensock JS with Wordpress

Recommended Posts

Hi everyone,

Could someone please give me a quick idiots guide to using greensock JS in wordpress.

Many thanks in advance, I look forward to some replies.

Link to comment
Share on other sites

Did you have any specific ideas about what you want to animate on your site, or were you just after ideas about how GSAP might be used in a blog?

 

http://www.greensock.com/get-started-js/ has some instructions for getting GSAP embedded in your web page, and if you aren't familiar with using GSAP, you can always check out http://www.greensock.com/jump-start-js/ for a great introduction to many of its features.

Link to comment
Share on other sites

  • 4 weeks later...

Well I guess since Ive first used greensock in flash, the weakness of flash was that it wasnt practical to use it with wordpress cms. using greensock to me is like being able to create a cool animated engaging website like i used to do in flash but now we have wordpress as the cms. I know how to use greensock now im learning php/wordpress to see if the two can integrate. Dwayne

Link to comment
Share on other sites

Hi Dwayne,

 

Thanks for sharing your findings. That article is a great starting point and quite frankly I don't have much to add.

 

Check out this article by Jens: http://ahrengot.com/tutorials/greensock-javascript-animation/

That is a wordpress post and he does some fancy GSAP work for his page-build animation.

I'm sure you could peak under the hood and get a feel for how he approached it.

Link to comment
Share on other sites

  • 1 year later...

I realize this thread is old, but in case others happen upon it like I did, another option for embedding GSAP in your WordPress site (especially if you don't have access to the file system, as is my case) is to use the CDN version of the JS file. Read more about this option here: http://greensock.com/get-started-js#loading

Link to comment
Share on other sites

Hello,

 

If you are developing a WordPress theme or customizing a WordPress theme, you can add your JS file within your WordPress functions.php file

<?php
// The proper way to enqueue GSAP script
// wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); 
function theme_gsap_script() {
	wp_enqueue_script( 'gsap-js', 'http://cdnjs.cloudflare.com/ajax/libs/gsap/1.13.2/TweenMax.min.js', array(), false, true );
}
add_action( 'wp_enqueue_scripts', 'theme_gsap_script' );
?>

:

The last parameter shows true which means WordPress will add the JS file to the footer instead of in the head,

 

Here is the link for more information on enqueuing JS files in WordPress:

 

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

 

Hope this helps! :)

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi, sorry if that's necro posting, but mentioned above method is not working anymore (?) or I messed up something.

Anyway, this should work, it did job for me   :)

function enqueue_TweenMax() {
  if ( ! is_admin() ) {
    $scriptsrc = get_template_directory_uri() . '/assets/js/TweenMax.min.js'; 
    wp_register_script( 'TweenMax', $scriptsrc );
    wp_enqueue_script( 'TweenMax' );
  }
}
add_action( 'wp_enqueue_scripts', 'enqueue_TweenMax' );

Hope this can help someone :)


 

  • Like 1
Link to comment
Share on other sites

Hello Patryk D&D

 

My code above uses a full external path CDN URL for the GSAP TweenMax script, and was using version 1.13.2, since it is an old post. So make sure you are using the latest version of GSAP ;)

 

And nothing has changed in the WordPress API for wp_enqueue_script(), and i just checked and that method still works!

 

The WordPress method get_template_directory_uri() will give the path to the parent theme. But in order to account for possible child themes you should be using get_stylesheet_directory_uri(), since that will be relative to the child theme if used.

So note that:

:)

  • Like 1
Link to comment
Share on other sites

Thanks Jonathan !

I did use your code with newest CDN (I pasted it into my parent theme (Avada) functions.php) and it didn't lunch, but probably I messed up something if it works for you ^_^ 

Anyway again thanks for double checking this :) 

Still maybe someone will find this helpful for local TweenMax in their WordPress theme :D

Have a great day !  

  • Like 2
Link to comment
Share on other sites

Hi, sorry if that's necro posting, but mentioned above method is not working anymore (?) or I messed up something.

 

Anyway, this should work, it did job for me   :)

function enqueue_TweenMax() {
  if ( ! is_admin() ) {
    $scriptsrc = get_template_directory_uri() . '/assets/js/TweenMax.min.js'; 
    wp_register_script( 'TweenMax', $scriptsrc );
    wp_enqueue_script( 'TweenMax' );
  }
}
add_action( 'wp_enqueue_scripts', 'enqueue_TweenMax' );

Hope this can help someone :)

 

 

 

 

 

i copied this code and pasted into functions.php of my WP theme. i can now see the that TweenMax is loaded into the Header of each page.

 

i have this on my homepage as a test. there is a div called iconWrapper on my homepage. 

 

it appears to be so simple but NOTHING happens and im losing the will to live after staring at it for hours

<button onclick="myFunction()">Try it</button>


<script>
function myFunction() { 
var tl= new TimelineMax(); 


tl.to("#iconWrapper", 0.5, { opacity: 0, ease: Power2.easeInOut })


 }
</script>
Link to comment
Share on other sites

Hi,

 

can anyone offer some insight on my post above?  

 

do i have to write js in some sort of shorthand or wrap in some tags that WP understands?

Link to comment
Share on other sites

Hi Liam,

 

Are you including the js file for the animation as well?. Are you setting GSAP and jQuery as a dependency for your animations file?. Finally are you queueing the files in the footer or adding a DOM ready listener in it?.

 

Most likely, your files are being loaded but the DOM might not be ready yet, specially if you're adding them on the header. Is better to move them to the footer using the final boolean of the enqueue_script method:

 

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

 

Also you might want lo wrap your js code inside this, in order to avoid conflicts with other files and be sure that the DOM is ready:

(function($){$(function(){
	
  // code
	
});}(jQuery));
  • Like 1
Link to comment
Share on other sites

 

Hi Liam,

 

Are you including the js file for the animation as well?. Are you setting GSAP and jQuery as a dependency for your animations file?. Finally are you queueing the files in the footer or adding a DOM ready listener in it?.

 

Most likely, your files are being loaded but the DOM might not be ready yet, specially if you're adding them on the header. Is better to move them to the footer using the final boolean of the enqueue_script method:

 

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

 

Also you might want lo wrap your js code inside this, in order to avoid conflicts with other files and be sure that the DOM is ready:

(function($){$(function(){
	
  // code
	
});}(jQuery));

 

hi

 

im not including the js file for the animation, i wrote everything in the text editor  (see below)

<button onclick="myFunction()">Try it</button>


<script>
function myFunction() { 
var tl= new TimelineMax(); 


tl.to("#iconWrapper", 0.5, { opacity: 0, ease: Power2.easeInOut })


 }
</script>

so as you can see my interaction is on click. if i wait 10 minutes before clicking, still nothing happens.

 

im enqueuing Tweenmax in the header, no DOM listener.

 

ill make the change as you suggested but do you think based on the fact that the animation is not automatically triggered, the loading times are not the source of my issue?

Link to comment
Share on other sites

Hi,

 

Mhh.. hard to tell. I've always included JS stuff for wordpress (regardless of it's complexity) via the functions.php file or a specific plugin file, using the common hooks. Never went the way you're describing (including code in the post editor I presume).

 

A couple of questions, are you getting any kind of errors in the JS console?; have you tried some console.log() or alert() in the code you're using to see if it's being executed at all?. Perhaps in the backend wordpress is removing <script> tags.

  • Like 1
Link to comment
Share on other sites

You might have to turn off the rich or visual editor for your user. Since WordPress will strip the <script> tag out of the editor, and sometimes the src attribute.

 

From the dashboard, go to Users > Your Profile > Personal Options

 

Here is the link to the WordPress Codex on how to use JavaScript in WordPress

 

WordPress Using Javascript: https://codex.wordpress.org/Using_Javascript

 

But like Rodrigo advised adding your script either the footer.php or through the functions.php is best practice. :)

Link to comment
Share on other sites

thanks guys,

 

il take that onboard. but to answer Rodrigo's question. i have done a simple alert in the text editor and it worked fine 

Link to comment
Share on other sites

Try to console out TweenMax to make sure it is defined

 

console.log(TweenMax);

 

If you get a TweenMax is undefined error message in the console. That means your trying to run your code before TweenMax is loaded. If TweenMax returns itself then you have something else going on.

 

But your better off just enqueing TweenMax in the footer and then enqueue another custom script with your custom TweenMax code.

  • Like 1
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.
×