问,html中,图片在特定位置,每几秒钟自动切换一张的代码怎么写?求一种最简单的

问,html中,图片在特定位置,每几秒钟自动切换一张的代码怎么写?求一种最简单的,第1张

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<script type="text/javascript">

window.onload=function(){

var oImg=document.getElementById('img1')

var arrImgUrl=["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg"]

var num=0

function Tab(){

num++

if(num>arrImgUrl.length-1){

num=0

}

oImg.src=arrImgUrl[num]

}

setInterval(Tab,1000)

}

</script>

</head>

<body>

<img id="img1" src="img/1.jpg" width="288">

</body>

</html>

注意图片放在 名称为 img 的文件夹中,而且是格式的,否则需要修改html

function changeSrc(nIndex){

var imageList = new Array()

imageList.push("1.jpg")

imageList.push("2.jpg")

var obj = document.getElementById("imagecontain")

obj.src = imageList[nIndex%imageList.length]

}

<div><img id="imagecontain" src="1.jpg" /></div>

<script>

var nIndex = 1

objTimer = window.setInterval(function(){

changeSrc(nIndex)

nIndex++

},1000)

document.getElementById("imagecontain").onmousemove = function(){

clearInterval(objTimer)

}

document.getElementById("imagecontain").onmouseout = function(){

objTimer = window.setInterval(function(){

changeSrc(nIndex)

nIndex++

},1000)

}

</script>

</body>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7067932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-01
下一篇 2023-04-01

发表评论

登录后才能评论

评论列表(0条)

保存