模仿jquery写了隐藏显示函数show、hide,好用

- 日理万妓 2021-07-25 02:01 335阅读 0赞
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=GBK">
  5. </head>
  6. <body>
  7. <div id="div" style="width:70px;height:50px;background:skyblue;left:25%;"><p>div</p></div>
  8. <button onclick="do_show()">show Div</button>
  9. <button onclick="do_hide()">hide Div</button>
  10. <button onclick="do_toggle()">toggle Div</button>
  11. </body>
  12. <script>
  13. //获取属性值
  14. function getStyle(obj, prop) {
  15. var prevComputedStyle = document.defaultView ? document.defaultView.getComputedStyle( obj, null ) : obj.currentStyle;
  16. return prevComputedStyle[prop];
  17. }
  18. /*
  19. obj:dom对象
  20. prop:动画参数
  21. speed:执行速度 fast slow 3000等
  22. func:回调函数
  23. */
  24. function animate(obj,prop,speed,func){
  25. //防止重复动画事件
  26. if(obj.timer) return ;
  27. //定义定时器执行次数和总执行时间
  28. var limit=10,totalTime;
  29. if(typeof speed==='number'){//如果传入的是
  30. totalTime = speed;
  31. }else if(speed==='slow'){
  32. totalTime = 600;
  33. }else if(speed==='fast'){
  34. totalTime = 200;
  35. }else{
  36. totalTime = 400;
  37. }
  38. var time = totalTime/limit;
  39. var n=0,cache={},display,primary_cur;//cache用来缓存,省的每次都去dom获取
  40. obj.timer = setInterval(function(){
  41. n++;//执行次数每次递增
  42. for(var p in prop){
  43. if("display"===p) {
  44. display = prop["display"];
  45. if(display!=='none'){
  46. obj.style['display'] = display;
  47. }
  48. delete prop["display"];
  49. continue;
  50. }
  51. //判断是否是可以递增的属性,如果不是则直接让它生效,并从prop中删除,删除后就不用每次任务都执行它
  52. var reg = /^(\d)+(px$)?/;//数字和像素这样的判定为可以递增的属性
  53. if(!reg.test(prop[p])){
  54. obj.style[p] = prop[p];
  55. delete prop[p];
  56. continue;
  57. }
  58. var value,opacityFlag=(p == "opacity")?true:false;
  59. var cur = 0;
  60. if(cache[p+"_cur"]){//从缓存中取
  61. cur = cache[p+"_cur"];
  62. value = cache[p+"_value"];
  63. }else{
  64. value = prop[p];
  65. if(opacityFlag) {
  66. //如果本来是隐藏的则cur默认就是0
  67. if(getStyle(obj, 'display')!=='none'){
  68. cur = Math.round(parseFloat(getStyle(obj, p)) * 100);
  69. }
  70. } else {
  71. cur = parseInt(getStyle(obj, p));
  72. //处理100px的格式
  73. (typeof value==='string') && (value=value.replace(/px$/,""));
  74. }
  75. primary_cur=cur;
  76. cache[p+"_value"] = value;
  77. }
  78. var incre ;
  79. if(cache[p+'_increment']){//如果缓存中有则从中取
  80. incre = cache[p+'_increment'];
  81. }else{
  82. if(opacityFlag){
  83. incre = (value*100-cur)/limit;//计算每次变化值
  84. }else{
  85. incre = (value-cur)/limit;//计算每次变化值
  86. }
  87. cache[p+'_increment']= incre;
  88. }
  89. //缓存起来,这样就不用每次都去dom中获取了。
  90. cache[p+"_cur"] = cur + incre;
  91. if (opacityFlag) {
  92. //obj.style.filter = "alpha(opacity : '+(cur + incre)+' )";
  93. obj.style.opacity = (cur + incre)/100 ;
  94. }else {
  95. obj.style[p] = cur + incre + "px";
  96. }
  97. }
  98. //如果达到了最大执行次数,要清除定时器,并执行回调函数
  99. if(n==limit){
  100. if(display==='none'){
  101. obj.style['display'] = 'none';
  102. }
  103. //清除定时器
  104. clearInterval(obj.timer);
  105. obj.timer=undefined;
  106. func && func();
  107. }
  108. },time)
  109. }
  110. var div = document.getElementById("div");
  111. /*
  112. obj:dom对象
  113. speed:执行速度 fast slow 3000等
  114. func:回调函数
  115. */
  116. function hide(obj,speed,func){
  117. if(obj.style.display==='none'){
  118. return ;
  119. }
  120. //获取本来的宽带和高度
  121. var width = getStyle(obj, 'width');
  122. var height = getStyle(obj, 'height');
  123. var new_func = function(){
  124. //设置回原来的高度和宽度
  125. obj.style['width'] = width;
  126. obj.style['height'] = height ;
  127. func();
  128. }
  129. animate(obj,{opacity: 0,display:'none',width:0,height:0},speed,new_func);
  130. }
  131. function show(obj,speed,func){
  132. if(obj.style.display!=='none'){
  133. return ;
  134. }
  135. //获取本来的宽带和高度
  136. var width = getStyle(obj, 'width');
  137. var height = getStyle(obj, 'height');
  138. //设置从0开始
  139. obj.style['width'] = 0;
  140. obj.style['height'] = 0 ;
  141. animate(obj,{opacity: 1,display:'block',width:width,height:height},speed,func);
  142. }
  143. function toggle(obj,speed,func){
  144. if(obj.style.display==='none'){
  145. show(obj,speed,func);
  146. }else{
  147. hide(obj,speed,func);
  148. }
  149. }
  150. //执行显示
  151. function do_show(){
  152. //执行速度 fast slow 3000等
  153. show(div,1000,function(){
  154. console.log('show_finished')
  155. })
  156. }
  157. //执行隐藏
  158. function do_hide(){
  159. //执行速度 fast slow 3000等
  160. hide(div,1000,function(){
  161. console.log('hide_finished')
  162. })
  163. }
  164. function do_toggle(){
  165. toggle(div,'slow',function(){
  166. console.log('toggle_finished')
  167. });
  168. }
  169. </script>
  170. </html>

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JQuery 控制html元素显示隐藏

            在做项目中用到界面元素显示或隐藏的功能。比如:点击编辑按钮,切换复选框的显示隐藏。根据数据库中某一字段的不同值,在界面上显示不同的按钮组合。因此查找了如何控制界

    相关 jquery动态控制div显示隐藏

    对于经常使用到的代码,在博客上备份一下,用到时,如果想不起来,直接copy过去。 最近在做项目的时候,经常卡在前段这方面,所以,对于经常用到的代码,如果一直出错的话,我们直接