简体   繁体   English

iframe和绝对定位的元素

[英]Iframe and absolute positioned elements

I have a small issue with absolute positioned elements inside of iframe. 我在iframe中使用绝对定位元素存在一个小问题。

So I have a page, on the page I have a iframe with small game. 所以我有一个页面,在这个页面上我有一个带有小型游戏的iframe。 In the game is a guy and the guy has absolute positioned pupils. 在游戏中是一个男生,这个男生绝对定位了学生。 The pupils are moving by javascript. 学生们正在用JavaScript移动。 Everything works just fine, but if I open that page, and go ahead and browse other pages in different tabs, After I come back the pupils are on other place than before (completely ut form eyes.) 一切正常,但是如果我打开该页面,然后继续浏览不同标签中的其他页面,我回来之后,学生们的位置就比以前大了(完全是双眼)。

There are more elements positioned absolutely in that iframe and none have the same problem I guess it is because of the Javascript animation. 在iframe中绝对有更多元素定位,我猜没有一个问题是因为Javascript动画。 Did someone encouter similar problem? 有人遇到类似的问题吗? Im sorry but for many reasons I cannot post the page here. 抱歉,由于多种原因,我无法在此处发布页面。

I can post script for pupils animation: 我可以为学生动画发布脚本:

function eyes_sides(){
    $('.eyes').animate({
            left:parseInt($('.eyes').css('left'))-9
        },{duration:1500});
    $('.eyes').animate({
            left:parseInt($('.eyes').css('left'))
        },{duration:1000});
}

function eyes_roll(){
    $('.eyes').animate({
        left:parseInt($('.eyes').css('left'))-7,
        top:parseInt($('.eyes').css('top'))-18
      },{duration:1800});
    $('.eyes').animate({
        left:parseInt($('.eyes').css('left')),
        top:parseInt($('.eyes').css('top'))
      },{duration:1300});
    }

function animate_eyes(){
      var animation = random_number(1,2);
      //alert(animation);
      switch(animation){
        case 1:
          eyes_roll();
          break;
        case 2: 
          eyes_sides();
          break;  
      }
      var delay = random_number(4000,12000);          
      window.animateEyes = setTimeout(function(){animate_eyes()},delay);
    }

The script is not perfect, but does what requires. 该脚本并不完美,但是可以满足要求。

I was thinking what could cause the problem, and maybe it is somehow connected to that animation runs when the tab is not active? 我在想是什么原因引起的,可能是在选项卡不活动时以某种方式连接到了动画的运行? Maybe adding "onBlur" and "onFocus" to stop / start animation would help? 也许添加“ onBlur”和“ onFocus”来停止/启动动画会有所帮助吗?

Thanks for advice! 谢谢你的建议!

This code is supposed to stop the animation. 该代码应停止动画。 At least it triggres onblur on your IFRAME . 至少它triggres onblur你的IFRAME You'll figure out easily, how to start the animation again. 您将轻松地弄清楚如何重新启动动画。

I'm not familiar with jQuery, but you can "translate" the code. 我对jQuery不熟悉,但是您可以“翻译”代码。

window.onload=function (){
    var ifr=document.getElementById('your_iframe');
    (function (x){x.onblur=function (){clearTimeout(x.contentWindow.animatedEyes);return;}}(ifr));
    return; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM