仅在滚动后画布可见后才启动动画(Animate CC)。

时间:2018-11-01 14:59:13

标签: javascript adobe-animate

我有一个动画(使用Adobe animate CC制作),该动画已包含到html文件中。我试图更改其原始脚本,以便仅在动画在浏览器的视口中可见时才从头开始播放动画。不幸的是,我进行了广泛搜索,但无法找到适合我的情况的任何解决方案。

这是我要更改的原始代码:

<html>

<head>

<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, 
fnStartAnimation;

function init() {
    canvas = document.getElementById("canvas");
    anim_container = document.getElementById("animation_container");
    dom_overlay_container = document.getElementById("dom_overlay_container");
    var comp=AdobeAn.getComposition("07578064E5C140B781D146A1AE4968A3");
    var lib=comp.getLibrary();
    handleComplete({},comp);
}
function handleComplete(evt,comp) {
    //This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
    var lib=comp.getLibrary();
    var ss=comp.getSpriteSheet();
    exportRoot = new lib.Water_illustrazione_revista_scrollplay();
    stage = new lib.Stage(canvas);  
    //Registers the "tick" event listener.
    fnStartAnimation = function() {
        stage.addChild(exportRoot);
        createjs.Ticker.setFPS(lib.properties.fps);
        createjs.Ticker.addEventListener("tick", stage);
    }

 //follow a code to support hidpi screens and responsive scaling.

</script>

</head>

<body onload="init();" style="margin:0px;">

<div id="anchor5" class="Anime_p">
 <div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:1280px; height:700px">
        <canvas id="canvas" width="1280" height="700" style="position: absolute; display: block; background-color:rgba(255, 255, 255, 1.00);"></canvas>
        <div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:1280px; height:700px; position: absolute; left: 0px; top: 0px; display: block;">
        </div>
    </div>
</div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

您只需要首先将动画导出为停止,然后检查div何时进入浏览器视图。只要有,就开始动画。

    在fnStartAnimation()的末尾
  1. 添加 exportRoot.stop();
  2. 按照以下说明检查浏览器中动画div的可见性-How to tell if a DOM element is visible in the current viewport?
  3. 只要满足条件,就添加代码-exportRoot.play();