简体   繁体   English

使用javascript滑动div

[英]slide a div using javascript

I wanted to write a javascript code that will slide a div in specific direction, distance and in some given time. 我想编写一个JavaScript代码,将div沿特定方向,距离和给定时间滑动。 I wrote this small script. 我写了这个小脚本。 but doesn't work at all. 但根本不起作用。 Instead browser gets slow. 而是浏览器变慢了。 No change in position is visible. 看不到位置变化。 Can someone tell me how to achieve the result ? 有人可以告诉我如何取得结果吗? I know there are many ready made libraries that can do this easily. 我知道有很多现成的库可以很容易地做到这一点。 But I just wanted to give it a try. 但我只是想尝试一下。

<script type="text/javascript" language="javascript">
    var element = '';
    var slidePerMS = '';
    function slideIt(ele, direction, distance, slideDuration){
        element = ele;
        var i=0;
        slidePerMS = distance / (slideDuration*1000);
        for(i=0; i<3000; i++){
            setTimeout("changePosition()",1);
    }
}
function changePosition(){              
    var currElement = document.getElementById(element);
    currElement.style.left = "'"+slidePerMS+"px'";
}
</script>

SOO many things wrong with that code it's not even funny... Let's see... 该代码有很多错误,甚至不好笑...让我们看看...

  1. You are trying to render a 1,000 FPS animation. 您正在尝试渲染1,000 FPS动画。 This is simply impossible for a browser. 对于浏览器来说,这根本是不可能的。
  2. You are passing a string as parameter to setTimeout , which is as evil as eval . 您正在将字符串作为参数传递给setTimeout ,它与eval一样邪恶。
  3. You set slidePerMS once but never change it after, resulting in the div being moved to the exact same spot over and over. 您只设置slidePerMS一次slidePerMS ,但之后再也不会对其进行更改,从而导致div一遍又一遍地移到完全相同的位置。
  4. You are setting the style with extra quotes inside - do you put quotes in a CSS file? 您要在样式内设置额外的引号-您是否将引号放在CSS文件中?

That's to name but a few. 仅举几例。 Try this instead: 尝试以下方法:

<script type="text/javascript" language="javascript"> 
function slideIt(elem, direction, distance, slideDuration){ 
    var elmt = document.getElementById(elem),
        i=0, step = distance / (slideDuration*20),
        stepper = setInterval(function() {
            i = Math.min(distance,i+step);
            elmt.style.left = i+'px';
            if( i == distance) clearInterval(stepper);
        },50);
}
</script>

You have many problems. 你有很多问题。

You are treating setTimeout as if it was sleep . 您正在将setTimeout当作sleep Don't do that. 不要那样做 It isn't like sleep at all, it runs a function after a given period of time, but doesn't pause the execution of anything else. 它根本不像sleep ,它在给定的时间段后运行一个函数,但不会暂停其他任何事情的执行。

This means you just hammer the function repeatedly 3000 times, which is what is locking up the browser. 这意味着您只需反复敲击该功能3000次,这就是锁定浏览器的原因。

Instead of using a for loop, you should be using setInterval . 您应该使用setInterval而不是使用for循环。

Don't pass a string to setInterval (or setTimeout ), it gets evaled, which is slow and hard to debug, and it breaks scope. 不要将字符串传递给setInterval (或setTimeout ),它会被逃避,这很慢且难以调试,并且会破坏作用域。 Pass a function instead. 而是传递一个函数。

Inside changePosition you are trying to use a variable called slidePerMS , which is undefined because it is defined in the scope of slideIt . changePosition内部,您尝试使用一个名为slidePerMS的变量,该变量undefined因为它是在slideIt的范围内定义的。

You are also trying to set left to "'123px'" . 你也尝试设置left ,以"'123px'" You can't quote your values in CSS. 您不能在CSS中引用您的值。

Get rid of both the ' s. 摆脱这两个的'秒。

This is why you can't see any change. 这就是为什么您看不到任何变化的原因。 Invalid values are ignored in CSS. 无效值在CSS中会被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM