首页 > 解决方案 > 声明的函数 ($.fn) 不是函数

问题描述

我试图让一个乐蒂动画开始,当在视野中时。我声明了一个函数

$.fn.isOnScreen = function(){

var win = $(window);

var viewport = {
    top : win.scrollTop(),
    left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();

var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();

return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};

然后调用它

jQuery(document).ready(function(){
    jQuery(window).scroll(function(){
        if (jQuery('#circle-anim').isOnScreen()) {
           
           var circle = lottie.loadAnimation({
            container: document.getElementById("anim-badge-1"),
            renderer: "svg",
            loop: true,
            autoplay: true,
            path: "PATH TO JSON" // the path to the animation json 
});
  
        circle.playSegments(
  [
    [0, 30],
    [162, 575]
  ],
  true
);


        var anim;

        anim = lottie.loadAnimation(circle);
           
            alert("in viewport!");
        } else {
            console.log('not visible');
        }
    });
});

我一滚动我就得到

未捕获的类型错误:jQuery(...).isOnScreen 不是
(index):102 处的函数在调度 (jquery-3.5.1.min.dc5e7f18c8.js?site=60ec2441446e3d0b9389fa3c:2) 在 v.handle (jquery-3.5 .1.min.dc5e7f18c8.js?site=60ec2441446e3d0b9389fa3c:2)

任何想法为什么isOnScreen不起作用?

标签: jqueryfunctionlottie

解决方案


推荐阅读