首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript Css动画

JavaScript Css动画
EN

Stack Overflow用户
提问于 2015-12-29 18:21:45
回答 1查看 68关注 0票数 0

我在http://dev17.edreamz3.com/css/有一个Javascript动画

然而,所有的代码都存在性能问题。在桌面上,它的优点是,在移动设备上,速度太慢,无法使用。我想优化动画,使它在移动上顺利运行。动画渲染可能需要20秒或更长时间。

现在,代码的设计方式在js/air.js中,每当滚动事件发生时,就有一个render()函数被执行。问题是这个例程效率不高,这就是我所想的。每次render()执行它遍历迷宫的所有路径和部分并重新绘制它们时,是否有其他方法或策略使其在移动和桌面上都能工作。

代码语言:javascript
复制
var offPathTime = 1000;
window.offSection = -1;
function render() {
    // var top = ($window.scrollTop() + (0.4 * $window.height())) / window.scale;
    var top = ($('.parent-div').scrollTop() + (0.4 * $('.parent-div').height())) / window.scale;
    top -= 660;
    top /= mazeSize.h;

    if (window.offSection != -1) {
        $body.addClass("blockScroll");
        $('.parent-div').addClass("blockScroll");
        // var wtop = $window.scrollTop() / window.scale;
        var wtop = $('.parent-div').scrollTop() / window.scale;
        wtop -= 660;
        wtop /= mazeSize.h;

        var $offSection = $("#offSection" + window.offSection);
        var $section = $("#section" + window.offSection);

        $(".section").removeClass("sectionActive");
        $offSection.addClass("sectionActive");
        $section.addClass("sectionActive");

        var sTop = 200 -(mazeSize.h * (window.offSections[window.offSection].cy - wtop));
        $container.animate({
            left: 290 -(mazeSize.w * window.offSections[window.offSection].cx) + "px",
            top: sTop + "px"
        }, offPathTime);

        // Path
        var lr = offPaths[window.offSection].x1 > offPaths[window.offSection].x0;
        var dx = Math.abs(offPaths[window.offSection].x1 - offPaths[window.offSection].x0);
        var dashw = (dx * mazeSize.w) | 0;

        $offPaths[window.offSection].css("width", "0px");
        $offPaths[window.offSection].show();
        if (lr) {
            $offPaths[window.offSection].animate({
                width: dashw + "px"
            }, offPathTime);
        } else {
            var x0 = offPaths[window.offSection].x0 * mazeSize.w;
            var x1 = offPaths[window.offSection].x1 * mazeSize.w;
            $offPaths[window.offSection].css("left", x0 + "px");
            $offPaths[window.offSection].animate({
                width: dashw + "px",
                left: x1 + "px"
            }, offPathTime);
        }

        return;
    }
    $body.removeClass("blockScroll");
    $('.parent-div').removeClass("blockScroll");
    $(".offPath").hide();
    if ($container.css("top") != "0px") {
        $container.animate({
                left: "-1550px",
                top: "0px"
            }, 500);
    }

    var pathIdx = -1;
    var path0 = paths[0];
    var path1;
    var inPath = 0;
    var i;
    var curTop = 0;
    var found = false;
    for (i=0; i<paths.length; i++) {
        var top0 = (i == 0) ? 0 : paths[i-1].y;
        var top1 = paths[i].y;

        if (top >= top0 && top < top1) {
            pathIdx = i;
            path1 = paths[i];
            inPath = (top - top0) / (top1 - top0);
            found = true;
            if (i > 0) {
                var dy = paths[i].y - paths[i-1].y;
                var dx = paths[i].x - paths[i-1].x;
                var vert = dx == 0;

                if (vert)
                    $paths[i-1].css("height", (dy * mazeSize.h * inPath) + "px");
                $paths[i-1].show();
            }
        } else if (top >= top0) {
            path0 = paths[i];
            var dy = paths[i].y - top0;
            var vert = dy != 0;

            if (i > 0) {
                if (vert)
                    $paths[i-1].css("height", (dy * mazeSize.h) + "px");
                $paths[i-1].show();
            }
        } else {
            if (i > 0) {
                $paths[i-1].hide();
            }
        }

        curTop = top1;
    }

    // Check for an active section
    $(".section").removeClass("sectionActive");
    var section;
    for (i=0; i<sections.length; i++) {
        var d = Math.abs(sections[i].cy - (top - 0.05));
        if (d < 0.07) {
            var $section = $("#section" + i);
            $section.addClass("sectionActive");
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-29 18:43:45

1)至少--将所有DOM对象分配给函数作用域以外的变量。如下所示:

代码语言:javascript
复制
var $parentDiv = $('.parent-div');
var $sections = $(".section");
...
function render() {
   ...

2)还应该在再次执行动画之前停止动画,如下所示:

代码语言:javascript
复制
$container.stop(true).animate({ 
   ...

如果在滚动上运行render()函数,它将每秒运行多次。停止()有助于在某种程度上防止它。

3)如果还不够--您可以从jQuery切换到泽普托(类似jQuery的api,但速度要快得多,并使用css转换用于动画),或者切换到速度(基本上是替代jQuery $.animate,甚至比原来快得多),甚至是GSAP --这显然是更多的工作,但它非常快速,而且功能强大。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34516747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档