<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body{
background: #ccc
}
.content{
position: absolute
top: 30%
}
#box1{
position: absolute
border: 1px solid red
transition: all 1s/*过渡时间*/
}
#box2{
position: absolute
border: 1px solid red
transition: all 1s/*过渡时间*/
}
.up{
/*图片向上状态*/
-webkit-transform: translateY(-100%)
transform: translateY(-100%)
opacity: 0
}
.down{
/*图片向下状态*/
-webkit-transform: translateY(100%)
transform: translateY(100%)
opacity: 0
}
</style>
</head>
<body>
<button onclick="fadeInOut()">淡入淡出</button>
<div class="content">
<!--图片1-->
<div id="box1" class=""><img src="https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png"/></div>
<!--图片2-->
<div id="box2" class="down"><img src="https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/w%3D500/sign=c27d4835de3f8794d3ff482ee21a0ead/024f78f0f736afc3290994e8b919ebc4b7451231.jpg"/></div>
</div>
<script>
function fadeInOut(){
console.log(1)
document.getElementById("box1").className="up"
document.getElementById("box2").className=""
}
</script>
</body>
</html>
如下代码可以实现,不过你要首先下载一个JS插件:Jquery,我用的是jquery-1.9.1.min.js,你下载下来后放到网页同级目录就可以了,如入代码适用于2张或多张图片。
<div id="imgbox"><img src="11.jpg" /><img src="22.jpg" />
</div>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function(){
$("#imgbox img").fadeOut(0).eq(0).fadeIn(0)
var i = 0
setInterval(function(){
if($("#imgbox img").length > (i+1)){
$("#imgbox img").eq(i).fadeOut(0).next("img").fadeIn(1000)
i++
}
else{
$("#imgbox img").eq(i).fadeOut(0).siblings("img").eq(0).fadeIn(1000)
i = 0
}
},2000)
})
</script>
//淡入效果(含淡入到指定透明度)function fadeIn(elem, speed, opacity){
/*
* 参数说明
* elem==>需要淡入的元素
* speed==>淡入速度,正整数(可选)
* opacity==>淡入到指定的透明度,0~100(可选)
*/
speed = speed || 20
opacity = opacity || 100
//显示元素,并将元素值为0透明度(不可见)
elem.style.display = 'block'
iBase.SetOpacity(elem, 0)
//初始化透明度变化值为0
var val = 0
//循环将透明值以5递增,即淡入效果
(function(){
iBase.SetOpacity(elem, val)
val += 5
if (val <= opacity) {
setTimeout(arguments.callee, speed)
}
})()
}
//淡出效果(含淡出到指定透明度)
function fadeOut(elem, speed, opacity){
/*
* 参数说明
* elem==>需要淡入的元素
* speed==>淡入速度,正整数(可选)
* opacity==>淡入到指定的透明度,0~100(可选)
*/
speed = speed || 20
opacity = opacity || 0
//初始化透明度变化值为0
var val = 100
//循环将透明值以5递减,即淡出效果
(function(){
iBase.SetOpacity(elem, val)
val -= 5
if (val >= opacity) {
setTimeout(arguments.callee, speed)
}else if (val < 0) {
//元素透明度为0后隐藏元素
elem.style.display = 'none'
}
})()
}
var btns = document.getElementById('demo')
btns.onclick = function(){
fadeIn(btns)
}
btns.onclick = function(){
fadeOut(btns,40)
}
<p id="demo" style="width:300pxheight:300pxbackground:#f00">淡入淡出</p>
试试吧