我想要实现的目标
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/