简体   繁体   English

即使条件为假,我的功能也会自动触发

[英]my function is triggering automaticly even though the condition is false

i have a dragable object that is suppesed to trigger a function when dragged far enough to a side. 我有一个可拖动对象,当拖动到足够远的一侧时,该对象可触发功能。 it is suppossed to trigger a different function depending on which side the object is dragged towards. 假定根据对象向哪一侧拖动触发不同的功能。 i am able to make it work at one side however if i do this it will also automaticly trigger at a different value i have not set. 我能够使其在一侧工作,但是如果我这样做,它也会自动以我未设置的其他值触发。

the move function triggers when the mouse is held down on a specific object and it works perfectly. 当将鼠标按住在特定对象上时,移动功能会触发,并且可以完美运行。 however if i add just about any kind of "<"-condition the code gets messed up. 但是,如果我添加几乎任何类型的“ <”-条件,代码都会陷入混乱。 the code below is supposed to remove the EventListener when when the object is dragged under the 400px mark, however it will also automaticly trigger when ever it is dragged to the right, always at 1000px. 下面的代码应该在将对象拖动到400px标记以下时删除EventListener,但是只要将其向右拖动(始终为1000px),它也会自动触发。 i hope you can help and thanks in advance. 我希望您能有所帮助,并在此先感谢。

function move() {
    if (m.style.left < '400px') {
    m.style.left = event.clientX + 'px';
    LMenu.style.width = event.clientX + 'px';
    } else {
        window.removeEventListener('mousemove', move, true);
    }
}

This is because javascript is comparing two strings here, and not two integers. 这是因为javascript在这里比较两个字符串,而不是两个整数。

('100px' < '400px') \\true
('1000px' < '400px') \\true
('500px' < '400px') \\false
('50px' < '400px') \\false

Try converting these numbers to ints first with javascripts parseInt(String) function, then comparing them. 尝试首先使用javascripts parseInt(String)函数将这些数字转换为int,然后进行比较。

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

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