轮播(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;
  }
}

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

相关推荐
WooaiJava1 小时前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
爱喝白开水a2 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
Never_Satisfied2 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌412 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
WeiXiao_Hyy2 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
吃杠碰小鸡3 小时前
高中数学-数列-导数证明
前端·数学·算法
kingwebo'sZone3 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
xjt_09013 小时前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js
我是伪码农3 小时前
Vue 2.3
前端·javascript·vue.js
夜郎king4 小时前
HTML5 SVG 实现日出日落动画与实时天气可视化
前端·html5·svg 日出日落