前端学习-图片轮播效果

js定时器实现

html标签

		<div id="box">
			<img src="img/pic1.jpg" />
			<span id="page">1</span>
		</div>
		<div id="btn1" onclick="back()">
			<img src="img/btn1.png" />
		</div>
		<div id="btn2" onclick="next()">
			<img src="img/btn2.png" />
		</div>

js代码

	<script>
		var box = document.getElementById('box');
		// var img = document.createElement("img");
		// img.setAttribute('src', 'img/pic1.jpg'); //给标签定义src链接
		// box.appendChild(img);
		box.style.float = "left";

		var imgs = document.getElementsByTagName('img')[0];
		var spans= document.getElementsByTagName('span')[0];
		var arrUrl = ['img/pic1.jpg', 'img/pic2.jpg', 'img/pic3.jpg'];
		var num = 0;


		//添加定时器
		function autoPlay() {
			timer = setInterval(function() {
				num++;
				num %= arrUrl.length; //以数组长度来循环切换
				imgs.src = arrUrl[num]; //显示第num张图片
				spans.innerHTML = num+1+'/'+arrUrl.length;
			}, 2000);
		}
		autoPlay(); //函数直接调用,打开页面就开始轮播
		// setTimeout(autoPlay, 2000); //打开页面2秒钟之后再开始轮播


		//下一张
		function next() {
			num++;
			num %= arrUrl.length; //以数组长度来循环切换
			imgs.src = arrUrl[num]; //显示第num张图片
			spans.innerHTML = num+1+'/'+arrUrl.length;
		}
	</script>

css

#box{
	position: absolute;
	left: 200px;
	right: 200px;
	top: 22px;
}
#btn1{
	float: left;
	position: absolute;
	left: 20px;
	top: 50%;
}
#btn2{
	float: right;
	position: absolute;
	right: 20px;
	top: 50%;
}
span{
	position: absolute;
	left: 50%;
	bottom: -20px;
	color: blueviolet;
}

css使用animation实现

html部分

<div></div>	

css

div {
	position: absolute;
	left: 200px;
	right: 200px;
	top: 22px;
	width: 1000px;
	height: 561px;
	background: #ffffff;
	
	/* animation-iteration-count: infinite; //动画播放次数 无限次 */
	animation: myfirst 8s linear 1s infinite normal;
	
	-moz-animation: myfirst 8s linear 1s infinite normal;
	/* Firefox */
	-webkit-animation: myfirst 8s linear 1s infinite normal;
	/* Safari and Chrome */
	-o-animation: myfirst 8s linear 1s infinite normal;
	/* Opera */
}

@keyframes myfirst {
	0% {background-image: url(../img/pic1.jpg);}
	35% {background-image: url(../img/pic2.jpg);}
	70% {background-image: url(../img/pic3.jpg);}
	100% {background-image: url(../img/pic1.jpg);}
}

@-moz-keyframes myfirst

/* Firefox */
	{
	0% {
		background-image: url(../img/pic1.jpg);
		
	}

	35% {
		background-image: url(../img/pic2.jpg);
		
	}

	70% {
		background-image: url(../img/pic3.jpg);
		
	}

	100% {
		background-image: url(../img/pic1.jpg);
		
	}
}

@-webkit-keyframes myfirst

/* Safari and Chrome */
	{
	0% {
		background-image: url(../img/pic1.jpg);
		
	}

	35% {
		background-image: url(../img/pic2.jpg);
		
	}

	70% {
		background-image: url(../img/pic3.jpg);
		
	}

	100% {
		background-image: url(../img/pic1.jpg);
		
	}
}

@-o-keyframes myfirst

/* Opera */
	{
	0% {
		background-image: url(../img/pic1.jpg)
	}

	25% {
		background-image: url(../img/pic2.jpg)
	}

	50% {
		background-image: url(../img/pic3.jpg)
	}

	100% {
		background-image: url(../img/pic1.jpg)
	}
}

css使用animation实现往左逐渐移动效果

html部分

		<div>
			<ul>
				<li><img src="img/pic1.jpg" /></li>
				<li><img src="img/pic2.jpg" /></li>
				<li><img src="img/pic3.jpg" /></li>
			</ul>
		</div>

css

dl, ol, ul {
    padding: 0;
}
div {
	position: absolute;
	left: 200px;
	right: 200px;
	top: 22px;
	width: 1000px;
	height: 561px;
	background: #ffffff;
	overflow: hidden;
}

li {
	float: left;
	width: 1000px;
	height: 561px;
}

ul {
	width: 3000px;
	height: 561px;

	
	/* animation-iteration-count: infinite; //动画播放次数 无限次 */
	animation: myfirst 50s ease-in-out 0s infinite normal;
	
	-moz-animation: myfirst 50s ease-in-out 0s infinite normal;
	/* Firefox */
	-webkit-animation: myfirst 50s ease-in-out 0s infinite normal;
	/* Safari and Chrome */
	-o-animation: myfirst 50s ease-in-out 0s infinite normal;
	/* Opera */
}




@keyframes myfirst {
	0% {
		margin-left: 0px;
	}

	20% {
		margin-left: -1000px;
	}
	25% {
		margin-left: -1000px;
	}

	45% {
		margin-left: -2000px;
	}
	50% {
		margin-left: -2000px;
	}
	
	70% {
		margin-left: -1000px;
	}
	75% {
		margin-left: -1000px;
	}
	
	95% {
		margin-left: 0px;
	}
	100% {
		margin-left: 0px;
	}


}

@-moz-keyframes myfirst

/* Firefox */
	{
	0% {
		margin-left: 0px;
	}

	20% {
		margin-left: -1000px;
	}
	25% {
		margin-left: -1000px;
	}

	45% {
		margin-left: -2000px;
	}
	50% {
		margin-left: -2000px;
	}
	
	70% {
		margin-left: -1000px;
	}
	75% {
		margin-left: -1000px;
	}
	
	95% {
		margin-left: 0px;
	}
	100% {
		margin-left: 0px;
	}
}

@-webkit-keyframes myfirst

/* Safari and Chrome */
	{
	0% {
		margin-left: 0px;
	}

	20% {
		margin-left: -1000px;
	}
	25% {
		margin-left: -1000px;
	}

	45% {
		margin-left: -2000px;
	}
	50% {
		margin-left: -2000px;
	}
	
	70% {
		margin-left: -1000px;
	}
	75% {
		margin-left: -1000px;
	}
	
	95% {
		margin-left: 0px;
	}
	100% {
		margin-left: 0px;
	}
}

@-o-keyframes myfirst

/* Opera */
	{
	0% {
		margin-left: 0px;
	}

	20% {
		margin-left: -1000px;
	}
	25% {
		margin-left: -1000px;
	}

	45% {
		margin-left: -2000px;
	}
	50% {
		margin-left: -2000px;
	}
	
	70% {
		margin-left: -1000px;
	}
	75% {
		margin-left: -1000px;
	}
	
	95% {
		margin-left: 0px;
	}
	100% {
		margin-left: 0px;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在uniapp中点击左右箭头左右滑动效果,你可以使用uni-swiper组件。uni-swiper是一个轮组件,可以实现左右滑动切换图片、内容等效果。 使用方法如下: 1. 在你的页面中引入uni-swiper组件: ```vue <template> <view class="swiper-container"> <uni-swiper :autoplay="true"> <uni-swiper-item> <image src="/static/img/1.jpg"></image> </uni-swiper-item> <uni-swiper-item> <image src="/static/img/2.jpg"></image> </uni-swiper-item> <uni-swiper-item> <image src="/static/img/3.jpg"></image> </uni-swiper-item> </uni-swiper> </view> </template> <script> import uniSwiper from '@/components/uni-swiper/uni-swiper.vue' import uniSwiperItem from '@/components/uni-swiper/uni-swiper-item.vue' export default { components: { uniSwiper, uniSwiperItem } } </script> ``` 其中,uni-swiper表示轮组件,uni-swiper-item表示轮项,可以在里面放置图片、文字、按钮等内容。 2. 配置uni-swiper组件的属性和事件: ```vue <uni-swiper :autoplay="true" @change="swiperChange"> <uni-swiper-item> <image src="/static/img/1.jpg"></image> </uni-swiper-item> <uni-swiper-item> <image src="/static/img/2.jpg"></image> </uni-swiper-item> <uni-swiper-item> <image src="/static/img/3.jpg"></image> </uni-swiper-item> </uni-swiper> ``` 其中,autoplay表示是否自动轮(默认为false),change表示轮切换时触发的事件。 3. 在页面中添加样式: ```css .swiper-container { height: 200rpx; overflow: hidden; } .swiper-item { display: flex; justify-content: center; align-items: center; height: 100%; } .swiper-item img { width: 100%; height: 100%; object-fit: cover; } ``` 这样,你就可以在uniapp中实现点击左右箭头左右滑动效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值