轮播(css+js)

目录

1.实现效果

2.基础代码演示

2.1js代码

2.1css样式

2.3实现效果

3.实现点击切换

3.1给button添加点击事件

3.2效果图如下

3.3发现问题

3.3.1不循环

3.3.2循环


1.实现效果

2.基础代码演示

2.1js代码

复制代码
 <div class="out-box">
      <div class="test-swiper">
       <div class="swiper-box">
        <div class="swiper-item" v-for="(item,index) in 7" :key="index">
          <img src="" alt="">
          <p>学习+积累</p>
        </div>
       </div>
     </div>
     <button class="button-one one">上一张</button>
      <button class="button-one two">下一张</button>
    </div>

2.1css样式

复制代码
.out-box{
   position: relative;
   width: 100%;
  .test-swiper{
    background-color: #d3d8e2;
    width: 1200px;
    margin:0 auto;
    height: 500px;
    position: relative;
    overflow: hidden;
    .swiper-box{
      position: absolute;
      display: flex;
      .swiper-item{
        width: 400px;
        height: 400px;
        background-color: #B7CBEA;
        margin:50px 100px;
      }
    }
  }
  .button-one{
    background-color: rgb(174, 165, 166);
    position: absolute;
    top: 50%;
    width: 50px;
    height: 50px;
    border-radius: 100%;
  }
  .one{
    left: 430px;
  }
  .two{
    right:430px;
  }
}

2.3实现效果

3.实现点击切换

3.1给button添加事件,transform和切换效果

复制代码
<div class="out-box">
      <div class="test-swiper">
       <div class="swiper-box" :style="{transform:`translateX(${translateXtest}px)`,transition: 'transform 0.5s ease-in-out' }">
        <div class="swiper-item" v-for="(item,index) in testList" :key="index">
          <img src="" alt="">
          <p>学习+积累</p>
        </div>
       </div>
     </div>
     <button class="button-one one" @click="onPre">上一张</button>
     <button class="button-one two" @click="onNext">下一张</button>
</div>

export defatult{
  data(){
    return{ 
       testList:[{},{},{},{}]
       testIndex:0
       }
   }
}
 computed: {
    translateXtest(){
      // 计算需要移动的距离
      return -this.testIndex * (400 + 200);
    },
}

onPre(){
   if(this.testIndex>0){ this.testIndex--}
},
onNext(){
  if(this.testIndex<this.testList.length-1){ this.testIndex++}
}

3.2效果图如下

3.3发现问题

发现数组长度只有3,当触发最后一次onNext操作时候,出现空白,如何解决

3.3.1不循环

一次显示2张图片,添加条件testIndex<testList.length-2

onNext(){

if(this.testIndex<this.testList.length-2){this.testIndex++}

}

3.3.2循环

可以当到最后显示testList最后一个数据时,让数组拼接

复制代码
onNext(){
  this.testIndex++
  if(this.testIndex>this.testList.length){
     this.testList=this.testList.concat(this.testList)
    }
}

4.完整代码

复制代码
<div class="out-box">
      <div class="test-swiper">
       <div class="swiper-box" :style="{ transform:`translateX(${translateXtest}px)`,transition: 'transform 0.5s ease-in-out' }">
        <div class="swiper-item" v-for="(item,index) in testList" :key="index">
          <img src="" alt="">
          <p>学习+积累{{ index }}</p>
        </div>
       </div>
     </div>
     <button class="button-one one" @click="onPrev">上一张</button>
     <button class="button-one two" @click="onNext">下一张</button>
</div>

export defatult{
  data(){
    return{ 
       testList:[{},{},{},{}]
       testIndex:0
       }
   }
}
 computed: {
    translateXtest(){
      // 计算需要移动的距离
      return -this.testIndex * (400 + 200);
    },
}

onPre(){
   if(this.testIndex>0){ this.testIndex--}
},
onNext(){
   if(this.testIndex<this.testList.length-2){this.testIndex++}
}

.out-box{
   position: relative;
   width: 100%;
  .test-swiper{
    background-color: #d3d8e2;
    width: 1200px;
    margin:0 auto;
    height: 500px;
    position: relative;
    overflow: hidden;
    .swiper-box{
      position: absolute;
      display: flex;
      .swiper-item{
        width: 400px;
        height: 400px;
        background-color: #B7CBEA;
        margin:50px 100px;
      }
    }
  }
  .button-one{
    background-color: rgb(174, 165, 166);
    position: absolute;
    top: 50%;
    width: 50px;
    height: 50px;
    border-radius: 100%;
  }
  .one{
    left: 430px;
  }
  .two{
    right:430px;
  }
}

如果有好的循环方式,欢迎留言

相关推荐
小二·几秒前
Python Web 开发进阶实战:生物启发计算 —— 在 Flask + Vue 中实现蚁群优化与人工免疫系统
前端·python·flask
局外人LZ几秒前
Forge:web端与 Node.js 安全开发中的加密与网络通信工具集,支持哈希、对称 / 非对称加密及 TLS 实现
前端·安全·node.js
2301_818732061 分钟前
前端一直获取不到后端的值,和数据库字段设置有关 Oracle
前端·数据库·sql·oracle
vx_bisheyuange2 分钟前
基于SpringBoot的酒店管理系统
前端·javascript·vue.js·spring boot·毕业设计
慧一居士5 分钟前
同一个服务器上不同的域名跳往不同的前端项目页面,不显示端口号 ngnix根据不同域名跳转
运维·服务器·前端
ct9787 分钟前
WebGL核心API
前端·gis·webgl
lexiangqicheng7 分钟前
Ant Design Pro 实战:Web 后台页面标准化开发规范与最佳实践
前端
ZI Keep Going9 分钟前
前来填坑:Search Around the World全球联合部署搜索引擎
前端·javascript·搜索引擎
手握风云-10 分钟前
JavaEE 进阶第十期:Spring MVC - Web开发的“交通枢纽”(四)
前端·spring·java-ee
孩子 你要相信光10 分钟前
解决:React 中 map 处理异步数据不渲染的问题
开发语言·前端·javascript