正在修复if语句以停止移动

Fixing if statement to stop movement

本文关键字:移动 语句 if      更新时间:2023-09-26

我试图实现的目标

racer偏移是为了让用户可以设置一个图像可以在px中移动多远。偏移量管理它偏移了多远。Speed-racer告诉图像在滚动过程中移动的速度。我的问题是它不会停止。我可以提供一把小提琴,但让我们先试试,看看没有它是否很容易修复。谢谢。

HTML

 <img class="images" racer-offset="250" speed-racer="3" src="http://assets/img/fwman/FeeneyMan_bike.png" loc="576" style="left: 540px;" offsetted="924">

Javascript

    $(window).scroll(function(){
    $('.images').each(function(){
        if($('this').attr('racer-offset') < $(this).attr('offsetted')){
        }
        else {
    var speedR = $(this).attr('speed-racer');
    $('.images').css({left : "-="+speedR});
    $('.images').attr('offsetted', $(this).position().left);
    }
    });
});
   $('.images').each(function(){
        $(this).attr('loc', $(this).position().left);
   });

新版本

$(window).scroll(function(){
    if(lastLeftLocation > $(document).scrollLeft()) {
        $('.images').each(function(){
                if($(this).position().left >= $(this).attr('loc')){
                console.log("0 == stopped");
                }
                else {
                speedR = $(this).attr('speed-racer');
                $(this).css({left : "+="+speedR});
                $(this).attr('racer-offset') + speedR;
                $(this).attr('racer-offset', $(this).attr('speed-racer') + $(this).attr('racer-offset'));
                }
            });
        }
    else {
        $('.images').each(function(){
            if($(this).attr('racer-offset') <= 0){
            console.log("0 == stopped");
            }
            else {
            speedR = $(this).attr('speed-racer');
            $(this).css({left : "-="+speedR});
            $(this).attr('racer-offset', $(this).attr('racer-offset') - speedR);
            }
        });
    }

});

唯一需要修复的是,我需要速度赛车手在if部分添加赛车手偏移。

       $(this).attr('racer-offset', $(this).attr('speed-racer') + $(this).attr('racer-offset'));

由于某些原因,您的引号中有this。。

$(this)$[('this')

if($('this').

应该是

if($(this).

代码的这一部分不应该是:吗

var speedR = $(this).attr('speed-racer');
//$('.images').css({left : "-="+speedR});
$(this).css({left: "-="+speedR});
//$('.images').attr('offsetted', $(this).position().left);
$(this).attr('offsetted', $(this).position().left);
}

注意:不过没有测试。