css背景图片每3秒自动切换

html-css012

css背景图片每3秒自动切换,第1张

<div class="change">

</div>

<style>

.change {

animation: change 9s steps(1) infinite

background-repeat: no-repeat

background-position: center center

background-size: 100% auto

width: 200px

height: 100px

}

@keyframes change {

0% {

background-image: url(https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png)

}

33% {

background-image: url(https://www.duhongwei.site/img/201809/s1.png)

}

66% {

background-image: url(https://p4.ssl.qhimg.com/t01fcaa9d8a4d24b5f1.png)

}

}

</style>

纯 css 每3秒播一张图片。9s是总共时间,如果是4张图片这里写 12s

经测试,采用Firefox 45.0.1存在闪烁问题,并发现以下浏览器的某个版本:微信内置浏览器、QQ浏览器、Safari手机浏览器及早期的Chrome可能存在类似情况,包括载入闪动,悬停后闪动。分析:Blink对transition中,属性transform的动画渲染存在差异,而Chrome中的最新版本,已经不存在此情况。解决方法:<style>.outter { height: 375px margin: 10px auto width: 500px }.inner{}img:hover { width:110% margin-left:-5% margin-top:-5%}img{width:100% transition:0.4s ease-in-out}</style>复制代码以上CSS,在IE10、IE11、Chrome及Firefox 45.0.1中调试通过:首次载入,及Ctrl+F5刷新,均不会有闪动情况

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>