b站黑马的Vue快速入门案例代码——图片切换(类似手动播放的轮播图)

目录

目标效果:

重点原理:

1.用数组储存图片的数据

2.v-bind指令可以设置元素属性 e.g.src 

语法   v-bind:属性名=表达式

简写【实际开发常用】   :属性名=表达式

3.v-show和v-if都可以切换元素的显示/隐藏状态

(1)频繁切换显示/隐藏的dom元素用 v-show

(2)不频繁切换显示/隐藏的dom元素用 v-if

实现步骤:

1.定义图片数组

2.添加图片索引  

3.绑定src属性

4.图片切换逻辑/5.显示状态切换

代码部分:

1.图片切换.html(全是重点)

2.index.css(辅助作用) 

3.vue.js(辅助作用)

安装Vue的方法 /获取vue.js文件的方法:​编辑


目标效果:

1.点击右边按钮,是往右播放一张图片;点击左边按钮,是往左播放一张图片

2.在第一张图片的时候,不显示按钮;在最后一张图片的时候,不显示按钮

3.初始状态显示是第一张图片 (在图片数组中index为0)

e.g.1初始效果,默认显示的是第一张图片:

 e.g.2在默认显示的是第一张图片的基础上,点击右按钮一次,切换到第二张图片:

 

 e.g.3一直点击右按钮,直到显示最后一张图片:

  e.g.4在显示最后一张图片的基础上,点击左按钮,可以查看倒数第二张图片:

重点原理:

1.数组储存图片的数据

e.g.

  <script>

    var app = new Vue({

      el: "#mask",

      data: {

        imgArr: [

          "./images/00.jpg",

          "./images/01.jpg",

          "./images/02.jpg",

          "./images/03.jpg",

          "./images/04.jpg",

          "./images/05.jpg",

          "./images/06.jpg",

          "./images/07.jpg",

          "./images/08.jpg",

          "./images/09.jpg",

          "./images/10.jpg",

        ],//图片数组

        index: 0//索引是从第一张图开始计算

      },

      methods: {

        prev: function () {//prev 切换到上一张图片

          this.index--;//此处this指当前对象#mask

        },

        next: function () {//next 切换到下一张图片

          this.index++;//此处this指当前对象#mask

        }

      }

    })

  </script>

2.v-bind指令可以设置元素属性 e.g.src 

语法   v-bind:属性名=表达式

简写【实际开发常用】   :属性名=表达式

e.g.

:src=“...”是v-bind:src=”...”的简写,都可以给img元素添加src属性

3.v-showv-if都可以切换元素的显示/隐藏状态

(1)频繁切换显示/隐藏的dom元素用 v-show

v-show=“表达式”    原理是【dom元素一直存在,只是修改display,对性能损耗小

v-show=“false”   隐藏   dom元素加上了display:none

v-show=“true”    不隐藏

(2)不频繁切换显示/隐藏的dom元素用 v-if

v-if=“表达式”     原理是【新增/删除dom元素,对性能损耗大

v-show=“false”,元素存在于dom树中(即该dom元素存在)

v-show=“false”,从dom树中移除(即该dom元素不存在

实现步骤:

1.定义图片数组

数组储存所有图片 

2.添加图片索引  

3.绑定src属性

4.图片切换逻辑/5.显示状态切换

代码部分:

1.图片切换.html(全是重点)

<!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">
      <h2 class="title">
        <img src="./images/logo.png" alt="">
        深圳创维校区环境
      </h2>
      <!-- 图片 -->
      <img :src="imgArr[index]" alt="" />
      <!-- 左箭头 -->
      <!-- 此处切换左右箭头的显示/隐藏:v-if也可以实现一样的效果,但是由于v-if对性能消耗比v-show大,所以应该首选用v-show -->
      <!-- v-show="index!=0"指: -->
      <!-- (1)当index的值不等于0的时候,显示左箭头 -->
      <!-- (2)当index的值等于0的时候,隐藏左箭头 -->
      <a href="javascript:void(0)" @click="prev" v-show="index!=0" class="left">
        <img src="./images/prev.png" alt="" />
      </a>
      <!-- 右箭头 -->
      <!-- v-show="index<imgArr.length-1"指: -->
      <!-- index<imgArr.length-1=10-1=9,index索引是9的时候是最后一张图片 -->
      <!-- (1)即最后一张图片之前,显示右箭头 -->
      <!-- (2)到最后一张图片,隐藏右箭头 -->
      <a href="javascript:void(0)" @click="next" v-show="index<imgArr.length-1" class="right">
        <img src="./images/next.png" alt="" />
      </a>
    </div>
  </div>

  <script src="../vue.js"></script>

  <script>
    var app = new Vue({
      el: "#mask",
      data: {
        imgArr: [
          "./images/00.jpg",
          "./images/01.jpg",
          "./images/02.jpg",
          "./images/03.jpg",
          "./images/04.jpg",
          "./images/05.jpg",
          "./images/06.jpg",
          "./images/07.jpg",
          "./images/08.jpg",
          "./images/09.jpg",
          "./images/10.jpg",
        ],//图片数组
        index: 0//索引是从第一张图开始计算
      },
      methods: {
        prev: function () {//prev 切换到上一张图片
          this.index--;//此处this指当前对象#mask
        },
        next: function () {//next 切换到下一张图片
          this.index++;//此处this指当前对象#mask
        }
      }
    })
  </script>
</body>

</html>

2.index.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;
}

3.vue.js(辅助作用)

因为该文件内容太多,请前往该网址(Vue官网)下载 

安装 — Vue.js

安装Vue的方法 /获取vue.js文件的方法:

  • 6
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的图片轮播图前端代码实现,包括自动播放手动播放的功能,并且使用 Vue.js 框架: ```html <template> <div class="carousel"> <div class="carousel-container"> <div class="carousel-item" v-for="(item, index) in items" :key="index" :style="{ backgroundImage: `url(${item.imageUrl})` }" :class="{ active: index === currentIndex }"></div> </div> <div class="carousel-indicators"> <span class="carousel-indicator" v-for="(item, index) in items" :key="index" :class="{ active: index === currentIndex }" @click="jumpTo(index)"></span> </div> <div class="carousel-buttons"> <button class="carousel-button" @click="prev"><i class="fas fa-chevron-left"></i></button> <button class="carousel-button" @click="next"><i class="fas fa-chevron-right"></i></button> </div> </div> </template> <script> export default { data() { return { items: [ { imageUrl: "https://picsum.photos/800/400?random=1" }, { imageUrl: "https://picsum.photos/800/400?random=2" }, { imageUrl: "https://picsum.photos/800/400?random=3" }, { imageUrl: "https://picsum.photos/800/400?random=4" }, { imageUrl: "https://picsum.photos/800/400?random=5" }, ], currentIndex: 0, timer: null, }; }, methods: { jumpTo(index) { this.currentIndex = index; }, prev() { this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length; }, next() { this.currentIndex = (this.currentIndex + 1) % this.items.length; }, startAutoPlay() { this.timer = setInterval(() => { this.next(); }, 3000); }, stopAutoPlay() { clearInterval(this.timer); }, }, mounted() { this.startAutoPlay(); }, beforeDestroy() { this.stopAutoPlay(); }, }; </script> <style scoped> .carousel { position: relative; width: 800px; height: 400px; overflow: hidden; } .carousel-container { position: relative; width: 100%; height: 100%; } .carousel-item { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-size: cover; background-position: center center; opacity: 0; transition: opacity 0.5s; } .carousel-item.active { opacity: 1; } .carousel-indicators { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); } .carousel-indicator { display: inline-block; width: 10px; height: 10px; margin: 0 5px; border-radius: 50%; cursor: pointer; background-color: #ccc; } .carousel-indicator.active { background-color: #333; } .carousel-buttons { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; height: 40px; display: flex; justify-content: space-between; align-items: center; } .carousel-button { width: 40px; height: 40px; border: none; background-color: rgba(255, 255, 255, 0.5); color: #333; font-size: 24px; cursor: pointer; transition: background-color 0.3s; } .carousel-button:hover { background-color: rgba(255, 255, 255, 0.8); } .carousel-button i { display: inline-block; width: 100%; height: 100%; transform: translateY(-2px); } </style> ``` 代码解释: 1. 在 `data` 中,我们定义了一个 `items` 数组,包含了要轮播的图片的 URL,以及一个 `currentIndex` 变量,表示当前轮播到哪一张图片。还有一个 `timer` 变量,用于存储自动播放的计时器。 2. 在 `mounted` 生命周期中,我们调用了 `startAutoPlay` 方法,启动自动播放功能,使用 `setInterval` 方法每隔 3 秒钟自动切换到下一张图片。 3. 在 `beforeDestroy` 生命周期中,我们调用了 `stopAutoPlay` 方法,停止自动播放功能,使用 `clearInterval` 方法清除定时器。 4. 在模板中,我们使用 `v-for` 指令遍历 `items` 数组,生成每一张图片的 DOM 元素。使用 `:style` 绑定 `backgroundImage` 样式,将每张图片作为背景图显示出来。使用 `:class` 绑定 `active` 样式,根据 `currentIndex` 变量判断当前的图片是否应该显示出来。 5. 在模板中,我们还使用了一个圆点指示器和两个箭头按钮,用于手动控制图片切换。使用 `v-for` 指令遍历 `items` 数组,生成每一个圆点指示器的 DOM 元素。使用 `:class` 绑定 `active` 样式,根据 `currentIndex` 变量判断当前的圆点指示器是否应该高亮显示。使用 `@click` 绑定 `jumpTo` 方法,点击圆点指示器时,跳转到对应的图片。 6. 在模板中,我们还使用了两个按钮,分别控制图片的向前和向后切换。使用 `@click` 绑定 `prev` 和 `next` 方法,点击按钮时,切换到上一张或者下一张图片。 7. 在样式中,我们使用了一些 CSS 属性和样式声明,用于布局、美化和动画。其中, `.carousel` 表示轮播图的容器,`.carousel-item` 表示每一张图片的容器,`.carousel-indicator` 表示圆点指示器,`.carousel-button` 表示箭头按钮。 希望以上代码能够满足您的需求,如果您有任何问题或者需要进一步的帮助,请随时联系我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值