DOM案例---經典的仿163首頁伸縮廣告

來源:互聯網
上載者:User

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>仿163首頁的廣告收縮效果</title>
<style>
#top{
 position:relative;
}
#content{
 position:absolute;
}
 
</style>
</head>
<body onload="moveDown()" style="margin:0px; 0px;">
<div id="top" style="width:100%; height:100px; background-color:#F96">網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
網頁的廣告部分<br>
</div>
<div id="content" style="width:100%; height:500px; background-color:#FF9">這是網頁的本文部分</div>
</body>
</html>
<script language="javascript" type="text/javascript">
  //定義要移動的距離
  var h=0;
  var maxheight=300;
  var st;
  var top=document.getElementById("top");
  //初始時,設定top為隱藏狀態
   top.style.display="none";
  //定義讓廣告部分展開
  function moveDown(){
   //累加 
   h+=2;
   //設定高度等於我們累加的值
   top.style.height=h;
   //設定層顯示
   top.style.display="block";
   //判斷是否到最大
   if(h<=maxheight){
 
   st=setTimeout("moveDown()",50); 
   
  }else{
 
  clearTimeout(st);
  //延遲3秒,開始收縮
  setTimeout("moveUp()",3000);
 
  }
  
  }
 
  //定義讓廣告部分收縮
  function moveUp(){
   //設定 
   h-=2;
   //設定高度等於我們累加的值
   top.style.height=h;
   //設定層顯示
   top.style.display="block";
  
   if(h<=0){
   
    top.style.display="none";
   
    //清空定時器
    clearTimeout(st);
   
   }else{
   
    //如果h>0,則繼續收縮
   st= setTimeout("moveUp()",50);
   
    }
  
  
 }

 
</script>


摘自 劉海闖的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.