【发布时间】: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