首页 > 语言 > JavaScript > 正文

javascript实现动态改变层大小的方法

2024-05-06 16:20:11
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了javascript实现动态改变层大小的方法,涉及javascript操作页面属性的相关技巧,需要的朋友可以参考下

本文实例讲述了javascript实现动态改变层大小的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <html xmlns="http://www.w3.org/1999/xhtml"> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <title>动态设置层的大小</title> 
  5. <style type="text/css"> 
  6. .divMain 
  7. { 
  8. width:10px; 
  9. height:100px; 
  10. border-style:solid; 
  11. border-width:1px; 
  12. border-color:Green; 
  13. display:none;  
  14. } 
  15. </style> 
  16. <script type="text/javascript"> 
  17. var setIntervalID; 
  18. function ZoomDiv() { 
  19. var divMain = document.getElementById("divMain"); 
  20. divMain.style.width = "200px"; 
  21. divMain.style.height = "200px"; 
  22. } 
  23. function ShowDiv() { 
  24. setIntervalID = setInterval("inc()", 100); 
  25. } 
  26. function inc() { 
  27. var divMain = document.getElementById("divMain2"); 
  28. //div不能设置class,否则使用divMain.style.width取到的值时空的 
  29. //只能在元素里设置style="width:10px;height:100px" 
  30. var oldWidth = divMain.style.width; 
  31. var oldHeight = divMain.style.height; 
  32. oldWidth = parseInt(oldWidth); 
  33. oldHeight = parseInt(oldHeight); 
  34. oldWidth += 1; 
  35. oldHeight += 1; 
  36. if (oldWidth >= 200) { 
  37. //清除定时器 
  38. clearInterval(setIntervalID); 
  39. return; 
  40. } 
  41. divMain.style.width = oldWidth + "px"; 
  42. divMain.style.height = oldHeight + "px"; 
  43. } 
  44. </script> 
  45. </head> 
  46. <body> 
  47. <input type="button" value="放大层" onclick="ZoomDiv()" /> 
  48. <input type="button" value="动态放大层" onclick="ShowDiv()" /> 
  49. <div id="divMain" class="divMain"> 
  50. 案例:跟着鼠标飞的图片。提示:鼠标移动的事件时onmousemove,通过 
  51. window.event的clientX,clientY属性获得鼠标的位置 
  52. </div> 
  53. <!--这里不能设置class,否则使用divMain.style.width取到的值时空的--> 
  54. <div id="divMain2" style="width:10px;height:100px;background-color:Red;"> 
  55. 案例:跟着鼠标飞的图片。提示:鼠标移动的事件时onmousemove,通过 
  56. window.event的clientX,clientY属性获得鼠标的位置 
  57. </div> 
  58. </body> 
  59. </html> 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选