javascript - content.toggleAnimationClass();不是函数

标签 javascript jquery html css smoothstate.js

我有 htmlcss 可以正常使用 smoothState.js 的最基本用法

;(function ($) {
 $('#main').smoothState();
})(jQuery);

然而,即使使用这个基本实现,如果您选择当前页面的链接,(即重新加载页面)您将得到一个空白的 white 屏幕,其中 2015 写在左上角。对于此错误,控制台中未记录错误,这让我认为这是由 smoothState.js

处理的错误

如果我扩展 smoothState 以允许更高级的选项,(即“is-exiting”)现在无法在站点中导航,离开页面。

通过高级实现,显示在这个问题的末尾,我得到了控制台错误:

Uncaught TypeError: content.toggleAnimationClass is not a function main.js:134
    $.smoothState.onStart.render @ main.js:134
    load                         @ jquery.smoothState.js:533
    clickAnchor                  @ jquery.smoothState.js:589
    n.event.dispatch             @ jquery.min.js:3
    n.event.add.r.handle         @ jquery.min.js:3

这是html:

<head></head>
<body>
 <div id="main" class="m-scene">

  <header id="header">
  </header>

  <div id="page_content_container" class="scene_element scene_element--moveUpIn">

   <section class="about-intro-container">
   </section>

   <section class="about-services-container">
   </section>

   <section class="about-randomBits-container">
   </section>

   <footer>
   </footer>

  </div><!-- #page_content_container -->
 </div><!-- #main -->

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="js/jquery.smoothState.js"></script>
 <script src="js/main.js"></script>

</body>
</html>

这是与 smoothState 相关的 css:

/* prefixes are just missing here, I have them in my file */

.m-scene .scene_element {
  animation-duration: .5s;
  transition-timing-function: ease-in;
  animation-fill-mode: both;
}

.m-scene .scene_element--moveUpIn {
  animation-name: moveUp, fadeIn;
}

.m-scene.is-exiting .scene_element {
  animation-direction: alternate-reverse;
}

@keyframes moveUp {
  0% {
    transform: translateY(100%) 
  }
  100% { 
    transform: translateY(0%) 
  }
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

这是更高级的 smoothState:

;(function($) {
  'use strict';
  var $body = $('html, body'),
  content = $('#main').smoothState({

    // Runs when a link has been activated
    onStart: {
      duration: 250, // Duration of our animation
      render: function (url, $container) {

        // toggleAnimationClass() is a public method
        // for restarting css animations with a class
        content.toggleAnimationClass('is-exiting');

        // Scroll user to the top
        $body.animate({
          scrollTop: 0
        });
      }
    }
  }).data('smoothState');
  //.data('smoothState') makes public methods available
})(jQuery);

最终我计划添加prefetchpageCacheSize 等...并根据您正在加载/退出的页面实现不同的转换。但是,在我解决上述问题之前,目前不值得继续前进。

欢迎任何想法或帮助,非常感谢,谢谢。

哦,我也刚遇到这个错误

XMLHttpRequest cannot load file:///Users/Parallelos/Developer/paralellos.github.io/projects.html.
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
  n.ajaxTransport.k.cors.a.crossDomain.send @ jquery.min.js:4
  n.extend.ajax                             @ jquery.min.js:4
  fetch                                     @ jquery.smoothState.js:355
  load                                      @ jquery.smoothState.js:529
  clickAnchor                               @ jquery.smoothState.js:589
  n.event.dispatch                          @ jquery.min.js:3
  n.event.add.r.handle                      @ jquery.min.js:3

最佳答案

我处理过这个完全相同的问题。如果您下载演示并浏览他们的“functions.js”,您会注意到处理现有 css 类的不同方式。这是该代码,已经过测试,对我有用。

$(function(){
  'use strict';
  var $page = $('#main'),
      options = {
        debug: true,
        prefetch: true,
        cacheLength: 2,
        forms: 'form',
        onStart: {
          duration: 250, // Duration of our animation
          render: function ($container) {
            // Add your CSS animation reversing class
            $container.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);
          }
        }
      },
      smoothState = $page.smoothState(options).data('smoothState');

});

关于javascript - content.toggleAnimationClass();不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932989/

相关文章:

javascript - 如果 HTML 值位于包含带有 javascript 的特定 href 的类中,如何编辑 HTML 值?

javascript - 使用 jQuery 删除列表项

javascript - 如何在 php 中使用 while 循环在 div 中换行?

Jquery datepicker 插件仅适用于第一个文本框,不适用于第二个

javascript - 清除 WinJS 绑定(bind) JavaScript 数组

javascript - 当 html 结构不确定时,CSS 选择器选择父级下的第一个和最后一个元素

javascript - 使用 Javascript 自动将年份添加到 HTML 选择

javascript - 如何通过单击 react 路由器 v4 中的按钮在路径上导航?

javascript - 如何同时发送getJson请求并提交输入表单

jquery-ui - 鼠标留在下拉菜单上不起作用 IE