javascript - 修复 if 语句以停止移动

标签 javascript jquery scroll attr

我想要实现的目标

racer-offset 是为了让用户可以设置图像可以以 px 为单位移动多远。偏移量管理偏移量。 Speed-racer 告诉我们图像在滚动过程中移动的速度。我的问题是它不会停止。我可以提供一个 fiddle ,但让我们先尝试看看如果没有它是否可以轻松修复。谢谢。

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);
            }
        });
    }


});

唯一需要解决的问题是我需要 speed-racer 添加到 if 部分中的 racer 偏移量。

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

最佳答案

出于某种原因,您将 this 放在引号内。

$(this)$('this') 不同。

if($('this').

应该

if($(this).

关于javascript - 修复 if 语句以停止移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20556164/

相关文章:

javascript - jQuery/JavaScript 碰撞检测

jquery - 滚动时点发生变化

android - 如果屏幕太小, View 应该滚动

javascript - jQuery 验证 addmethod - 如何返回不同的语言消息?

javascript - 如何使用 Javascript 在 Safari HTML5 播放器中使用 iOS 锁屏上一个/下一个播放器按钮

javascript - 获取 iframe 的当前位置/url 并分配给变量

javascript - 通过 jQuery 隐藏前 n 个元素

vba - 使我的解析器向下滚动时出现问题

javascript - FormData 不发送附加文件

javascript - jQuery 使用最接近时无法获取输入字段的值