简体   繁体   English

Vuejs来回过渡跳到背面的最后一帧

[英]Vuejs back and forth transition skipping ahead to last frame on back

I'm creating a slider element of 2 items only. 我正在创建仅2个项目的滑块元素。 I want them to smoothly slide back and forth to the left and right when I click back / next. 我希望它们在我单击“后退” /“下一步”时可以平稳地前后左右滑动。 It works well when I click next, elements scroll and fade, but when I click back, the first element jumps ahead in time no matter what I do, this gif explains a bit more visually: 当我单击下一步时,它运行良好,元素滚动并消失,但是当我单击返回时,无论我做什么,第一个元素都会在时间上跳前,此gif从视觉上进行了解释:

在此处输入图片说明

Edit: Here is a fiddle - http://jsfiddle.net/eywraw8t/273583/ 编辑:这是一个小提琴-http: //jsfiddle.net/eywraw8t/273583/

This is my css: 这是我的CSS:

.first-slide-enter{
  opacity: 0;
  transform: translatex(-100%);
  transition: all 1.5s ease-out;
}
.first-slide-enter-to{
  opacity: 1;
  transform: translatex(0);
  transition: all 1.5s ease-out;
}
.first-slide-leave-to{
    opacity: 0;
    transform: translatex(-100%);
    transition: all 1.5s ease-out;
}


.second-slide-enter{
  opacity: 0;
  transform: translatex(0);
  transition: all 1.5s ease-out;
}
.second-slide-enter-to{
    opacity: 1;
    transform: translatex(-100%);
    transition: all 1.5s ease-out;
}
.second-slide-leave-to{
    opacity: 0;
    transform: translatex(0);
    transition: all 1.5s ease-out;
}

This is my html: 这是我的html:

        <transition name="first-slide">
            <div v-if="!newShortcut.link">

                <div id="drop-shortcut" class="drag-file clickable" @click="addShortcut">
                    <i class="fas fa-file-upload fa-lg"></i>
                    <p style="margin:20px 0 0;">Drag file here
                        <br> or click to browse</p>
                </div>                      
                <div>
                        <button @click="newShortcut.link = !newShortcut.link">Next</button>
                </div>

            </div>
        </transition>

        <transition name="second-slide">
            <div v-if="newShortcut.link">
                <div id="drop-icon" class="drag-file" @click="">
                    <i class="far fa-file-image fa-lg"></i>
                    <p style="margin:20px 0 0;">Drag file here
                        <br> or click to browse</p>
                </div>
                <div>
                    <button @click="newShortcut.link = !newShortcut.link">back</button>
                </div>
            </div>
        </transition>

How to make this work? 如何使这项工作?

The issue was because of how your elements are displayed. 问题是由于元素的显示方式。 Each slide is a block , so it occupies a space in your layout. 每张幻灯片都是一个block ,因此在您的布局中占据了一定的空间。

So when you were at the second slide and click the back button. 因此,当您在第二张幻灯片上并单击“后退”按钮时。 What happens before that is, there was no slide 1 in the layout and when vue starts to insert the slide 1 back, it occupies a space which causes the second slide to suddenly jump from left to right. 在此之前发生的是,布局中没有幻灯片1,当vue开始将幻灯片1插入时,它占据了一个空间,该空间导致第二张幻灯片突然从左跳到右。

So to fix that behavior, you can make each slide's position as absolute so that if they are inserted back, they will not cause such behavior because absolute position will not affect it's neighboring element's position unlike the relative position. 因此,要解决该问题,您可以将每张幻灯片的位置设为absolute位置,这样,如果将它们插入回去,它们就不会引起这种行为,因为absolute位置不会像relative位置那样影响其相邻元素的位置。

 #app>div { position: absolute; height: 200px; width: 200px; text-align: center; left: 0; top: 0; } 

and update the css transition for the second slide a little bit: 并更新第二张幻灯片的CSS过渡:

 .second-slide-enter { opacity: 0; transform: translatex(100%); transition: all 1.5s ease-out; } .second-slide-enter-to { opacity: 1; transform: translatex(0); transition: all 1.5s ease-out; } .second-slide-leave-to { opacity: 0; transform: translatex(100%); transition: all 1.5s ease-out; } 

Here's the full JS Fiddle: http://jsfiddle.net/eywraw8t/273631/ 这是完整的JS小提琴: http : //jsfiddle.net/eywraw8t/273631/

So @Julian is correct for the blocking issue, but there is another way to fix it that might work for you better. 因此,@ Julian对于阻止问题是正确的,但是还有另一种修复它的方法可能会对您更好。 See this fiddle : http://jsfiddle.net/xcmn76Lo/ 看到这个小提琴: http : //jsfiddle.net/xcmn76Lo/

Basically what was needed was some tweaking to the second slide. 基本上需要对第二张幻灯片进行一些调整。 There is a hook you can use similar to enter to specify where it starts leaving from. 您可以使用类似的钩子来enter以指定钩子从何处开始。 In this case since the other div reserves its space immediately, you need to start the leave transition offsetting appropriately. 在这种情况下,由于另一个div立即保留其空间,因此您需要适当地开始偏移过渡。

.second-slide-leave{
  transform:translatex(-100%);
}

Then instead of transition to 100%, set leave-to to 0 (aka where it WOULD end up if moved over by the incoming div). 然后,而不是过渡到100%,集leave-to 0(又名它最终会如果由输入格上移动)。

.second-slide-leave-to{
    opacity: 0;
    transform: translatex(0);
    transition: all 1.5s ease-out;
}

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

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