首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AddEventListener Mraid

AddEventListener Mraid
EN

Stack Overflow用户
提问于 2015-09-16 08:29:21
回答 1查看 1.4K关注 0票数 0

嘿,我有点晚了,但我的代码有点问题。

代码语言:javascript
复制
function Orientation()
{


if ( (window.orientation == 0) || (window.orientation == 180) )//portrait
    {

        Paysage.style.visibility = "hidden";
        Portrait.style.visibility = "visible";
        mraid.removeEventListener("stateChange", mraidIsReady);
        mraid.removeEventListener("orientationchange", mraidIsReady);
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);
        //var video = document.getElementById("video");
        //video.pause();

    }
if ( (window.orientation == 90) || (window.orientation == -90) )//paysages
  {
        Portrait.style.visibility = "hidden";
        Paysage.style.visibility = "visible";
        mraid.removeEventListener("stateChange", mraidIsReady);
        mraid.removeEventListener("orientationchange", mraidIsReady);
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);
        //var video = document.getElementById("video");
        //video.play();
    //overlayObj.style.visibility = "";
    //var video = document.getElementById("video");
    //video.play(); 
  }
}

function doReadyCheck()
{   
    if (mraid.getState() == 'loading') 
    {   
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);  
    } 
    else
    {   
        mraid.addEventListener("orientationchange", Orientation);  
        mraid.addEventListener("stateChange", Orientation);         
    }
}
doReadyCheck(); 
</script>

我的EvetListener总是在加载时工作,但在加载之后就不能工作了.

顺便说一句,我有一个在IOS上工作的代码来阻止景观中的定向,但是它不能在Android上工作,为什么?

谢谢!:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-26 11:11:41

我注意到您的代码很少有问题。

  1. 确保您的第一行代码是<script src="mraid.js"></script>,很可能是因为您没有在脚本标记中粘贴整个代码。
  2. 第二,检查mraid是否仍在加载,如果仍在加载,则侦听mraid“就绪”事件。在read事件回调处理程序中,添加其他列表或任何您想要做的与mraid相关的操作。
  3. 第三,当您正在添加事件列表并使用某些回调方法时,但是在删除时您正在删除不同的回调处理程序,例如,如果您添加了这个listner mraid.addEventListener("orientationchange", Orientation);,那么在删除时您应该调用mraid.removeEventListener("orientationchange", Orientation);而不是mraid.removeEventListener("orientationchange", mraidIsReady);
  4. 第四,您不需要在每个状态变化时调用不需要的定向处理程序,只听方位变化。
  5. 第五,并非所有mraid投诉SDK的支持方向更改,如果Ad不支持该更改事件,那么请听 Window.addEventListener(方向变化,取向);

这是代码

代码语言:javascript
复制
<script src="mraid.js"></script>
function Orientation()
{


    if ( (window.orientation == 0) || (window.orientation == 180) )//portrait
    {

        Paysage.style.visibility = "hidden";
        Portrait.style.visibility = "visible";


    }
    if ( (window.orientation == 90) || (window.orientation == -90) )//paysages
    {
        Portrait.style.visibility = "hidden";
        Paysage.style.visibility = "visible";        
    }
}

function doReadyCheck()
{
    if (mraid.getState() == 'loading')
    {
        //Mraid is still loading so listen to ready state change
        mraid.addEventListener("ready", mraidIsReady);

    }
    else
    {
        //Mraid is already ready so do your mraid related stuff here

        //orientationchange event will only be added in case your SDK supports    orientationchange otherwise add
        //window.addEventListener("orientationchange", Orientation);
        mraid.addEventListener("orientationchange", Orientation);
        //This is not needed,why do you need to listen to stateChange as well, but its upto you if you want to do that
        //mraid.addEventListener("stateChange", Orientation);
    }
}
/**
* Mraid is ready, so add your mraid related code here
*/
function mraidIsReady(){
    //Remove the ready listener
    mraid.removeEventListener("ready", mraidIsReady);

    //Now add mraid related listeners
    //orientationchange even will only be added in case your SDK supports orientationchange otherwise add       //window.addEventListener("orientationchange", Orientation);
    mraid.addEventListener("orientationchange", Orientation);

    //I don't know if you really need to do that this is un-necessary, so   every stateChange will trigger orientation check
    //mraid.addEventListener("stateChange", Orientation);
}
doReadyCheck();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32603386

复制
相关文章

相似问题

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