【问题标题】:Fixing if statement to stop movement修复 if 语句以停止移动
【发布时间】:2013-12-31 14:40:44
【问题描述】:

我正在努力实现的目标

racer-offset 是为了让用户可以设置图像可以移动多远(以 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);
            }
        });
    }


});

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

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

【问题讨论】:

    标签: javascript jquery scroll attr


    【解决方案1】:

    由于某种原因,您在引号内有 this..

    $(this)$('this') 不一样。

    if($('this').
    

    应该是

    if($(this).
    

    【讨论】:

    • 我想我需要改变它。我需要做的是创建一个名为 leftmovement 的racer-offset 的副本,然后从racer-offset 中减去直到它达到0。如果它达到0,它会停止,然后重新开始计数,这意味着不朝另一个方向前进- 但是+。
    【解决方案2】:

    你的这部分代码不应该是:

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

    警告:但未经测试。

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2019-02-26
      • 1970-01-01
      相关资源
      最近更新 更多