javascript - 动态添加变换 - 最后恢复到原始值

标签 javascript css transform

我试图动态添加一个 transform:translate3d 来移动 div,但在转换结束时,div 会恢复到原始位置。我正在添加一个新的样式表并使用 insertRule()。有什么想法可能导致此问题吗?

这里是a fiddle :

slideshow = (function() {
  function slideshow() {
    var _this = this;
    this.divMiddle;
    this.divLarge = []; // holds full sized image
    this.largeSize = [];
    this.slide = -1;
    this.oldSlide = -1;
    this.img = [];

    this.divMiddle = document.createElement("div");
    this.divMiddle.id = "divMiddle";
    this.divMiddle.style.position = "relative"
    this.divMiddle.style.width = "500px";
    this.divMiddle.style.height = "400px";
    this.divMiddle.style.border = "1px solid black";
    document.body.appendChild(this.divMiddle);
    document.getElementById("next").addEventListener("click", function() {
      _this.transitionSlides(1)
    });
    document.getElementById("prev").addEventListener("click", function() {
      _this.transitionSlides(-1)
    });

    this.oldSlide = -1;
    this.slide = -1;
    this.transitionSlides(1);
  }

  // transition between slides - fading new one in and old one out
  slideshow.prototype.transitionSlides = function(direction) {
    // if not the first one loaded, will do transition
    var begin, end;
    this.oldSlide = this.slide;
    this.slide += direction;
    if (this.slide > this.oldSlide) {
      this.createImage();
    }
    if (CSSRule.KEYFRAMES_RULE) {
      var prefix = ""
    } else if (CSSRule.WEBKIT_KEYFRAMES_RULE) {
      var prefix = "-webkit-"
    }
    var style = document.createElement("style");
    document.head.appendChild(style);

    // fade out the old slide
    if (this.oldSlide >= 0) {
      // fade(this.divLarge[this.oldSlide], 1, .1, 20);      
      this.divLarge[this.oldSlide].style.opacity = ".3";

      var oldMargin = (parseInt(this.divMiddle.style.width) - parseInt(this.largeSize[this.oldSlide].displayWidth)) / 2;
      begin = 0;
      if (direction == 1) {
        // next
        // move existing slide from left to right
        end = (parseInt(this.largeSize[this.oldSlide].displayWidth) + oldMargin);
      } else {
        // prev
        // move existing slide from right to left
        end = -(parseInt(this.largeSize[this.oldSlide].displayWidth) + oldMargin + 1);
      }
      var rule = "@" + prefix + "keyframes moveOld {" +
        "0% {" + prefix + "transform: translate3d(" + begin + "px, 0, 0); }" +
        "100% {" + prefix + "transform: translate3d(" + end + "px, 0, 0); }" +
        "}";
      style.sheet.insertRule(rule, 0);

      var rule2 = "#" + this.divLarge[this.oldSlide].id + "{" + prefix + "animation: moveOld 3s linear" + "}";
      style.sheet.insertRule(rule2, 0);

    }
    // fade in the new slide
    //fade(this.divLarge[this.slide], .1, 1, 20);
    this.divLarge[this.slide].style.opacity = "1";
    this.divLarge[this.slide].style.display = "block";

    var margin = (parseInt(this.divMiddle.style.width) - parseInt(this.largeSize[this.slide].displayWidth)) / 2;
    begin = 0;
    if (this.oldSlide == -1) {
      // first slide - don't move
      this.divLarge[this.slide].style.left = margin + "px";
      end = 0;
    } else if (direction == 1) {
      // next
      // move new slide from left to right
      // move to starting position
      this.divLarge[this.slide].style.left = (oldMargin - parseInt(this.largeSize[this.slide].displayWidth)) + "px";
      end = (-oldMargin + parseInt(this.largeSize[this.slide].displayWidth) + margin);
    } else {
      // prev
      // move new slide from right to left
      // move to starting position
      this.divLarge[this.slide].style.left = (oldMargin + parseInt(this.largeSize[this.oldSlide].displayWidth)) + "px";
      end = +oldMargin - (parseInt(this.largeSize[this.slide].displayWidth) + margin);
    }
    var rule = "@" + prefix + "keyframes moveNew {" +
      "0% {" + prefix + "transform: translate3d(" + begin + "px, 0, 0); }" +
      "100% {" + prefix + "transform: translate3d(" + end + "px, 0, 0); }" +
      "}";
    style.sheet.insertRule(rule, 0);

    var rule2 = "#" + this.divLarge[this.slide].id + "{" + prefix + "animation: moveNew 2s linear" + "}";
    style.sheet.insertRule(rule2, 0);
  }

  slideshow.prototype.createImage = function() {
    var img = document.createElement("img");
    var from = 100;
    var to = 300;
    var width = Math.floor(Math.random() * (to - from + 1) + from);
    var height = Math.floor(Math.random() * (to - from + 1) + from);
    img.src = "http://placehold.it/" + width + "x" + height;
    img.style.height = height;
    img.style.width = width;
    this.img.push(img);

    var div = document.createElement("div");
    div.id = "slideshow-large-" + (this.img.length - 1)
    div.style.display = "none";
    div.style.top = ((parseInt(this.divMiddle.style.height) - height) / 2) + "px";
    div.style.position = "absolute";
    div.style.width = width + "px";
    div.className = "slideshow-large";
    div.appendChild(img);

    this.divLarge.push(div);
    this.largeSize.push({
      displayWidth: width,
      displayHeight: height
    });
    this.divMiddle.appendChild(div);
  }

  return slideshow;

})();

var ss = new slideshow();

样式和 HTML

<div id="next">Next Slide</div>
<div id="prev">Prev Slide</div>


#divMiddle {
  width: 600px;
  height: 600px;
  display: flex;
  position: relative;
  top: 0px;
  left: 0px;
  overflow: hidden;
}

div.slideshow-large {
  overflow: hidden;
  top: 0;
  position: absolute;
}

div.slideshow-large img {
  display: block;
}

div.slideshow-large span {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1070;
}

最佳答案

您应该添加animation-fill-mode: forwards对于您的代码,它会将动画保持在最终值。对于您的代码示例,它会是这样的:

...
var rule2 = "#" + this.divLarge[this.slide].id + "{" + prefix + "animation: moveNew 2s linear forwards" + "}";
...

关于javascript - 动态添加变换 - 最后恢复到原始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42326364/

相关文章:

javascript - 在 Immutable.js 中深入查找

c++ - 计算横向拉伸(stretch)对象的比例和位置

javascript - 悬停时包括有关对象 PVIZ 的更多详细信息

html - overflow-y 滚动不适用于嵌套在 div 中的 ul

css - 无法在检查时更改单选按钮 css 样式

html - 旋转div后如何设置边距自动适合顶部

java - Clojure JUNG setVertexFillPaintTransformer

javascript - 有没有一种简单的方法来判断 json 对象是否包含属性?

javascript - 我的 Angular 应用程序中 BehaviorSubject 的混淆行为

javascript - 无法在 pubnub javascript 回调中收到错误