图片轮播组件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
		<style type="text/css">
			.myswiper{
				width:500px;
				height:500px;
				position: absolute;
				margin: auto;
				top:0;
				right:0;
				left:0;
				bottom: 0;
				border:1px solid blue;
			}
			.myswiper img{
				width:300px;
				height:300px;
				position: absolute;
				margin: auto;
				top:0;
				right:0;
				left:0;
				bottom: 0;
			}
		</style>
	</head>
	<body>
		<div id="app">
		  <lunbo :array="array"></lunbo>	
		</div>
		
		<script type="text/javascript">
			// 全局组件的写法
			var  obj={
				template:`
				 <div class="myswiper">
				     <img :src="array[currentIndex]"/>
				 </div>
				`,
				props:{
					// 父传子过来的图片数组
					array:Array
				},
				data:function(){
					return {
						// 图片数组索引默认值
						currentIndex:0
					}
				},			
				// 当组件加载完后,自动执行图片轮播函数
				// mounted 生命周期函数,当元素和数据渲染完网页之后,会自动执行该函数
				mounted(){
					var that = this;
					console.log(that.array) 
					setInterval(function(){
						console.log(1)
						if(that.currentIndex==that.array.length-1){
							that.currentIndex = 0;
						}
						else{
							that.currentIndex = that.currentIndex +1;
						}						
					},2000)					
											
				}
								
			  }
			
			var app = new Vue({
				el:"#app",
				data:{// 图片的数组变量
					array:['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg','img/6.jpg']
				},
				components:{
					"lunbo":obj
				}
			})
			
		</script>		
	</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zaltana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值