javascript忽略等式元素大于1的if语句?
我正在使用这段代码为站点上的div背景制作视差效果 它所做的是获取div和页面顶部之间的距离,并使div背景图像开始以20%或我选择的任何值向上移动。它使用jqueries.scrollTop启动一个等式,该等式使用如下等式更改div的背景位置:javascript忽略等式元素大于1的if语句?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用这段代码为站点上的div背景制作视差效果 它所做的是获取div和页面顶部之间的距离,并使div背景图像开始以20%或我选择的任何值向上移动。它使用jqueries.scrollTop启动一个等式,该等式使用如下等式更改div的背景位置: $('.kujoimgpos').css({ 'background-position' : '0 -'+(200 + ((wScroll - scrollStart) *.2)) +'px' }); 现在
$('.kujoimgpos').css({
'background-position' : '0 -'+(200 + ((wScroll - scrollStart) *.2)) +'px'
});
现在,当我测试这个的时候,我使用了*.2,因为我想要一个缓慢移动的图像。但我只是想看看你滚动速度的2到3倍是什么样子。所以我把它改为2,一旦你这样做了,它只会在div顶部到达页面顶部时开始滚动,这是有意义的,因为if语句检查scrollTop是否大于offset()。top,只有在这样做之后,我才意识到我不应该使用offset().top因为我希望在图像第一次接触页面底部时开始滚动,好吧,我们将scrollTop更改为ScrollBottom..哈哈,ScrollBottom将使用与文档实际底部的距离,这是我们不希望的,因此我们创建了一个新的变量,名为ScrollBottomvar ScrollBottom=$(窗口)。scrollTop()+$(窗口).高度()编码>并在我们的if语句中使用它,但在我们的背景位置方程中仍然使用wScroll,因为这只是一种在我们到达屏幕上的该位置后将(1*(您想要的任何百分比))px添加到图像背景位置的方法
因此,通过改变我们控制图像滚动速度的值,它似乎也会影响滚动的开始时间。有什么想法吗
以下是所有相关的html和css:
<section id="acts" class="acts">
<div class="container-fluid" >
<div class="row">
<div class="col-sm-4 col-sm-offset-2 kujopad" id="heightid">
<h1>The Kujo Kings</h1>
<p>
Known for their Ska/Punk anthems and high energy packed out shows, this 20 something band has built a strong Australian-wide following since forming in 2010. The six-piece band are dedicated to entertaining fans with their catchy and humorous tunes, with dangerous amounts of energy, dancing, costumes and gratuitous fun being a consistent feature of their shows, the Kujo Kings are an act not to be missed!
</p>
</div>
<div id="imgstart" class="col-sm-4 kujoimgpos">
<! the image is here >
</div>
</div>
</div>
</section>
好的,我通过将if语句从原来的内容更改为以下内容,解决了这个问题:
if (wScroll >= scrollStart) {
$('.kujoimgpos').css({
'background-position' : '0 -'+(60 + ((wScroll - scrollStart) *.2)) +'px'
});
}
实际上,将scrollStart变量改为减去窗口高度,以便所有内容都匹配
var scrollStart = $('#imgstart').offset().top - $(window).height();
“胖箭头”(=>
)是一个。您的if
语句可以重写为以下内容:
if (function(scrollBottom) { return scrollStart }) {
// ...
}
。。。显然,它的计算结果总是true
,因为所有函数基本上都是对象
正如其他答案中所指出的,如果您想进行“大于或等于”的比较,您需要使用=
与=
而不是=>
var scrollStart = $('#imgstart').offset().top - $(window).height();
if (function(scrollBottom) { return scrollStart }) {
// ...
}