账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue.js的transition动画中,v-if和v-show在表现上有什么差异?
    62
    0
    <template>
      <div class="slider-show" @mouseover="clearInv" @mouseout="runInv">
        <div class="slide-img">
            <transition name="slide-trans">
              <img v-if="isShow" :src="img[nowIndex].src">
            </transition>
            <transition name="slide-trans-old">
              <img v-if="!isShow" :src="img[nowIndex].src">
            </transition>
        </div>
        <div class="mask">
          <h2 class="img-title">{{img[nowIndex].title}}</h2>
          <ul class="control">
            <li @click="goto (prevIndex)">&lt;</li>
            <li v-for="(item,index) in img">
              <a :class="{'on' : index === nowIndex}" @click="goto (index)">
                {{index + 1}}
              </a>
            </li>
            <li @click="goto (nextIndex)">&gt;</li>
          </ul>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      data () {
        return {
          inv: 2000,
          nowIndex: 0,
          isShow: true,
          img: [
            {
              src: require('../assets/slideShow/pic1.jpg'),
              title: '数据统计',
              toKey: 'detail/analysis'
            },
            {
              src: require('../assets/slideShow/pic2.jpg'),
              title: '数据预测',
              toKey: 'detail/count'
            },
            {
              src: require('../assets/slideShow/pic3.jpg'),
              title: '数据分析',
              toKey: 'detail/analysis'
            },
            {
              src: require('../assets/slideShow/pic4.jpg'),
              title: '广告发布',
              toKey: 'detail/forecast'
            }
          ]
        }
      },
      computed: {
        prevIndex () {
          if (this.nowIndex === 0) {
            return this.img.length - 1
          } else {
            return this.nowIndex - 1
          }
        },
        nextIndex () {
          if (this.nowIndex === this.img.length - 1) {
            return 0
          } else {
            return this.nowIndex + 1
          }
        }
      },
      methods: {
        goto (index) {
          this.isShow = false
          setTimeout(() => {
            this.nowIndex = index
            this.isShow = true
          }, 1000)
        },
        runInv () {
          this.invId = setInterval(() => {
            this.goto(this.nextIndex)
          }, this.inv)
        },
        clearInv () {
          clearInterval(this.invId)
        }
      },
      mounted () {
        this.runInv()
      }
    }
    </script>
    
    <style scoped>
    /*动画样式*/
    .slide-trans-enter-active {
      transition: all .5s;
    }
    .slide-trans-enter {
      transform: translateX(900px);
    }
    .slide-trans-old-leave-active {
      transition: all .5s;
      transform: translateX(-900px);
    }
    .slider-show {
      width: 900px;
      height: 500px;
      position: relative;
      margin-top: 15px;
      overflow: hidden;
    }
    .slide-img {
      height: 100%;
    }
    .slide-img img {
      position: absolute;
      top: 0;
    }
    .img-title {
      font-size: 14px;
      color: #ddd;
    }
    .control {
      position: absolute;
      right: 0;
      bottom: 0;
    }
    .control li {
      float: left;
      cursor: pointer;
      color: #fff;
      margin-right: 22px;
    }
    .on {
      text-decoration: underline;
    }
    .mask {
      position: absolute;
      bottom: 0;
      width: 100%;
      line-height: 30px;
      height: 30px;
      background-color: rgba(0,0,0,0.5);
      padding-left: 20px;
      z-index: 999;
    }
    </style>
    

    如上,一个轮播图组件,基于轮播图会频繁变换及性能的考虑,第5和第8行我本用的是v-show,然后调试发现进入和退出的动画的对象都是新图,头都想秃掉了,才把v-show换成了v-if,成功...

    所以是为什么啊喂~!

    我所知的v-ifv-show的区别是:
    1.前者是不满足条件时不渲染dom树上无内容;
    2.后者是渲染并用css的display:none方式隐藏.

    但为什么在表现上回出现这样的差异呢?
    求指教~谢谢~!(/ω\)

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 提酒:買花o∠※ 普通会员 1楼
      在Vue.js中,v-if和v-show都可以用来控制元素的显示与隐藏,但在过渡动画(transition)的表现上存在一些差异: 1. v-if: - 当使用v-if时,条件为false时,Vue会从DOM中移除该元素,这意味着在进行过渡动画时,实际上是执行了“插入”和“删除”的DOM操作。 - 进行过渡动画时,v-if会在进入/离开时触发相应的CSS过渡或动画。 2. v-show: - 当使用v-show时,无论条件如何,元素始终会被渲染到DOM中,只是简单地切换CSS中的`display`属性来控制元素的显示与隐藏。 - 因此,在过渡动画方面,v-show同样支持CSS过渡或动画,但请注意,由于v-show并不会真正移除或插入DOM元素,所以v-show的过渡效果只适用于通过改变`display`属性实现动画的场景。 总结一下,如果你想在元素显示/隐藏时有过渡动画,并且不关心条件变化时对DOM的影响,可以使用v-show。而如果你希望在元素被添加/删除DOM时有过渡动画,则应使用v-if。
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部