87,916
社区成员
发帖
与我相关
我的任务
分享
var intervalId = null;
function slideAd(id,nStayTime,sState,nMaxHth,nMinHth)
{
this.stayTime=nStayTime*500 || 100;
this.maxHeigth=nMaxHth || 490;
this.minHeigth=nMinHth || 1;
this.state=sState || "down" ;
var obj = document.getElementById(id);
if(intervalId != null)window.clearInterval(intervalId);
function openBox()
{
var h = obj.offsetHeight;
obj.style.height = ((this.state == "down") ? (h + 10) : (h - 10))+"px";
if(obj.offsetHeight>this.maxHeigth)
{
window.clearInterval(intervalId);
intervalId=window.setInterval(closeBox,this.stayTime);
}
if (obj.offsetHeight<this.minHeigth)
{
window.clearInterval(intervalId);
obj.style.display="none";
}
}
function closeBox()//
{
slideAd(id,this.stayTime,"up",nMaxHth,nMinHth);
}
intervalId = window.setInterval(openBox,10);
}
<html>
<head>
<title>Test</title>
<style type="text/css">
body{
margin: 0px;
padding: 0px;
}
</style>
</head>
<body onload="">
<div id="pic" style="height: 200px; width:300px; border: solid red 1px;overflow: hidden">
<!--img src="http://avatar.profile.csdn.net/2/5/E/2_sunxing007.jpg" /-->
</div>
</body>
<script>
var intervalId = null;//定时器
function slideAd(id,nStayTime,sState,nMaxHth,nMinHth)
{
this.stayTime=nStayTime*500 || 100;//如果参数中有nStayTime,则stayTime = (stayTime乘以500), 否则取值为stayTime=100。
this.maxHeigth=nMaxHth || 490;//整个广告的最大高度
this.minHeigth=nMinHth || 1;//;//整个广告的最xiao高度
this.state=sState || "down" ;//方向
var obj = document.getElementById(id);//展示广告的容器,我用的是上面的div (id=pic),
if(intervalId != null)window.clearInterval(intervalId);//如果正在运行,则中指定时器
function openBox()
{
var h = obj.offsetHeight;//容器的高度
obj.style.height = ((this.state == "down") ? (h + 10) : (h - 10));//如果方向朝下,则每次高度+10,否则-10
if(obj.offsetHeight>this.maxHeigth)//如果大于最大高度了,则开始变小
{
window.clearInterval(intervalId);
intervalId=window.setInterval(closeBox,this.stayTime);
}
if (obj.offsetHeight<this.minHeigth)
{
window.clearInterval(intervalId);
obj.style.display="none";
}
}
function closeBox()//
{
slideAd(id,this.stayTime,"up",nMaxHth,nMinHth);
}
intervalId = window.setInterval(openBox,10);
}
slideAd('pic');//开始调用
// slideAd('pic', 2,'down',500, 100);//也可以这样调用
</script>
</html>
//声明变量
var intervalId = null;
//定义方法
function slideAd(id,nStayTime,sState,nMaxHth,nMinHth)
{
this.stayTime=nStayTime*500 || 100;
this.maxHeigth=nMaxHth || 490;
this.minHeigth=nMinHth || 1;
this.state=sState || "down" ;
var obj = document.getElementById(id);
//如果不为null 则删除setInterval 产生的对象
if(intervalId != null)window.clearInterval(intervalId);
function openBox()
{
var h = obj.offsetHeight;
obj.style.height = ((this.state == "down") ? (h + 10) : (h - 10))+"px";
if(obj.offsetHeight>this.maxHeigth)
{
window.clearInterval(intervalId);
//调用closeBox方法
intervalId=window.setInterval(closeBox,this.stayTime);
}
if (obj.offsetHeight<this.minHeigth)
{
window.clearInterval(intervalId);
obj.style.display="none";
}
}
function closeBox()//
{
slideAd(id,this.stayTime,"up",nMaxHth,nMinHth);
}
//每隔10毫秒调用下openBox方法
intervalId = window.setInterval(openBox,10);
}