2016-04-05 4 views
0

トランジションに問題があります。イメージを含むdivは「right:200px;」から移動する必要があります。ここイメージをロードした後でトランジションイベントを開始する

div.contenedor { 
     width: 300px; 
     height: 257px; 
     position: fixed; 
     right: 200px; 
     bottom: 0px; 
     z-index: 100; 
     -webkit-transition: right 2s; 
     -moz-transition: right 2s; 
     transition: right 2s; 
    } 

とJSコード:

 function transi() { 
      var id = "L" + Math.floor(Math.random() * 10000); 
      function open() { 
       var eOpen = document.getElementById(id + "_container"); 
        eOpen.style.right = "400px"; 
      }; 

      var dContenedor = document.createElement("div"); 
      dContenedor.id = id + "_container"; 
      dContenedor.className = "contenedor"; 

      var ima = document.createElement("img"); 
      ima.src = "XXXX.jpg"; 
      ima.width = "300"; 
      ima.height = "250"; 

      dContenedor.appendChild(ima); 

      document.onreadystatechange = function(){ 
       var rsw = this.readyState; 
       console.log(rsw); 
       if (rsw) if (rsw != 'complete') if (rsw != 'loaded') {return;} 
       document.body.appendChild(dContenedor); 
       console.log(ima.complete); 
       if (ima.complete) { 
        open(); 
       }; 
      } 
     }; 
     transi(); 

については2Sでの推移と画像が文書にロードされたときに開始する必要が移行する「400ピクセル」に、ここでCSSのコードがあります何らかの理由で移行が行われておらず、divが直接「400px」に向かいます。誰かが考えていることがあれば、私はそれを感謝します。

+0

それはあなたが要素にトランジションを設定しましたが、あなたはJS 'オープン()'関数に移動しているものと思われますその要素のコンテナです。 – KWeiss

+0

'document.querySelector().onload = function(){...}' –

+0

いいえ、同じ要素です.dContenedorはトランジションを持ち、open()を呼び出すとその要素 " document.getElementById(id + "_container"); " "style.right"を400に変更してください。 はい、私はonloadを試してみましたが、同じ結果が得られました。 私は最近、 "setTimeout(function(){open(); 3000);のように" setTimeout() "の中にopen()今、移行が表示されます、なぜですか?私は考えていない... –

答えて

2

多分、loadイベントでコール機能がありますか?

UPD を追加しました作業例

function transi() { 
 
    var id = "L" + Math.floor(Math.random() * 10000); 
 
    var dContenedor = document.createElement("div"); 
 
    dContenedor.id = id + "_container"; 
 
    dContenedor.className = "contenedor"; 
 

 
    function open() { 
 
    // we already have ref to needed element 
 
    dContenedor.style.right = "400px"; 
 
    }; 
 

 
    var ima = document.createElement("img"); 
 
    ima.width = "300"; 
 
    ima.height = "250"; 
 

 
    dContenedor.appendChild(ima); 
 
    document.body.appendChild(dContenedor); 
 
    // when image is loaded fire event 
 
    ima.onload = function() { 
 
    open(); 
 
    } 
 
    ima.src = "https://v1.std3.ru/32/9b/1455811008-329b55c554efcec3988dc8ab44eb972f.jpeg"; 
 
}; 
 
transi();
div.contenedor { 
 
    width: 300px; 
 
    height: 257px; 
 
    position: fixed; 
 
    right: 200px; 
 
    bottom: 0px; 
 
    z-index: 100; 
 
    -webkit-transition: right 2s; 
 
    -moz-transition: right 2s; 
 
    transition: right 2s; 
 
    background: #333; 
 
    }

+0

はい、私はそれを試してみましたが、同じ結果で –

+0

私はそれが私のテストページで動作しなかった理由を参照してください、私はにスクリプトに入れて一度私は完璧、やっぱり@ThisMan –

関連する問題