为什么在定义时返回revealPoint是未定义的

时间:2017-07-19 00:49:08

标签: javascript animation console

Script

                 var pointsArray = document.getElementsByClassName('point');

                 var animatePoints = function(points) {

                   var revealPoint = function(index) {

                        points[index].style.opacity = 1;
                        points[index].style.transform = "scaleX(1) translateY(0)";
                        points[index].style.msTransform = "scaleX(1) translateY(0)";
                        points[index].style.WebkitTransform = "scaleX(1) translateY(0)";
                   }
                 }

                 for (i = 0; i < pointsArray.length; i++)

                 {
                    revealPoint(i);
                 }

                 window.onload = function() {
                 var sellingPoints = document.getElementsByClassName("selling-points")[0];
                  var scrollDistance = sellingPoints.getBoundingClientRect().top - window.innerHeight + 200;

                  window.addEventListener("scroll", function(event) {
                    if (document.documentElement.scrollTop || document.body.scrollTop >= scrollDistance)
                    {
                      animatePoints(pointsArray);
                    }

                  });
}

控制台显示在定义时,revealPoint未定义。

3 个答案:

答案 0 :(得分:0)

在animatePoints的函数范围中定义的revealPoint,而不是在它被调用的for循环中。

答案 1 :(得分:0)

哪个执行上下文未定义? revealPoint似乎只在animatePoints的上下文中定义。它只能从该函数中访问,并且不会从那里返回。

答案 2 :(得分:0)

var animatePoints = function(points){

var revealPoint = function(index){

                points[index].style.opacity = 1;
                points[index].style.transform = "scaleX(1) translateY(0)";
                points[index].style.msTransform = "scaleX(1) translateY(0)";
                points[index].style.WebkitTransform = "scaleX(1) translateY(0)";

           }
              for (i = 0; i < points.length; i++)
            {
                revealPoint(i);
            }
       };