Vue入门实战-轮播图切换

这是一个使用Vue.js编写的图片切换效果实现代码。通过数据绑定和事件监听,实现了左右箭头点击切换图片的功能。HTML结构包含图片显示区和导航箭头,CSS用于样式布局,使得图片居中显示,并为箭头添加了圆角和阴影效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述

实现代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>图片切换</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>

  <body>
    <div id="mask">
      <div class="center">
        <!-- 图片 -->
        <img :src="imgList[imgIndex]" alt="" />
        <!-- 左箭头 -->
        <a
          href="javascript:void(0)"
          class="left"
          v-show="imgIndex>0"
          @click="prev"
        >
          <img src="./images/prev.png" alt="" />
        </a>
        <!-- 右箭头 -->
        <a
          href="javascript:void(0)"
          class="right"
          v-show="imgIndex<imgList.length-1"
          @click="next"
        >
          <img src="./images/next.png" alt="" />
        </a>
      </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
      var vue = new Vue({
        el: "#mask",
        data: {
          // 图片数据源
          imgList: [
            "./images/00.jpg",
            "./images/01.jpg",
            "./images/02.jpg",
          ],
          // 当前图片的索引
          imgIndex: 0,
        },
        methods: {
          prev() {
            // 上一张图片按钮
            if (this.imgIndex >= 0) {
              this.imgIndex--;
            }
          },
          next() {
            // 下一张图片
            if (this.imgIndex < this.imgList.length-1) {
              this.imgIndex++;
            }
          },
        },
      });
    </script>
  </body>
</html>

CSS

* {
  margin: 0;
  padding: 0;
}

html,
body,
#mask {
  width: 100%;
  height: 100%;
}

#mask {
  background-color: #c9c9c9;
  position: relative;
}

#mask .center {
  position: absolute;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 10px;
}
#mask .center .title {
  position: absolute;
  display: flex;
  align-items: center;
  height: 56px;
  top: -61px;
  left: 0;
  padding: 5px;
  padding-left: 10px;
  padding-bottom: 0;
  color: rgba(175, 47, 47, 0.8);
  font-size: 26px;
  font-weight: normal;
  background-color: white;
  padding-right: 50px;
  z-index: 2;
}
#mask .center .title img {
  height: 40px;
  margin-right: 10px;
}

#mask .center .title::before {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 65px solid;
  border-color: transparent transparent white;
  top: -65px;
  right: -65px;
  z-index: 1;
}

#mask .center > img {
  display: block;
  width: 700px;
  height: 458px;
}

#mask .center a {
  text-decoration: none;
  width: 45px;
  height: 100px;
  position: absolute;
  top: 179px;
  vertical-align: middle;
  opacity: 0.5;
}
#mask .center a :hover {
  opacity: 0.8;
}

#mask .center .left {
  left: 15px;
  text-align: left;
  padding-right: 10px;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}

#mask .center .right {
  right: 15px;
  text-align: right;
  padding-left: 10px;
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值