![]() |
#2
aspic2009-09-14 11:46
|

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS动画展开收缩显示层</title>
<style>
* { margin:0; padding:0;}
body { text-align:center; font:75% Verdana, Arial, Helvetica, sans-serif;}
h1 { font:125% Arial, Helvetica, sans-serif; text-align:left; font-weight:bolder; background:#333; padding:3px; display:block; color:#99CC00}
.class1 { width:40%; background:#CCC; position:relative; margin:0 auto; padding:5px;}
span { position:absolute; right:10px; top:8px; cursor:pointer; color:yellow;}
p { text-align:left; line-height:20px; background:#333; padding:3px; margin-top:5px; color:#99CC00}
#class1content1 { height:300px;overflow:hidden}
#class1content2 { height:300px;overflow:hidden}
</style>
<script type="text/javascript">
function $(element){
return element = document.getElementById(element);
}
function $D1(obj){
var h=obj.offsetHeight;
var maxh=300;
function dmove(){
h+=20; //设置层展开的速度
if(h>=maxh){
obj.style.height=maxh+'px';
clearInterval(iIntervalId);
}else{
obj.style.display='block';
obj.style.height=h+'px';
}
}
iIntervalId=setInterval(dmove,2);
}
function $D2(obj){
var h=obj.offsetHeight;
var maxh=300;
function dmove(){
h-=20;//设置层收缩的速度
if(h<=0){
obj.style.display='none';
clearInterval(iIntervalId);
}else{
obj.style.display='block';
obj.style.height=h+'px';
}
}
iIntervalId=setInterval(dmove,2);
}
function $use(obj){
var obj=$(obj);
var sb=$('stateBut');
if(obj.style.display=='none'){
$D1(obj);
sb.innerHTML='<img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" />';
}else{
$D2(obj);
sb.innerHTML='<img name="" src="" width="16" height="16" alt="展开" style="background-color: #FF0000" />';
}
}
</script>
</head>
<body>
<div class="class1">
<h1>展开收缩动画层效果</h1>
<span id="stateBut" onclick="$use('class1content1')"><img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" /></span>
<p id="class1content1">小蜗牛问妈妈:为什么我们从生下来,就要背负这个又硬又重的壳呢?<br />
妈妈:因为我们的身体没有骨骼的支撑,只能爬,又爬不快。所以要这个壳的保护!<br />
小蜗牛:毛虫姊姊没有骨头,也爬不快,为什么她却不用背这个又硬又重的壳呢? <br />
妈妈:因为毛虫姊姊能变成蝴蝶,天空会保护她啊。 <br />
小蜗牛:可是蚯蚓弟弟也没骨头爬不快,也不会变成蝴蝶他什么不背这个又硬又重的壳呢? <br />
妈妈:因为蚯蚓弟弟会钻土, 大地会保护他啊。 <br />
小蜗牛哭了起来:我们好可怜,天空不保护,大地也不保护。 <br />
蜗牛妈妈安慰他:所以我们有壳啊!我们不靠天,也不靠地,我们靠自己。</p>
</div>
<div class="class1">
<h1>展开收缩动画层效果</h1>
<span id="stateBut" onclick="$use('class1content2')"><img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" /></span>
<p id="class1content2">小蜗牛问妈妈:为什么我们从生下来,就要背负这个又硬又重的壳呢?<br />
妈妈:因为我们的身体没有骨骼的支撑,只能爬,又爬不快。所以要这个壳的保护!<br />
小蜗牛:毛虫姊姊没有骨头,也爬不快,为什么她却不用背这个又硬又重的壳呢? <br />
妈妈:因为毛虫姊姊能变成蝴蝶,天空会保护她啊。 <br />
小蜗牛:可是蚯蚓弟弟也没骨头爬不快,也不会变成蝴蝶他什么不背这个又硬又重的壳呢? <br />
妈妈:因为蚯蚓弟弟会钻土, 大地会保护他啊。 <br />
小蜗牛哭了起来:我们好可怜,天空不保护,大地也不保护。 <br />
蜗牛妈妈安慰他:所以我们有壳啊!我们不靠天,也不靠地,我们靠自己。</p>
</div>
</body>
</html>
第二个DIV调用图片有问题,第二个DIV 的 onclick="$use('class1content2') 如何调用 黑色 和 红色 图片。<title>JS动画展开收缩显示层</title>
<style>
* { margin:0; padding:0;}
body { text-align:center; font:75% Verdana, Arial, Helvetica, sans-serif;}
h1 { font:125% Arial, Helvetica, sans-serif; text-align:left; font-weight:bolder; background:#333; padding:3px; display:block; color:#99CC00}
.class1 { width:40%; background:#CCC; position:relative; margin:0 auto; padding:5px;}
span { position:absolute; right:10px; top:8px; cursor:pointer; color:yellow;}
p { text-align:left; line-height:20px; background:#333; padding:3px; margin-top:5px; color:#99CC00}
#class1content1 { height:300px;overflow:hidden}
#class1content2 { height:300px;overflow:hidden}
</style>
<script type="text/javascript">
function $(element){
return element = document.getElementById(element);
}
function $D1(obj){
var h=obj.offsetHeight;
var maxh=300;
function dmove(){
h+=20; //设置层展开的速度
if(h>=maxh){
obj.style.height=maxh+'px';
clearInterval(iIntervalId);
}else{
obj.style.display='block';
obj.style.height=h+'px';
}
}
iIntervalId=setInterval(dmove,2);
}
function $D2(obj){
var h=obj.offsetHeight;
var maxh=300;
function dmove(){
h-=20;//设置层收缩的速度
if(h<=0){
obj.style.display='none';
clearInterval(iIntervalId);
}else{
obj.style.display='block';
obj.style.height=h+'px';
}
}
iIntervalId=setInterval(dmove,2);
}
function $use(obj){
var obj=$(obj);
var sb=$('stateBut');
if(obj.style.display=='none'){
$D1(obj);
sb.innerHTML='<img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" />';
}else{
$D2(obj);
sb.innerHTML='<img name="" src="" width="16" height="16" alt="展开" style="background-color: #FF0000" />';
}
}
</script>
</head>
<body>
<div class="class1">
<h1>展开收缩动画层效果</h1>
<span id="stateBut" onclick="$use('class1content1')"><img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" /></span>
<p id="class1content1">小蜗牛问妈妈:为什么我们从生下来,就要背负这个又硬又重的壳呢?<br />
妈妈:因为我们的身体没有骨骼的支撑,只能爬,又爬不快。所以要这个壳的保护!<br />
小蜗牛:毛虫姊姊没有骨头,也爬不快,为什么她却不用背这个又硬又重的壳呢? <br />
妈妈:因为毛虫姊姊能变成蝴蝶,天空会保护她啊。 <br />
小蜗牛:可是蚯蚓弟弟也没骨头爬不快,也不会变成蝴蝶他什么不背这个又硬又重的壳呢? <br />
妈妈:因为蚯蚓弟弟会钻土, 大地会保护他啊。 <br />
小蜗牛哭了起来:我们好可怜,天空不保护,大地也不保护。 <br />
蜗牛妈妈安慰他:所以我们有壳啊!我们不靠天,也不靠地,我们靠自己。</p>
</div>
<div class="class1">
<h1>展开收缩动画层效果</h1>
<span id="stateBut" onclick="$use('class1content2')"><img name="" src="" width="16" height="16" alt="收起" style="background-color: #000000" /></span>
<p id="class1content2">小蜗牛问妈妈:为什么我们从生下来,就要背负这个又硬又重的壳呢?<br />
妈妈:因为我们的身体没有骨骼的支撑,只能爬,又爬不快。所以要这个壳的保护!<br />
小蜗牛:毛虫姊姊没有骨头,也爬不快,为什么她却不用背这个又硬又重的壳呢? <br />
妈妈:因为毛虫姊姊能变成蝴蝶,天空会保护她啊。 <br />
小蜗牛:可是蚯蚓弟弟也没骨头爬不快,也不会变成蝴蝶他什么不背这个又硬又重的壳呢? <br />
妈妈:因为蚯蚓弟弟会钻土, 大地会保护他啊。 <br />
小蜗牛哭了起来:我们好可怜,天空不保护,大地也不保护。 <br />
蜗牛妈妈安慰他:所以我们有壳啊!我们不靠天,也不靠地,我们靠自己。</p>
</div>
</body>
</html>
黑色 代表箭头朝上的图片收起,红色代表箭头朝下的图片展开。