javascript - 在 SmoothState.Js 页面更改后重新初始化页面脚本

标签 javascript jquery ajax smoothstate.js

我正在使用 SmoothState.js 进行页面转换,它工作正常并使用 ajax 加载新页面。但是,我在每个需要重新初始化的页面上都有 JS 脚本,但我无法让它们始终出现在页面转换中。

Based on the FAQ:

smoothState.js provides the onAfter callback function that allows you to re-run your plugins. This can be tricky if you're unfamiliar with how AJAX works.

When you run a plugin on $(document).ready(), it's going to register only on elements that are currently on the page. Since we're injecting new elements every load, we need to run the plugins again, scoping it to just the new stuff.

A good way to do this is to wrap your plugin initializations in a function that we call on both $.fn.ready() and onAfter. You'll want to specify the context each time you initialize the plugins so that you don't double-bind them. This is called a "module execution controller".

我的计划是从我的 JS 文件中获取函数并将它们全部放入 SmoothState.js 文件内的 onAfter 调用中。这样,每次用户更改页面时,这些功能将始终可用。

这是我现在的代码结构。我做了很多挖掘,但我被困住了。你能帮我弄清楚我做错了什么吗?感谢您的宝贵时间!

$(document).ready(function() {
    mail();

});

$('#main').smoothState({
    onAfter: function() {
        mail();
    }
});

function mail() {
    // script from mail.js goes here
}

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

$(function() {
    "use strict";
    var options = {
            prefetch: true,
            pageCacheSize: 3,
            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 = $("#main").smoothState(options).data("smoothState");
});

最佳答案

通过将 onAfter 脚本移动到主 smoothstate 函数(删除其他 smoothstate 函数),我设法在代码末尾运行一个单独的函数。在浏览器刷新的情况下,也可以在文档就绪时运行您的函数。如果它与您的代码一样工作,它将如下所示:

$(document).ready(function() {
        mail();

    });

    function mail() {
        // script from mail.js goes here
    }

    $(function() {
        "use strict";
        var options = {
                prefetch: true,
                pageCacheSize: 3,
                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);


                    }

                },
                onAfter: function() {
                    mail();
                }
            },
            smoothState = $("#main").smoothState(options).data("smoothState");
    });

关于javascript - 在 SmoothState.Js 页面更改后重新初始化页面脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620499/

相关文章:

jquery - Silverlight 视频拉伸(stretch)

javascript - 使用 AJAX 和 PHP 附加加载 gif 文件上传

javascript - 对于动态添加的元素,动画无法正常工作

javascript - 当我通过 postman 收到请求时,它不会向我显示数据,但对于书籍来说它有效

javascript - 如何屏蔽 Lerna 包的内部结构,使其他包无法导入它们?

javascript - 在同步调用之前调用一个函数

javascript - getJSON + reddit api——如何咬它?

javascript - Bing map 问题 --p_elSource.attachEvent 不是函数

javascript - 如何更新网页的一部分而不重定向到新页面? Express、NodeJS、 Mongoose

javascript - AJAX 在 IE 中返回错误的字符集