如何用javascript降低div高度? [英] how can decrease div height with javascript?

查看:70
本文介绍了如何用javascript降低div高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我写了一些代码来增加我的div的高度,如下所示:

i write some code to increase my div's height over time like below :

var  hei = document.getElementById(" div1")。s​​tyle.height;

               

       功能关闭()

        {

            if(hei> 50)

            {

                document.getElementById('div1')。style.height =  hei +'px';

                               


                 开平=  hei- 5  ;
$
            

                setTimeout('Close()',5000);

            }
        }

var  hei= document.getElementById("div1").style.height;
               
        function Close()
        {
            if( hei> 50)
            {
                document.getElementById('div1').style.height =  hei + 'px';
                               
                 hei=  hei- 5  ;
            
                setTimeout('Close()', 5000);
            }
        }

 

< body>

    < div id =" div1"风格= QUOT;宽度:800像素;身高:400px; background-color:#C0C0C0;保证金:50px自动自动自动;">< / div>

   

    < form>

    &NBSP;&NBSP;&NBSP; < input type =" button" value ="关闭框! onclick =" Close()" />

    < / form>

< / body>

<body>
    <div id="div1" style="width:800px; height: 400px; background-color: #C0C0C0; margin: 50px auto auto auto;"></div>
   
    <form>
        <input type="button" value="Close up box!" onclick="Close()"/>
    </form>
</body>

 

但它不起作用:(

请帮我解决我的代码出错的问题?

pls help me where of my code is wrong ?

thnks

推荐答案

尝试这样的事情:

Try something like this:

var hei = parseInt(document.getElementById(" div1")。s​​tyle.height);

var intervalid = window.setInterval('Close();',5000);

var hei=parseInt(document.getElementById("div1").style.height);
var intervalid=window.setInterval('Close();',5000);

function close()

{

   if(hei> 50)

   {

     document.getElementById(" div1")。 .height = hei +'px';

     hei = hei-5;

  }

   else

   {

     window.clearInterval(intervalid);

  }

}

function Close()
{
   if(hei>50)
   {
       document.getElementById("div1").style.height=hei+'px';
       hei=hei-5;
   }
   else
   {
       window.clearInterval(intervalid);
   }
}

请注意,这里的两个主要区别是:

Note that the two main differences here are:

1。 hei变量使用parseInt函数

2. setInte使用rval函数代替setTimeout。这在你的情况下效率更高,因为每次调用Close()方法时执行的方法都少一些(尽管如果你愿意,我认为你的代码将像以前一样使用
setTimeout一次添加parseInt方法)

1. The declaration of the hei variable uses the parseInt function
2. The setInterval function is used instead of the setTimeout. This is more efficient in your case because it is one less method to be executed each time the Close() method is called (although if you prefer, I think your code will work as it was before with setTimeout once you add the parseInt method)

我不知道你从哪里调用Close()方法,但我假设它被调用的点是在声明和初始化之后嘿变量。在我的代码中,Close方法被setInterval方法调用,所以我在我的代码的第2行称它为
(间接地,但它仍然被调用),但我没有在你的调用中看到任何代码它。祝你好运!

I don't know where you call the Close() method from, but I am assuming that the point at which it gets called is after the declaration and initialization of the hei variable. In my code, the Close method get called by the setInterval method, so I call it (indirectly, but it still gets called) on line 2 of my code, but I didn't see any code in yours that calls it. Good Luck!


这篇关于如何用javascript降低div高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆