-
AuthorPosts
-
March 11, 2016 at 2:00 am #833289
Hi,
I would like to add a script for scroll-controlled animations such as seen here: https://ihatetomatoes.net/demos/one-page-scroll-animations/
Would just like to have some pointers on whether this type of script is possible to integrate within X theme. It is composed of multiple js scripts with their own folder structure.
And then the on-page integration, I am in doubt whether this is done via code snippet or raw content?
Would appreciate some pointers.
Many thanks,
March 11, 2016 at 2:30 am #833308Hello There,
Thanks for writing in! Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.
Once you already have your child theme active and ready, please insert this following code in your child theme’s functions.php file so that you can add a custom JS to the site.
// Add custom JS scripts // ============================================================================= function load_custom_js() { wp_enqueue_style( 'custom-js', 'get_template_directory_uri() . '/custom-js.css', array(), ); } add_action( 'wp_enqueue_scripts', 'load_custom_js' ); // =============================================================================
To know more how you properly load custom js, please check the codex: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
Hope this helps.
March 11, 2016 at 1:18 pm #833900thank you! will check it out.
March 11, 2016 at 5:54 pm #834208You’re most welcome.
March 12, 2016 at 3:30 am #834645actually, the above code causes an error. the whole site goes blank…
<?php // ============================================================================= // FUNCTIONS.PHP // ----------------------------------------------------------------------------- // Overwrite or add your own custom functions to X in this file. // ============================================================================= // ============================================================================= // TABLE OF CONTENTS // ----------------------------------------------------------------------------- // 01. Enqueue Parent Stylesheet // 02. Additional Functions // ============================================================================= // Enqueue Parent Stylesheet // ============================================================================= add_filter( 'x_enqueue_parent_stylesheet', '__return_true' ); // Add custom JS scripts // ============================================================================= function load_custom_js() { wp_enqueue_style( 'custom-js', 'get_template_directory_uri() . '/custom-js.css', array(), ); } add_action( 'wp_enqueue_scripts', 'load_custom_js' ); // =============================================================================
March 12, 2016 at 3:39 am #834649Hi there,
Please add JS file URL instead of
/custom-js.css
.e.g:
custom-js-folder/custom-js-file.js
Hope it helps.
March 12, 2016 at 3:40 am #834653Yes, I see that now (I was taking your example code too literally). Thanks.
March 12, 2016 at 3:44 am #834657You’re welcome.
March 12, 2016 at 4:10 am #834671hmmm. still having problems. maybe I’m not understanding the relative URL scoping from the child theme’s functions php.
I have a js script at child theme’s root level (where functions.php is) and then:
function load_custom_js() { wp_enqueue_style( 'custom-js', 'get_template_directory_uri() . '/scrollissimo.js', array(), ); } add_action( 'wp_enqueue_scripts', 'load_custom_js' ); // =============================================================================
This causes an error.
March 12, 2016 at 4:23 am #834676Hi There,
Please try with this code instead:
function load_custom_js() { wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/scrollissimo.js' ); } add_action( 'wp_enqueue_scripts', 'load_custom_js' );
Hope it helps 🙂
March 12, 2016 at 4:29 am #834682that cleared the error! thanks! Am I right in thinking that if I use external js scripts like greensock tweening js which are on another server, I will need to copy them locally and use the same method as above? And do I use a load_custom_js() function for each and every one of them?
March 12, 2016 at 4:34 am #834685Hi There,
And do I use a load_custom_js() function for each and every one of them?
You don’t need to repeat adding the function load_custom_js(), please try with this code:
function load_custom_js() { wp_enqueue_script( 'TweenMax', '//cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js' ); wp_enqueue_script( 'custom-js', get_template_directory_uri() . '/scrollissimo.js' ); } add_action( 'wp_enqueue_scripts', 'load_custom_js' );
Hope it helps 🙂
March 12, 2016 at 4:36 am #834687beautiful, thanks.
March 12, 2016 at 4:46 am #834695Glad we were able to help 🙂
If you need anything else, please let us know.
-
AuthorPosts