vue3用js+css实现轮播图(可调整堆叠程度)

先看效果
在这里插入图片描述
html

 <div
        class="outer"
        style="
          width: 650px;

          background: #fff;
          box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.1);
          border-radius: 15px;
          margin: 0 10px 15px 5px;
        "
      >
      //这里用的是svg-icon,需要的可自行替换为其他图片
        <svg-icon
          id="btn_l"
          class="iconfont"
          style="cursor: pointer"
          v-show="imgList.length > 1"
          name="goleft"
        ></svg-icon>

        <svg-icon
          id="btn_r"
          class="iconfont"
          style="cursor: pointer"
          @click="frount()"
          v-show="imgList.length > 1"
          name="goright"
        ></svg-icon>
        <div class="wrapper" @mouseover="appMouseover()" @mouseout="appMouseout()">
          <!-- <el-carousel height="300px;" >
          <el-carousel-item v-for="(item, index) in imgList"  :key="index">
        
            <el-image style="width: 100%" :src="item.url" :fit="'fill'" />
          </el-carousel-item>
        </el-carousel> -->
          <div
            class="divImg"
            v-for="(item, index) in imgList"
            :style="appIsHover && current == index ? appStyle : ''"
          >
            <img @click="appJump(item, index)" class="image" :src="item.url" />
            <span class="appName" @click="appJump(item, index)">{{ item.name }}</span>
          </div>
        </div>
      </div>

js

//图片列表
const imgList = ref([
  {
    name: '',
    url: new URL('@/assets/banner3.png', import.meta.url).href
  },
  {
    name: '',
    url: new URL('@/assets/banner.png', import.meta.url).href
  },
  {
    name: '',
    url: new URL('@/assets/banner2.png', import.meta.url).href
  }
])



const appStyle = 'width:330px!important;height:220px!important'
//鼠标悬浮时清除自动滚动,2秒后恢复
const appMouseover = (item, index) => {
  clearInterval(timer)
}
const appMouseout = (item, index) => {
  if (timer2) {
    clearTimeout(timer2)
  }
  timer2 = setTimeout(() => {
    autoplay()
  }, 2000)
}
//点击后直接跳到对应的图片
const appJump = (item, index) => {
  if (current == index) {
  }
  clearInterval(timer)
  if (timer2) {
    clearTimeout(timer2)
  }
  timer2 = setTimeout(() => {
    autoplay()
  }, 2000)
  current = index
  frount()
}

const appJump = (item, index) => {
  if (current == index) {
  }
  clearInterval(timer)
  if (timer2) {
    clearTimeout(timer2)
  }
  timer2 = setTimeout(() => {
    autoplay()
  }, 2000)
  current = index
  frount()
}










var imgs = document.getElementsByClassName('divImg')
var len = imgs.length
var current = 0

var timer
var timer2
//页面初始调用
function wrapper() {
  imgs = document.getElementsByClassName('divImg')
  len = imgs.length
  current = 0

  if (len != 0) {
    frount()
    bind()
  }
  autoplay()
}
 wrapper()
//点击图片跳转主要方法
function frount() {
  var mlen = Math.floor(len / 2)
  var limg, rimg
  for (var i = 0; i < mlen; i++) {
    limg = len + current - i - 1
    rimg = current + i + 1
    if (limg >= len) {
      limg -= len
    }
    if (rimg >= len) {
      rimg -= len
    }
   // 这里70为叠加度,可自行调整
    imgs[limg].style.transform =
      `translateX(` + -70 * (i + 1) + `px) translateZ(` + (200 - i * 100) + `px) rotateY(30deg)`
    imgs[rimg].style.transform =
      `translateX(` + 70 * (i + 1) + `px) translateZ(` + (200 - i * 100) + `px) rotateY(-30deg)`
  }
  imgs[current].style.transform = `translateZ(300px)`
}
//给左右箭头添加点击效果
function bind() {
  // for (var i = 0; i < len; i++) {
  //     (function (i) {
  //         imgs[i].onclick = function () {
  //             current = i;
  //             frount();
  //         }
  //     })(i);
  // }
  var btn_l = document.getElementById('btn_l')
  var btn_r = document.getElementById('btn_r')
  btn_l.onclick = function () {
    clearInterval(timer)
    if (timer2) {
      clearTimeout(timer2)
    }
    timer2 = setTimeout(() => {
      autoplay()
    }, 2000)
    if (current <= 0) {
      current = len - 1
    } else {
      current--
    }
    frount()
  }
  btn_r.onclick = function () {
    clearInterval(timer)
    if (timer2) {
      clearTimeout(timer2)
    }
    timer2 = setTimeout(() => {
      autoplay()
    }, 2000)

    if (current >= len - 1) {
      current = 0
    } else {
      current++
    }
    frount()
  }
}
//自动播放
function autoplay() {
  timer = setInterval(function () {
    if (current >= len - 1) {
      current = 0
    } else {
      current++
    }
    frount()
  }, 2000)
}

css

.wrapper .divImg {
  float: left;
  width: 300px;
  height: 150px;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border-radius: 8px;
  transition: transform 1s ease-in-out, width 0.5s ease-in-out, height 0.5s ease-in-out;
  cursor: pointer;
}
.wrapper .image {
  width: 100%;
  height: 100%;
}
.wrapper .appName {
  display: inline-block;
  position: absolute;
  bottom: 4px;
  width: 100px;
  font-size: 15px;
  color: #3fe2ff;
  text-align: center;
  left: calc(50% - 50px);
}

.wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: none;
  perspective: 800px;
  transform-style: preserve-3d;
}
#btn_r {
  position: absolute;
  right: 30px;
  top: 48%;
  // background: rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  width: 30px;

  height: 30px;
  z-index: 999; 
}
.outer {
  color: #fff;
  position: relative;
}
#btn_l {
  position: absolute;
  left: 30px;
  top: 48%;
  // background: rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  width: 30px;
  //  opacity: 0.5;
  height: 30px;

  z-index: 99999999999;
}
uniapp 是一个使用 Vue.js 开发所有前端应用的框架,可以编译到 iOS、Android、以及各种小程序等多个平台。在 uniapp 中实现堆叠轮播图翻书效果,通常需要结合 Vue 组件、CSS 动画以及可能的一些第三方插件来完成。 堆叠轮播图翻书效果是通过模拟翻页的动作,给用户一种翻看真实书籍的感觉。实现这样的效果通常要做的步骤包括: 1. 使用 Vue 组件作为轮播图的基础,可以使用 uniapp 提供的 `<swiper>` 和 `<swiper-item>` 组件来创建基本的轮播结构。 2. 设计页面的布局,使得每个轮播项能够像书籍的页面一样堆叠起来。这通常需要通过 CSS 的样式控制,比如使用 `position: absolute;` 来定位每个轮播项,确保它们能够正确地层叠。 3. 实现翻页动画效果,可以通过 CSS 动画或者 JavaScript 动画库来实现。在翻页时,需要确保新的页面能够从一侧“翻出”,而旧的页面则从另一侧“翻入”。这需要精心设计动画的起始和结束状态,以及过渡时间。 4. 处理用户的交互,比如手指滑动或者点击事件来触发翻页动作。在 uniapp 中,可以通过绑定 touch 事件来实现。 以下是一个基本的代码示例,展示如何开始构建这样的轮播图: ```vue <template> <swiper class="swiper-container" indicator-dots="true" autoplay="true" interval="2000"> <swiper-item v-for="(item, index) in imageList" :key="index"> <view class="slide"> <image :src="item" class="slide-image"/> </view> </swiper-item> </swiper> </template> <script> export default { data() { return { imageList: [ &#39;url/to/image1.jpg&#39;, &#39;url/to/image2.jpg&#39;, &#39;url/to/image3.jpg&#39;, // 更多图片... ] }; } }; </script> <style> .swiper-container { height: 200px; /* 轮播图的高度 */ } .slide { position: relative; width: 100%; height: 100%; } .slide-image { width: 100%; height: 100%; object-fit: cover; position: absolute; /* 使得图片可以重叠 */ } </style> ``` 这段代码只是一个基础的轮播图实现,若要实现堆叠式翻书效果,则需要添加相应的 CSS 动画效果,并且处理好每个页面的层叠和翻转逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值