简体   繁体   中英

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. 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.

('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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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