vue 使用 transition 做页面过渡切换时引起的 position 定位问题

1 篇文章 0 订阅

vue 使用 transition 做页面过渡切换时引起的 position 定位问题

vue项目 home 页面的 <router-view /> 使用了 transition 包裹起来做过渡效果的页面切换,当切换到 info 页面,如图:

home页面

<template>
  <div id="app">
    <div class="main">
      <transition :enter-active-class="currentClass">
        <router-view/>
      </transition>
    </div>
   </div>
</template>

info 页面

<template>
  <div class="info_box">
    <div class="top">
      <span @click="back">&lt;</span>
      <p>{{infos.title}}</p>
    </div>
    <div class="info">
      <swiper ref="mySwiper" :options="swiperOptions">
          <swiper-slide v-for="item in 5" :key="item">
            <img class="imgs" :src="infos.img" alt="">
          </swiper-slide>
          <div class="swiper-pagination" slot="pagination"></div>
      </swiper>
    </div>
    <p>{{infos.info}}</p>
    <p>{{infos.num}}</p>

	//重点是这个盒子,样式在下面
    <div class="btn_box">
     <input @click="addCars(infos.id)" class="btn" type="button" value="添加到购物车"> 
    </div> 
  </div>
</template>

btn_box 的 样式

.btn_box {
  position: fixed;
  width: 100%;
  height: 50px;
  left: 0; 
  bottom: 0;
}

然后切换到 info 页面, 如图
在这里插入图片描述
通过css样式可以看到,设置的定位应该是国定在底部,当时刚进入的时候就停留字在中间等过渡效果完成才会固定到底部, 不管是什么定位都是这种情况

.btn_box {
  position: fixed;
  width: 100%;
  height: 50px;
  left: 0; 
  bottom: 0;
}

然后通过查找资料和多次测试,发现是高度问题引起的,于是给 info_box 这个类添加高度

.info_box{
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0px;
  bottom: 0px;
}

在这里插入图片描述
问题解决,一进入按钮就固定在底部

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用 Vue.js 中的过渡效果和动画来实现卡片的左右切换。以下是一个简单的实现示例: ```html <template> <div class="card-container"> <transition-group name="slide" tag="div"> <div v-for="(card, index) in cards" :key="index" class="card"> {{ card }} </div> </transition-group> <button @click="prevCard" class="prev-button">Prev</button> <button @click="nextCard" class="next-button">Next</button> </div> </template> <script> export default { data() { return { cards: ['Card 1', 'Card 2', 'Card 3', 'Card 4'], currentIndex: 0 }; }, methods: { prevCard() { if (this.currentIndex > 0) { this.currentIndex--; } }, nextCard() { if (this.currentIndex < this.cards.length - 1) { this.currentIndex++; } } } }; </script> <style> .card-container { position: relative; } .card { display: inline-block; width: 200px; height: 200px; background-color: #ccc; margin-right: 10px; } .slide-enter-active, .slide-leave-active { transition: all 0.5s; } .slide-enter, .slide-leave-to { transform: translateX(100%); } </style> ``` 这里使用了 `<transition-group>` 组件来包裹卡片,并添加了 `name="slide"` 属性,以便定义过渡动画的类名。 在 `methods` 中,`prevCard` 和 `nextCard` 方法分别用于切换到前一个和后一个卡片。通过更新 `currentIndex` 的值来改变当前显示的卡片。 在样式中,使用了 CSS 的 `transform` 属性来实现卡片的平移动画效果。 你可以根据自己的需求修改卡片的内容、样式和动画效果。希望对你有帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值