2017-02-19 11 views
1

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; 
} 
+0

問題がある最小限のコードスニペットを作成できましたか?今ではそれが大きすぎるので、[animation-fill-mode](https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode)プロパティについて読むことをお勧めします。 –

+1

@ SergeyDenisov - 頭の爪に当たった。そして私はあなたが気付いたように非常に簡単な例を投稿しなかったということを少し不信感に抱いています(それは遅かったです...)。私はアニメーションを新しくしました(明らかに)、私は決してアニメーション・フィル・モードに遭遇したことはありません。あなたがそれが含まれると思うような基本的な設定。ありがとう! – mseifert

+0

'animation-fill-mode'はあなたを助けましたか? –

答えて

0

あなたはあなたのコードにanimation-fill-mode: forwardsを追加する必要があり、それが終了値でアニメーションしておこう。あなたのコード例では、次のようなものになります。

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

@mseifertあなたも答えをupvoteすることができます。 –

関連する問題