javascript - 如何禁用页面加载?

标签 javascript php jquery svg

我的网站上有一个 SVG 动画,它放在页面中间,当我进入我的网站时,页面加载,SVG 动画自动绘制图像,那么当我向下滚动时如何加载 SVG 动画?

<script type="text/javascript">

    (function(){ 

        document.onreadystatechange = () => {

            if (document.readyState === 'complete') {

                /**
                * Setup your Lazy Line element.
                * see README file for more settings
                */

                let el = document.querySelector('#case');

                let myAnimation = new LazyLinePainter(el, {"ease":"easeLinear","strokeWidth":4.6,"strokeOpacity":1,"strokeColor":"#2ECC70","strokeCap":"square","delay":320}); 
                myAnimation.paint();

                let el1 = document.querySelector('#clients');

                let myAnimation1 = new LazyLinePainter(el1, {"ease":"easeLinear","strokeWidth":2.6,"strokeOpacity":1,"strokeColor":"#2ECC70","strokeCap":"square","delay":210});

                myAnimation1.paint();
            }
        }

    })();

</script>

最佳答案

当用户向下滚动时,您可以在滚动事件处理程序中加载它

// jquery solution
var lastScrollTop = 0;
$(window).scroll(function(event){
   var st = $(this).scrollTop();
   if (lastScrollTop != 'loaded' && st > lastScrollTop){
            let el = document.querySelector('#case');

            let myAnimation = new LazyLinePainter(el, {"ease":"easeLinear","strokeWidth":4.6,"strokeOpacity":1,"strokeColor":"#2ECC70","strokeCap":"square","delay":320}); 
            myAnimation.paint();

            let el1 = document.querySelector('#clients');

            let myAnimation1 = new LazyLinePainter(el1, {"ease":"easeLinear","strokeWidth":2.6,"strokeOpacity":1,"strokeColor":"#2ECC70","strokeCap":"square","delay":210});

            myAnimation1.paint();
   }
   lastScrollTop = 'loaded';
});

这应该会在用户第一次向下滚动您的页面时开始加载 svg。

关于javascript - 如何禁用页面加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920903/

相关文章:

javascript - Express js Controller 从 api 服务获取数据并呈现 View

javascript - 如何向 PHP 元素发送 post 请求

javascript - 阻止在 vue.js 中输入某些数字

javascript - JavaScript 中不区分大小写的字符串替换?

php - magento 1.8 使用 php 将产品添加到购物车

PHP用分号替换第一个空格

php - 在 student_ids 到 class_times 的映射中找到常见负空间的最佳方法

javascript - 根据子元素的字符串匹配创建父元素文本的数组

javascript - 检测输入值是否通过 Enter 按下而不是 onclick 选择

jQuery 包含带括号的内容