Javascript阻止CSS?

时间:2016-03-04 15:50:35

标签: javascript css image xmlhttprequest blocking

我正在建造一个灯箱照片库。当第一个图像加载css过渡时。当加载后的每个图像都没有。有什么想法吗?第一次加载后的照片但没有过渡。



Image.prototype.load = function(url){
          var thisImg = this;
          var xmlHTTP = new XMLHttpRequest();
          xmlHTTP.open('GET', url,true);
          xmlHTTP.responseType = 'arraybuffer';
          xmlHTTP.onload = function(e) {
              var blob = new Blob([this.response]);
              thisImg.src = window.URL.createObjectURL(blob);
          };
          xmlHTTP.onprogress = function(e) {
              thisImg.completedPercentage = parseInt((e.loaded / e.total) * 100);
          };
          xmlHTTP.onloadstart = function() {
              thisImg.completedPercentage = 0;
          };
          xmlHTTP.onloadend = function() {
              thisImg.completedPercentage = 0;
              gallery.appendChild(currentImage);
              gallery.className = "gallery toggle";
          };
          xmlHTTP.send();
        };

.gallery {
  display: absolute;
  position: fixed;
  margin: auto;
  visibility: hidden;
  opacity: 0;
  -moz-transition: opacity 0.5s, visibility 0.5s ease-in-out;
  -webkit-transition: opacity 0.5s, visibility 0.5s ease-in-out;
  -ms-transition: opacity 0.5s, visibility 0.5s ease-in-out;
  transition: opacity 0.5s, visibility 0.5s ease-in-out;
  background: rgba(0, 0, 0, 0.0);
  width: 96%;
  height: 96%;
  margin-top: 2%;
  margin-left: 2%;
  overflow: hidden;
  z-index: 1001;
}

.gallery.toggle {
  visibility: visible;
  opacity: 1;
}




2 个答案:

答案 0 :(得分:0)

您的CSS在页面加载时执行。动态添加或删除内容时,不会再次加载CSS。在加载图像后,您可能必须使用JS创建过渡。

也许这会有所帮助:

https://timtaubert.de/blog/2012/09/css-transitions-for-dynamically-created-dom-elements/

答案 1 :(得分:0)

您正在使用类gallery toggle添加图像,这意味着永远不会发生转换;它将被加载到最终状态。您需要使用gallery加载图像,允许DOM渲染,然后添加toggle类。你可以这样做:

xmlHTTP.onloadend = function() {
    thisImg.completedPercentage = 0;
    gallery.appendChild(currentImage);
    gallery.className = "gallery";

    setTimeout(function () {
        gallery.className = 'gallery toggle';
    }, 20);
};