JS实现图片自动播放

news/2024/7/19 15:26:34 标签: JS, CSS

一、JS实现图片自动播放

1、先看效果图

2、完整代码

<!DOCTYPE html>
<html>
<head>
	<style>
		/* 定义样式 */
		body{
			margin: 5% 30%;
		}
		.bannerimage{width:700px;height:400px;float:left;background-size:100% 100%;color:#fff;box-shadow: 0 0 12px 2px  #142732;}
		.box{width:700px;height:400px;margin:0px auto;overflow: hidden;}
        /* box的宽度为img的个数乘以bannerimage的宽度*/
		.img-g{width:4900px;height:400px;position:relative;}
		.img-g img{float:left;width:700px;height:400px;}
		.button-g{position:relative;top:-35px;text-align:center;}
		.button-g span{display:inline-block;position:relative;z-index:10;width:10px;height:10px;margin:0 5px;border-radius:100%;cursor: pointer;}
	</style>

	<script type="text/javascript" src="js/jquery.js"></script>

	<script type="text/javascript">
	$(function () {
	    // 实现自动播放
		var p=document.getElementsByClassName('img-g')[0];
		var button=document.querySelectorAll('.button-g span')
		// 设置3秒播放
		window.timer=setInterval(move,3000);
		// 轮播设置
		function move(){
		    // bannerimage的宽度乘以图片的个数
			if(parseInt(p.style.left)>-4200){
			    // 和bannerimage的宽度保持一致即可:700
				p.style.left=parseInt(p.style.left)-700+'px'
				p.style.transition='left 1s';
				tog(-Math.round(parseInt(p.style.left)/700))
				if(parseInt(p.style.left)<=-4200){
					setTimeout(function(){
						tog(0)
						p.style.left='0px'
						p.style.transition='left 0s';
					},1000)
				}
			}else{
				p.style.left='0px'
				p.style.transition='left 0s';
			}
		}

		// 设置小圆点
		for(var i=0;i<button.length;i++){
			// button[i].style.backgroundColor='#eee';
			button[i].onclick=function(){
				p.style.left=-700*this.getAttribute('data-index')+'px'
				tog(this.getAttribute('data-index'))
				clearInterval(window.timer)
				window.timer=setInterval(move,3000);
			}
		}
		// 设置小圆点
		function tog(index){
			if(index>5){
				tog(0);
				return;
			}
			for(var i=0;i<button.length;i++){
				button[i].style.backgroundColor='rgba(255, 255, 255, 0.5)';
			}
			button[index].style.backgroundColor='rgb(255, 255, 255)';
		}

		// 鼠标移上事件
		p.onmouseover=function(){
			clearInterval(window.timer)
		}
        // 鼠标移除事件
		p.onmouseout=function(){
			window.timer=setInterval(move,3000);
		}
	});
	</script>
</head>
<body>	
	<div class="bannerimage">
		<div class='box'>
			<div class='img-g' style='left:0px;transition:left 1s;'>
				<img src="images/img_1.jpg" alt="1">
				<img src="/images/img_2.jpg" alt="2">
				<img src="/images/img_3.jpg" alt="3">
				<img src="/images/img_4.jpg" alt="4">
				<img src="/images/img_5.jpg" alt="5">
				<img src="/images/img_6.jpg" alt="6">
				<img src="/images/img_1.jpg" alt="1">
			</div>
			<div class='button-g'>
				<span data-index='0' style="background-color: #eeeeee"></span>
				<span data-index='1' style="background-color: rgba(255, 255, 255, 0.5)"></span>
				<span data-index='2' style="background-color: rgba(255, 255, 255, 0.5)"></span>
				<span data-index='3' style="background-color: rgba(255, 255, 255, 0.5)"></span>
				<span data-index='4' style="background-color: rgba(255, 255, 255, 0.5)"></span>
				<span data-index='5' style="background-color: rgba(255, 255, 255, 0.5)"></span>
			</div>
		</div>
	</div>
</body>
</html>

3、关键代码讲解

3.1、css设置注意事项:img-g的宽度为:img的个数乘以bannerimage的宽度,如下:

.img-g{width:4900px;height:400px;position:relative;}

3.2、轮播常量及事件设置

常量1设置为:bannerimage的宽度乘以图片的个数,如下:

if(parseInt(p.style.left)>-4200){}

 常量2设置为:bannerimage的宽度保持一致即可(700),如下:

p.style.left=parseInt(p.style.left)-700+'px'

小圆点显示设置:

// 设置小圆点
for(var i=0;i<button.length;i++){
	button[i].style.backgroundColor='#eee';
	button[i].onclick=function(){
	    p.style.left=-700*this.getAttribute('data-index')+'px'
	    tog(this.getAttribute('data-index'))
	    clearInterval(window.timer)
	    window.timer=setInterval(move,3000);
	}
}
// 设置小圆点点击事件
function tog(index){
    // 圆点的个数
	if(index>5){
		tog(0);
		return;
	}
	for(var i=0;i<button.length;i++){
        // 默认圆点的显示颜色
		button[i].style.backgroundColor='rgba(255, 255, 255, 0.5)';
	}
    // 当前圆点的显示颜色
	button[index].style.backgroundColor='rgb(255, 255, 255)';
}

鼠标事件:鼠标移上停止播放,移除3秒后播放。

// 鼠标移上事件
p.onmouseover=function(){
	clearInterval(window.timer)
}
// 鼠标移除事件
p.onmouseout=function(){
	window.timer=setInterval(move,3000);
}

欢迎关注

 


http://www.niftyadmin.cn/n/857091.html

相关文章

matlab计算奇异值和正交矩阵,奇异值、奇异矩阵、SVD分解、正交矩阵

奇异值&#xff1a;奇异值分解法是线性代数中一种重要的矩阵分解法&#xff0c;在信号处理、统计学等领域有重要应用。定义&#xff1a;设A为m*n阶矩阵&#xff0c;A表示A的转置矩阵&#xff0c;A*A的n个特征值的非负平方根叫作A的奇异值。记为σi(A)。如果把A‘*A的特征值记为…

使用My97DatePicker封装一个双选日期组件

由于项目中使用My97DatePicker作为日期控件&#xff0c;额外不用其它日期组件。但是项目需要用到双选日期&#xff0c;一方面放两个空间有限&#xff0c;另一方面看起也不太美观&#xff0c;所以就简单的自己封装一个。 一、准备工作 My97DatePicker下载&#xff1a;http://ww…

jpa oracle默认序列,為什么使用帶有Oracle 10g方言的Hibernate使用JPA創建一個名為hibernate_sequence的序列?...

4I see the following code in org.hibernate.id.SequenceGenerator:我在org.hibernate.id.SequenceGenerator中看到以下代碼:public void configure(Type type, Properties params, Dialect dialect) throws MappingException {ObjectNameNormalizer normalizer ( ObjectName…

关闭linux系统命令是什么,linux shutdown关闭系统命令使用介绍

关闭、重启系统Linux是一个多用户、多任务系统&#xff0c;如果不正确地关闭或重启系统&#xff0c;可能会导致系统中的用户数据丢失。可能的情况是用户正在执行某个关键的运算或操作等&#xff0c;如果不经提示关闭系统&#xff0c;用户将来不及保存当前数据&#xff0c;从而导…

内网穿透之HTTP穿透

一、内网穿透 在很多开发的时候需要临时体验开发时往往没有公网域名或者公网IP&#xff0c;本篇介绍一个有钉钉官方提供的一个公网代理服务&#xff0c;目的是方便开发测试。 二、准备工作 1、官方文档&#xff1a;https://open-doc.dingtalk.com/microapp/kn6zg7/hb7000 2、…

.a 和.o 合并成一个.a_泉美环保为您讲解地埋式污水处理设备A/O工艺详情

不管什么类型的废水处理设备工艺都是其中的核心部分&#xff0c;而A/O工艺可以说是目前地埋式污水处理设备使用比较广泛成熟的一种工艺&#xff0c;虽然算不上很先进的工艺但是胜在处理效果稳定、质优价廉&#xff0c;还是比较推荐大家了解使用的。今天泉美环保公司就给您详解介…

JS实现数字翻牌效果

JS实现数字变化翻牌效果 一、效果图如下&#xff1a; 二、代码如下&#xff1a; 注意&#xff1a;需要引入JQuery.js。 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>数字翻牌器</title>&…

linux内核运行关系图,[科普] Linux 的内核与 Linux 系统之间的关系

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼在 FHS 协议里&#xff0c;有这样的规定&#xff1a;/bin/ 需要在单用户模式可用的必要命令(可执行文件)&#xff1b;面向所有用户&#xff0c;例如&#xff1a; cat、 ls、 cp。/boot/ 引导程序文件&#xff0c;例如&#xff1a; …