简体   繁体   English

Smoothstate.js-通过functions.php加载特定的样式表和脚本不起作用

[英]Smoothstate.js - Loading specific stylesheets and scripts through functions.php not working

I'm wondering if someone could help me please. 我想知道是否有人可以帮助我。 I'm currently using the Smoothstate.js plugin to transition between pages on my Wordpress website. 我目前正在使用Smoothstate.js插件在我的Wordpress网站上的页面之间进行转换。 So far the plugin works great and does the majority of what i want it to do. 到目前为止,该插件工作得很好,并且可以完成我想要的大部分工作。

However, when I try and load separate files for individual pages via the functions.php file in Wordpress, when I navigate between pages some of the scripts aren't working. 但是,当我尝试通过Wordpress中的functions.php文件为各个页面加载单独的文件时,当我在页面之间导航时,某些脚本无法正常工作。

I have made sure the scripts are loaded in the container so Smoothstate.js knows what files have been added and I have also re init the functions using the onAfter function. 我已确保将脚本加载到容器中,以便Smoothstate.js知道已添加了哪些文件,并且我还使用onAfter函数重新初始化了这些函数。

It's weird because when I check the DOM after navigating the pages, I can see the scripts being loaded in but then If i check the sources tab in the dev tools they don't appear? 这很奇怪,因为在浏览页面后检查DOM时,我可以看到正在加载的脚本,但是如果我在开发工具中检查“源”选项卡,它们不会出现吗?

I need to be able to specifically load seperate files depending on what page you're on for performance reasons so I'm hoping there's a way of doing this without conflicting with Smoothstate.js. 由于性能原因,我需要能够根据您所在的页面来专门加载单独的文件,因此我希望有一种方法可以做到这一点而不与Smoothstate.js发生冲突。 Does anyone have any experience with this or know of an answer? 有人对此有任何经验或知道答案吗?

My setup is like so: 我的设置是这样的:

Functions.php Functions.php

As you can see I only want Google maps to load on the contact page. 如您所见,我只希望Google地图加载到联系页面上。

  wp_enqueue_script( 'anime-script', get_stylesheet_directory_uri() . '/js/anime.min.js', array(), $the_theme->get( 'Version' ), true);
    wp_enqueue_script( 'scrollMonitor-script', get_stylesheet_directory_uri() . '/js/scrollMonitor.js', array(), $the_theme->get( 'Version' ), true);
    wp_enqueue_script( 'scroll-scripts', get_stylesheet_directory_uri() . '/js/scrollreveal.js', array(), $the_theme->get( 'Version' ), true );
    wp_enqueue_script( 'type-script', get_stylesheet_directory_uri() . '/js/typed.min.js', array(), $the_theme->get( 'Version' ), true);

    if(is_page('contact-us')) {
        wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyC5_sL4wf3GIEgwI7bUXWD4pqkkc8er7tQ', array(), $the_theme->get('Version'), true);
        wp_enqueue_script( 'google-maps-settings', get_stylesheet_directory_uri() . '/js/google-maps.js', array(), $the_theme->get( 'Version' ), true);
        wp_enqueue_script( 'contact-slider', get_stylesheet_directory_uri() . '/js/contact-slider.js', array(), $the_theme->get( 'Version' ), true);
    }

Smoothstate js Smoothstate js

$(document).ready(function() {
  init();
});

    function init() {
      ImageSliders();
      navbarColour();
      typedBanners();
      scrollingAnimations();
      navBarHide();
      animateOnLoadPosition();
      openSubscribePanel();
    };

    $(document).ready(function() {
      'use strict';

      addBlacklistClass();

      // Init here.
           var $body = $('body'),
               $main = $('#page'),
               $site = $('html, body'),
               transition = 'fade'


      var options = {
        prefetch: true,
        cacheLength: 2,
        debug: true,
        blacklist: '.wp-link, .open-panel-link',

        onBefore: function($anchor, $container) {
                    var current = $('[data-viewport]').first().data('viewport'),
                        target = $anchor.data('target');
                    current = current ? current : 0;
                    target = target ? target : 0;
                    if (current === target) {
                        transition = 'fade';
                    } else if (current < target) {
                        transition = 'grow';
                    } else {
                        transition = 'moveleft';
                    }

                  var highResImageUrl = $anchor.find('.thumbnail-holder').data('src');
                  var thumbnailholder = $anchor.find('.thumbnail-holder');
                  var newImage = $anchor.closest('.thumbnail-wrapper').append('<img class="imageGrow" src="' + highResImageUrl + '">');
      },

        onStart: {
          duration: 1200, // Duration of our animation
          render: function (url, $container) {
            // Add your CSS animation reversing class
            $main.attr('data-transition', transition);
            $main.addClass('is-exiting');

            // Restart your animation
            smoothState.restartCSSAnimations();
          }
        },
        onReady: {
          duration: 0,
          render: function ($container, $newContent) {
            // Remove your CSS animation reversing class
            $container.removeClass('is-exiting');

            // Inject the new content
            $container.html($newContent);
          }
        },
        onAfter: function() {

            init();
            addBlacklistClass();

              if (typeof contactSliderUI == 'function') {
                contactSliderUI();
              }

              $('.acf-map').each(function() {
                    // create map
                    map = new_map( $(this) );
                });


            var $hash = $( window.location.hash );

                    if ( $hash.length !== 0 ) {

                        var offsetTop = $hash.offset().top;

                        $( 'body, html' ).animate( {
                                scrollTop: ( offsetTop - 60 ),
                            }, {
                                duration: 280
                        } );
                    }
                }
      },
      smoothState = $('#page').smoothState(options).data('smoothState');
    });

You need to add <?php body_class ?> to your container or to another element inside your container (m-scene). 您需要将<?php body_class ?>添加到容器或容器内的另一个元素(m-scene)。 Smoothstate is only handling what is inside the container, your classes on your <body> element will not change unless it's inside your "m-scene" container. Smoothstate仅处理容器中的内容,除非您在“ m-scene”容器中,否则<body>元素上的类不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM