vue3旋转木马型轮播图,环型滚动

<template>
  <div>
    <div class="content">
      <div class="but1" @click="rotateLeft">--向左</div>
      <div class="ccc">
        <main id="main">
          <div class="haha" ref="haha">
            <div
              class="box"
              v-for="(item, index) in imgList"
              :key="index"
             :style="boxStyle(index)"
            >
            <!-- :style="{ '--d': index }" -->
            <!--  :style="boxStyle(index)" -->
              <div class="boxs">{{ index }}
                <img class="img" :src="item.url" alt="" />
              </div>
            </div>
          </div>
        </main>
      </div>
      <div class="but2" @click="rotateRight">++向右</div>
    </div>
    <p>当前中间的图片索引是: {{ currentIndex }}</p>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted,computed } from "vue";
import img1 from "./imgs/pic_dsjfx.png";
import img2 from "./imgs/pic_lyxxw.png";
import img3 from "./imgs/pic_rjgl.png";
import img4 from "./imgs/pic_yqjc.png";
import img5 from "./imgs/pic_lyzhjgpt.png";

const haha = ref<HTMLDivElement | null>(null);
const zhuan = ref(0);
let lunbo: number | undefined;
const imgList = [
{
    url: img4,
  },
  {
    url: img5,
  },
  {
    url: img1,
  },
  {
    url: img2,
  },
  {
    url: img3,
  },
  {
    url: img4,
  },
  {
    url: img5,
  },
  {
    url: img1,
  },
  {
    url: img2,
  },
];
const boxes = [
  { background: "orange" },
  // { background: 'pink' },
  { background: "green" },
  { background: "red" },
  { background: "blue" },
  { background: "aqua" },
];

const rotateRight = () => {
  zhuan.value -= 20;
  console.log(zhuan.value);
  if (haha.value) {
    haha.value.style.transform = `rotateY(${zhuan.value}deg)`;
  }
};

const rotateLeft = () => {
  zhuan.value += 20;
  console.log(zhuan.value);
  if (haha.value) {
    haha.value.style.transform = `rotateY(${zhuan.value}deg)`;
  }
};

const startInterval = () => {
  lunbo = setInterval(rotateRight, 3000);
};

const clearInterval = () => {
  if (lunbo !== undefined) {
    window.clearInterval(lunbo);
  }
};
let chushidushi = 0
let dangqian = 0
const totalBoxes = imgList.length;
// 计算当前中间的图片索引
const currentIndex = computed(() => {
  // zhuan.value / 20 用于计算旋转了多少个位置
  // Math.round 用于修正因浮点数运算导致的小数问题
  const normalizedIndex = Math.round(zhuan.value / 20) % totalBoxes;

  // 确保 index 是一个正值并且在 [0, totalBoxes-1] 范围内
  console.log(-(normalizedIndex ) % totalBoxes);
  dangqian = -(normalizedIndex ) % totalBoxes
  return -(normalizedIndex ) % totalBoxes;
});
// 动态计算每个 box 的样式,包括大小调整
// const boxStyle = (index: number) => {
    
//   const offset = (index - currentIndex.value + totalBoxes) % totalBoxes;
// // const offset = dangqian;
// console.log(offset,'---offset');

//   let scale = 1;
    
//   if (offset === 0) {
//     scale = 1.2; // 正中间的图片放大
//   } else if (offset === 1 || offset === totalBoxes - 1) {
//     scale = 0.8; // 左右两侧的图片缩小
//   } else {
//     scale = 0.6; // 其他图片更小
//   }

//   return {
//     '--d': index.toString(),
//     transform: ` transform: rotateY(calc(var(--d) * 20deg)) translateZ(450px); scale(${scale})`,
//   };
// };

const boxStyle = (index: number) => {
  const offset = (index - currentIndex.value + totalBoxes) % totalBoxes;
  let scale = 1;

  if (offset === 0) {
    scale = 1.2; // 正中间的图片放大
  } else if (offset === 1 || offset === totalBoxes - 1) {
    scale = 1; // 左右两侧的图片缩小
  } else if(offset === 2 || offset === totalBoxes - 2) {
    scale = 0.6; // 其他图片更小
  } else {
    scale = 0.2; // 其他图片更小

  }

  return {
    '--d': index.toString(),
    transform: `rotateY(calc(var(--d) * 20deg)) translateZ(450px) scale(${scale})`,
    transition: 'transform 0.5s ease',
  };
};


onMounted(() => {
  // startInterval();
//   zhuan.value -= 20;
});

onUnmounted(() => {
  // clearInterval();
});
</script>

<style scoped lang="scss">
.content {
  width: 1800px;
  margin: 0px auto;
//   background-color: pink;
  position: relative;
  padding: 50px 0;
  display: flex;
  align-items: center;
}

.ccc {
  width: 1800px;
  height: 600px;
  background-color: #4fb3f6;
  margin: 0 auto;
  display:flex;
  align-items:center;
  justify-content: center
}

main {
  position: relative;
  width: 130px;
  height: 130px;
//   margin:  auto;
  perspective: 900px;
  background-color: rgb(242, 171, 232);
}

.haha {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: all 1s;
}

.but1,
.but2 {
  width: 50px;
  height: 50px;
  background-color: greenyellow;
}

.box {
  position: absolute;
  width: 100%;
  height: 100%;
  transform: rotateY(calc(var(--d) * 20deg)) translateZ(450px);
  background-color: #d0c2c2ac;
  display: flex;
  justify-content: center;
}

.boxs {
  width: 100px;
  height: 160px;
}
.img {
  width: 100%;
  height: 100%;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值