开始/暂停定时器影片剪辑的视口时, [英] Start/pause timer in MovieClip when in Viewport

查看:134
本文介绍了开始/暂停定时器影片剪辑的视口时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来启动一个定时器我的Flash影片剪辑时,它目前正处于一个HTML页面的视口可见,但不使用JavaScript + ExternalInterface的?也许一个事件或其他把戏?

Is there a way to start a timer in my flash MovieClip when it is currently visible in the viewport of a HTML page without using Javascript + ExternalInterface? Maybe an event or another trick?

我想开始/停止计时时,发生这种情况。

I'd like to start/stop a timer when this happens.

推荐答案

就像你<一个href="http://stackoverflow.com/questions/28989153/ensure-that-throttling-is-supported-in-browser-as3">already发现中的 ThrottleEvent 会告诉你,当Flash播放器已经被扼杀在受支持的情况下,其中包括滚动关闭屏幕和浏览器选项卡切换。

Like you already discovered the ThrottleEvent will tell you when the Flash Player has been throttled in cases where that is supported, which includes scrolling off screen and browser tab switching.

除此之外,您可以手动检测,如果Flash对象使用JavaScript和回调在Flash中是可见的。例如:

In addition to that, you can manually detect if the Flash object is visible using JavaScript and callbacks in Flash. Example:

首先,设置回调在SWF暂停和恢复您的计时器:

First, set up callbacks in your SWF to pause and resume your timer:

var timer:Timer = new Timer(1000);

ExternalInterface.addCallback("pause", timer.pause);
ExternalInterface.addCallback("resume", timer.resume);

接下来,在你的HTML,跟踪HTML视口中您的Flash对象的位置,并在发生变化时调入SWF。下面是这样做的一个很好的例子:<一href="http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433">How判断一个DOM元素是在当前视口中可见?

您处理程序将只需要跟踪Flash对象的可见性变化时,并调用到SWF时,它的作用:

You handler would simply need to track when the Flash object visibility changes and call into the SWF when it does:

function isElementInViewport (element) {

    //special bonus for those using jQuery
    if (typeof jQuery === "function" && el instanceof jQuery) {
        el = el[0];
    }

    var rect = el.getBoundingClientRect();

    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
        rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
    );
}

$(window).on('DOMContentLoaded load resize scroll', handler); 

var flashIsVisible = false;
function handler(e){
    var flash = document.getElementById("myFlash");
    var isVisible = isElementInViewport(flash);
    if(isVisible != flashIsVisible){
        flashIsVisible = isVisible;
        isVisible ? flash.resume() : flash.pause();
    }
}

这篇关于开始/暂停定时器影片剪辑的视口时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆