前端vue实现轮播图(自动轮播)!!!以及轮播图不显示图片问题解决方法!

 

话不多说直接上代码

html部分:

<div class="showImg">
          <!--轮播图片 -->
          <img
            @mouseover="changeInterval(true)"
            @mouseleave="changeInterval(false)"
            v-for="item in imgArr"
            :key="item.id"
            :src="item.url"
            alt="暂无图片"
            v-show="item.id === currentIndex"
          />
          <!-- //左侧按钮 -->
          <div @click="clickIcon('up')" class="iconDiv icon-left">
            <i class="el-icon-caret-left"></i>
          </div>
          <!-- //右侧按钮 -->
          <div @click="clickIcon('down')" class="iconDiv icon-right">
            <i class="el-icon-caret-right"></i>
          </div>
          <!-- //控制圆点 -->
          <div class="banner-circle">
            <ul>
              <li
                @click="changeImg(item.id)"
                v-for="item in imgArr"
                :key="item.id"
                :class="item.id === currentIndex ? 'active' : ''"
              ></li>
            </ul>
          </div>
        </div>

css部分:

* {
  padding: 0;
  margin: 0;
}
/* 清除li前面的圆点 */
li {
  list-style-type: none;
}
.showImg {
  position: relative;
  width: 1540px;
  height: 290px;
  margin: 0 auto;
  overflow: hidden;
}
/* 轮播图片 */
.showImg img {
  width: 100%;
  height: 100%;
}

/* 箭头图标 */
.iconDiv {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: 1px solid #666;
  border-radius: 15px;
  background-color: rgba(125, 125, 125, 0.2);
  line-height: 30px;
  text-align: center;
  font-size: 25px;
  cursor: pointer;
}
.iconDiv:hover {
  background-color: white;
}
.icon-left {
  left: 10px;
}
.icon-right {
  right: 10px;
}

/* 控制圆点 */
.banner-circle {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 20px;
}
.banner-circle ul {
  margin: 0 50px;
  height: 100%;
  text-align: right;
}
.banner-circle ul li {
  display: inline-block;
  width: 14px;
  height: 14px;
  margin: 0 5px;
  border-radius: 7px;
  background-color: rgba(125, 125, 125, 0.8);
  cursor: pointer;
}
.active {
  background-color: black !important;
}

js部分:

		data(){
			return {
				currentIndex :0,//当前所在图片下标
				timer:null,//定时轮询
				imgArr:[
					{	id:0,
						url:require("../../assets/img/1.jpg"),
					},{
						id:1,
						url:require("../../assets/img/2.jpg"),
					},{
						id:2,
						url:require("../../assets/img/3.jpg"),
					},{
						id:3,
						url:require("../../assets/img/4.jpg"),
					},
				]
			}
		},
		methods:{
			//开启定时器
			startInterval(){
				// 事件里定时器应该先清除在设置,防止多次点击直接生成多个定时器
				clearInterval(this.timer);
				this.timer = setInterval(()=>{
					this.currentIndex++;
					if(this.currentIndex > this.imgArr.length-1){
						this.currentIndex = 0
					}
				},3000)
			},
			// 点击左右箭头
			clickIcon(val){
				if(val==='down'){
					this.currentIndex++;
					if(this.currentIndex===this.imgArr.length){
						this.currentIndex = 0;
					}
				}else{
					/* 第一种写法
					this.currentIndex--;
					if(this.currentIndex < 0){
						this.currentIndex = this.imgArr.length-1;
					} */
					// 第二种写法
					if(this.currentIndex === 0){
						this.currentIndex = this.imgArr.length;
					}
					this.currentIndex--;
				}
			},
			// 点击控制圆点
			changeImg(index){
				this.currentIndex = index
			},
			//鼠标移入移出控制
			changeInterval(val){
				if(val){
					clearInterval(this.timer)
				}else{
					this.startInterval()
				}
			}
		},
		//进入页面后启动定时轮询
		mounted(){
			this.startInterval()
		}
	})

轮播i图完成喽!!!

若是不显示图片,将data里的url用require写即可显示图片!!!例如:

url: require("../../assets/img/2.jpg")

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,以下是一个简单的图片轮播图前端代码实现,包括自动播放和手动播放的功能,并且使用 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` 表示箭头按钮。 希望以上代码能够满足您的需求,如果您有任何问题或者需要进一步的帮助,请随时联系我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值