Javascript应用--浮动的广告图片

本文介绍了一个使用HTML和JavaScript实现的浮动广告案例。该广告能在网页上自动移动,并且可以通过鼠标悬停停止移动,点击按钮手动移动或者关闭广告。代码详细展示了如何通过设置定时器和监听事件来控制广告元素的位置。
Code:
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  5. <title>浮动广告</title>
  6. <scripttype="text/javascript">
  7. varx=1,y=1,movex=0,movey=0;
  8. functionmovead()
  9. {
  10. vardivObj=document.getElementById("adid");
  11. movexmovex=movex+x*5;
  12. moveymovey=movey+y*5;
  13. divObj.style.top=movey;
  14. divObj.style.left=movex;
  15. if(movex+divObj.offsetWidth>=document.body.clientWidth)
  16. x=-1;
  17. elseif(movex<=0)
  18. x=1;
  19. if(movey+divObj.offsetHeight>=document.body.clientHeight)
  20. y=-1;
  21. elseif(movey<=0)
  22. y=1;
  23. }
  24. vartimeid;
  25. functionover()
  26. {
  27. clearInterval(timeid);
  28. }
  29. functionout()
  30. {
  31. fly();
  32. }
  33. functionfly()
  34. {
  35. timeid=setInterval("movead()",10);
  36. }
  37. window.onload=function()
  38. {
  39. fly();
  40. }
  41. functionclosead()
  42. {
  43. vardivObj=document.getElementById("adid");
  44. divObj.style.display="none";
  45. over();
  46. }
  47. </script>
  48. </head>
  49. <body>
  50. <inputtype="button"value="手动广告"onclick="movead()"/><br/>
  51. <divid="adid"style="position:absolute;top:0px;left:0px"onmouseover="over()"onmouseout="out()"><ahref="http://www.sss.com.cn"target="_blank"><imgsrc="2.jpg"height="100px"width="150px"/></a><br/>
  52. <ahref="javascript:void(0)"onclick="closead()">关闭</a></div>
  53. <imgsrc="1.jpg"height="600"width="700"border="2"/>
  54. </body>
  55. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值