如何让我的 Flash 对象专注于负载? [英] How do I make my flash object get focus on load?

查看:21
本文介绍了如何让我的 Flash 对象专注于负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的 Flash 游戏设置此测试页面,但它拒绝关注加载.我读了一堆论坛条目,但没有做任何事情,我真的不敢相信这会这么难.

这是我所拥有的:

 <title>我们在这里逃离老鼠赛跑</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script><script type="text/javascript">swfobject.embedSWF("UpHere.swf", "myContent", "700", "300", "9.0.0");函数 setFocusOnFlash() {var fl = document.getElementById("myContent");if (fl) { fl.focus();}}<body onload="setFocusOnFlash()"><div style="margin:0 auto; text-align:center; width:700px;"><div id="myContent" style="margin:0 auto; text-align:center; width:700px;"><p>替代内容</p>

你可以在这里看到它,http://joon.be/exclusivepreview/

这是怎么回事?我对 swfObject 没有很深入的了解...

解决方案

我找到了一种适用于 Firefox 16、Chrome 23 和 IE 8 的方法(目前我已经测试过这些).当然,这是一堆黑客,所以谁知道它是否会永远有效......但它肯定不会让事情变得更糟.

function setFocusOnFlash() {var flash = document.getElementById("theIdOfTheObjectElement");flash.tabIndex = 1234;//这在 Chrome 23 上是需要的flash.focus();//注意:FireFox 需要 wmode "opaque"!}

仅在 Firefox 上,还需要 <param name="wmode" value="opaque">object 元素下,否则 focus() 没有效果.(我使用了 Stephen Belanger 的 jquery.flash,您可以在其中指定 wmode;我认为 SWFObject 也可以.)

但更棘手的部分是您不能过早调用 setFocusOnFlash.对于 Chrome 和 IE,在插入对象的 JavaScript 工作完成后直接添加 setTimeout(setFocusOnFlash, 1).直接发出 setFocusOnFlash() 没有.我认为这个技巧很简单,定时回调只在浏览器完全处理完文档更改后才被调用,而不管您指定的延迟如何.但是在 Firefox 上以这种小延迟调用还为时过早;它在 object 元素周围放置了一个虚线边框(它不应该)并且 Flash 没有得到击键.将延迟设置为 250 已在我的计算机上解决了这个问题,但谁知道您需要多大的延迟.(更糟糕的是,重复 setFocusOnFlash 调用也无济于事……一旦那个虚线边框出现,它们就没有进一步的效果.)所以,我所做的是添加一个 ExternalInterface.call("flashLoaded") 回调flash 文档类构造函数.需要明确的是,您在 Flash/ActionScript 中执行此操作,因此您需要访问源或 SWF 文件的作者.这样,当 SWF 启动时,它会调用嵌入 HTML 页面的 flashLoaded JavaScript 方法,因此您知道它已准备就绪.功能如下:

function flashLoaded() {//奇怪的是,直接调用setFocusOnFlash()在IE8上不起作用setTimeout(setFocusOnFlash, 1);}

I've been trying to set up this test page for my flash game but it refuses to gain focus on load. I read a bunch of forum entries and didn't get it to do anything, I can't really believe this should be so hard.

Here's what I have:

  <head>
<title>UP HERE WE ESCAPE THE RAT RACE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
    swfobject.embedSWF("UpHere.swf", "myContent", "700", "300", "9.0.0");

    function setFocusOnFlash() { 
    var fl = document.getElementById("myContent"); 
      if (fl) { fl.focus(); } 
    } 
</script>
  </head>
  <body  onload="setFocusOnFlash()">
  <div style="margin:0 auto; text-align:center; width:700px;">
    <div id="myContent" style="margin:0 auto; text-align:center; width:700px;">
      <p>Alternative content</p>
    </div>
     </div>
      </body>

You can see it live here, http://joon.be/exclusivepreview/

what's wrong with it? I don't have a very deep knowledge of swfObject...

解决方案

I have found a way that works for me on Firefox 16, Chrome 23 and IE 8 (these are where I have tested it so far). Of course, this is a bunch of hacks so who knows if it will work forever... but it certainly doesn't make things worse.

function setFocusOnFlash() {    
    var flash = document.getElementById("theIdOfTheObjectElement");
    flash.tabIndex = 1234;  // This was needed on Chrome 23
    flash.focus();
    // Attention: FireFox needs wmode "opaque"!
}

On Firefox only, <param name="wmode" value="opaque"> under the object element was also needed, or else focus() had no effect. (I have used Stephen Belanger's jquery.flash, where you can specify wmode; I assume it's also possible with SWFObject.)

But the trickier part is that you must not call setFocusOnFlash too early. For Chrome and IE, adding setTimeout(setFocusOnFlash, 1) directly after the JavaScript that inserts the object has worked. Directly issuing setFocusOnFlash() didn't. I assume the trick is simply that the timed callbacks are only called after the browser has fully processed the document change, regardless of the delay you specify. But on Firefox calling with this small delay was too early; it has put a dotted border around the object element (it shouldn't) and Flash didn't get the key strokes. Setting the delay to 250 has fixed this on my computer, but who knows how big delay you need. (Worse, repeating the setFocusOnFlash calls didn't help either... once that dotted border was there, they had no further effect.) So, what I did instead is adding an ExternalInterface.call("flashLoaded") callback to the flash document class constructor. To be clear, you do that in Flash/ActionScript, so you need access to the source or to the author of the SWF file. This way, when the SWF starts, it calls the flashLoaded JavaScript method of the embedding HTML page, so you know it's ready. The function was like:

function flashLoaded() {
    // Oddly, directly calling setFocusOnFlash() didn't work on IE8
    setTimeout(setFocusOnFlash, 1);
}

这篇关于如何让我的 Flash 对象专注于负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆