在所有浏览器中,Flash 游戏之上的 Javascript 模式

发布于 2024-07-25 19:53:20 字数 446 浏览 7 评论 0原文

所以我们在工作中遇到了一个奇怪的问题。 我们需要显示一个使用键盘的 Flash 游戏,当游戏结束时,它会通过 Javascript 触发模式弹出窗口。 我们使用设置为 opaque 的 wmode 参数,以便模式显示在 Flash 元素的顶部。

除 IE6/7 外,一切正常。 当您按下键盘上的向上或向下键时,它会上下滚动页面,并将键盘输入发送到游戏。

我们似乎找到的唯一解决办法是从 object 标记中完全删除 classid 属性。 但这会导致 Flash 元素在 IE6/7 中显示在 Javascript 模式的顶部。

所以看来如果我们想支持 IE,我们只能选择其中之一,而不能同时选择两者。

任何帮助、提示、指示或任何东西都会很棒,因为网上关于这些问题的信息并不多。 至少谷歌不这么认为。

So we have a weird issue at work. We need to display a Flash game which uses the keyboard, when the game finishes, it triggers a modal popup via Javascript. We're using the wmode param set to opaque so that the modal displays on top of the Flash elment.

All works fine, except in IE6/7. When you press the UP or DOWN keys on the keyboard, it scrolls the page up and down, as well as sending the keyboard input to the game.

The only fix we seem to have found for this, is to remove the classid attribute completely from the object tag. But that causes the Flash element to display on top of our Javascript modal in both IE6/7.

So it seems we can only have one or the other, but not both if we wanna support IE.

Any help, hints, pointers or anything would be great, cause there's not really much available online about these kinda issues. At least not according to Google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腻橙味 2024-08-01 19:53:20

我不确定这是最好的解决方案,但您可以考虑在浏览器(或仅在 IE)中绑定键盘事件,这样只要访问者正在玩游戏,它们就不会触发。 onkeyup、onkeydown 和 onkeypress 是需要查看的事件。 这样您就可以在其中放置对象标签,同时防止默认的滚动行为。

希望这可以帮助!

I'm not sure this is the best solution, but you could look into binding the keyboard events in the browser (or just in IE) so they don't fire as long as the visitor is playing the game. onkeyup, onkeydown and onkeypress are the events to look at. This way you can have the object tag in there, while preventing the default scrolling behaviour.

Hope this helps!

紫﹏色ふ单纯 2024-08-01 19:53:20

好的,
经过 2 天的网络搜索答案后,我发现了一个纯 JS 函数,可以在所有浏览器中修复它!

就这样:

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

现在您可以在页面加载时使用 jQuery 运行:

 $(document).ready(function () {
            fix_flash();    
 }

ok,
after 2 days of searching the web for the answer i've found a pure JS function that fix it in all browsers!

there you go:

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

now you can just run in when the page loads with jQuery:

 $(document).ready(function () {
            fix_flash();    
 }
小嗷兮 2024-08-01 19:53:20

一个简单的解决方案是将页面上 body/html 元素的溢出样式切换为隐藏。 这会禁用页面滚动,但应该保持您在页面上的位置。

//toggle this on and off when showing the window
document.body.style.overflow = 'hidden';

A simple solution would to be to toggle the overflow style for the body/html element on your page to hidden. This disables the page from scrolling, but should maintain your position on the page.

//toggle this on and off when showing the window
document.body.style.overflow = 'hidden';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文