简体   繁体   English

滚动加载外部脚本

[英]loading external script on scroll

There few external scripts loading on my website.我的网站上加载的外部脚本很少。 for example, this Facebook Page Like Widget script.例如,这个 Facebook Page Like Widget 脚本。 I don't want to load it until the user scrolls down to a certain position or at least a user scrolls.我不想在用户向下滚动到某个位置或至少用户滚动之前加载它。 How can I achieve this with jquery or ajax or something?我怎样才能用 jquery 或 ajax 或其他东西来实现这一点?

connect.facebook.net/en_US/sdk.js

             <script>
                    (function() {
                        var loadFB = function (d, s, id) {
                            var js, fjs = d.getElementsByTagName(s)[0];
                            if (d.getElementById(id)) return;
                            js = d.createElement(s);
                            js.onload = function() {
                                // After FB Page plugin is loaded, the height of its container changes.
                                // We need to notify theme about that so elements like eg. sticky widgets can react
                                FB.Event.subscribe('xfbml.render', function () {
                                    jQuery('body').trigger('g1PageHeightChanged');
                                });
                            };
                            js.id = id;
                            js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5";
                            fjs.parentNode.insertBefore(js, fjs);
                        };

                                                loadFB(document, 'script', 'facebook-jssdk');
                                            })();
                </script>

您可以为此参考文档使用滚动事件

You can try to add an event that triggers when You reach a certain div:您可以尝试添加到达某个 div 时触发的事件:

$(window).scroll(function() {
       var heightT = $('.your-div').offset().top,
           outerH = $('.your-div').outerHeight(),
           windowH = $(window).height(),
           wS = $(this).scrollTop();

       if (wS > (heightT+outerH-windowH-200)){
          //add Your script here
       }
});

By calculating the screen height and the place of the div.通过计算屏幕高度和div的位置。

Don't have all Your code so can only guide You to the solution of "how to".没有你的所有代码,所以只能指导你解决“如何”的问题。

Please attach the rest if You need more help!如果您需要更多帮助,请附上其余部分!

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

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