如何使用CSS同时滑动两个元素?

时间:2018-09-17 18:01:30

标签: javascript html css vue.js css-transitions

我正在尝试创建一个带有2个按钮的滑块(向上和向下箭头),当用户单击向上箭头时,滑块中的当前元素(居中)会向上滑动并淡入淡出(例如,隐藏或不透明)变为0),然后下一个元素从屏幕底部向上滑动,直到不透明度变为1。

但是我似乎无法仅使用CSS使其工作。我设法使用javascript(Vuejs)复制了它,但是效率很低。

<template>
  <transition appear>
    <div v-if="condition" class="trans-container">
      <slot></slot>
    </div>
  </transition>
</template>
<script>
  import velocity from 'velocity-animate'
  export default {
    name: "Slide",
    props: {
      condition: {
        default: true,
        type: Boolean
      },
      direction: {
        type: String
      }
    },
    methods: {
      // --------
      // ENTERING
      // --------

beforeEnter: function (el) {
        // el.style.opacity = 0
        Velocity(el, {
          translateY: this.direction === 'up' ? '100%' : '-100%',
          opacity: 0
        }, {
          duration: 300
        });
      },
      // the done callback is optional when
      // used in combination with CSS
      enter: function (el, done) {
        Velocity(el, {
          translateY: '0%',
          opacity: 1
        }, {
          duration: 300
        });
        // ...
        // done()
      },
      afterEnter: function (el) {

      },
      enterCancelled: function (el) {
        // ...
      },

      // --------
      // LEAVING
      // --------

      beforeLeave: function (el) {
        Velocity(el, {
          translateY: this.direction === 'down' ? '100%' : '-100%',
          opacity: 0
        }, {
          duration: 300
        });
        // ...
      },
      // the done callback is optional when
      // used in combination with CSS
      leave: function (el, done) {
        Velocity(el, {
          translateY: this.direction === 'down' ? '100%' : '-100%',
          opacity: 0
        }, {
          duration: 300
        });
        // ...
        // done()
      },
      afterLeave: function (el) {
        // ...
      },
      // leaveCancelled only available with v-show
      leaveCancelled: function (el) {
        // ...
      }
    }
  }
</script>
<style scoped>
  .trans-container {
    width: 100%;
    display: flex;
    justify-content: space-between;
    position: absolute;
    align-items: center;
  }
</style>

我只是无法使用CSS做到这一点。有可能吗?

1 个答案:

答案 0 :(得分:0)

基本思路:

用户单击按钮后,附加课程:

.disappear {
  transform: translate(0, -100px);
  transition: opacity 2s, transform 2s;
  opacity: 0
}

然后

window.setTimeout(function() { 
// set display to none, and then remove .disappear
}, 2000)

编辑:详细说明

给每张幻灯片

transition: opacity 2s, transform 2s;

并将它们放在(页面)中心。所有未显示的幻灯片都获得.slidedBottom

使用以下类:

.slidedTop {
  transform: translate(0, -100px);
  opacity: 0;
}
.slidedBottom {
  transform: translate(0, 100px);
  opacity: 0;
}
.disappear {
  opacity: 0;
}
.appear {
  opacity: 1;
}
.hidden {
  display: none; // or use the hidden attribute
}

让元素淡出(向上):

// add .disappear

window.setTimeout(function() { 
// remove disappear, add hidden, slidedTop
}, 2000)

淡入:

// remove hidden, slidedBottom, add appear
window.setTimeout(function() {
// remove appear
}, 2000)