简体   繁体   English

Javascript function 未在页面加载时加载

[英]Javascript function is not loading on page load

I have an animation on my page and the animation only works when I scroll, but I want to run the animation on page load.我的页面上有一个 animation 并且 animation 仅在我滚动时才有效,但我想在页面加载时运行 animation。

The function code is as following: function代码如下:

window.addEventListener("scroll", function () {                      
      var scroll = window.pageYOffset || document.documentElement.scrollTop;
      var elTop = el.getBoundingClientRect().top + scroll - document.documentElement.clientHeight;
      var elBottom = el.getBoundingClientRect().bottom + scroll;
           if ((el.getAttribute('data-effect') === 'false') && (scroll > elTop) && (scroll < elBottom)) {
                        el.setAttribute('data-effect', 'true');
                        if (!notVisible()) {
                            initVisualAnimation();
                        }
                    }               
             }
window.addEventListener('load', (event) => {
  console.log('page is fully loaded');
});

You have added scroll event.. Call the code on page load您已添加滚动事件。在页面加载时调用代码

You can make a function, and call it on page load.您可以制作一个 function,并在页面加载时调用它。

<script type="text/javascript">
    function Animation_Scrolling(){
        var scroll = window.pageYOffset || document.documentElement.scrollTop;
        var elTop = el.getBoundingClientRect().top + scroll -document.documentElement.clientHeight;
        var elBottom = el.getBoundingClientRect().bottom + scroll;
        if ((el.getAttribute('data-effect') === 'false') && (scroll > elTop) && (scroll < elBottom)) {
            el.setAttribute('data-effect', 'true');
            if (!notVisible()) {
                initVisualAnimation();
            }
        }
    }
    window.onload = Animation_Scrolling;
</script>

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

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