如果位置小于 X,则执行某些操作。Jquery
$("#rightControl").click(function(){
$("#thumb_menu").animate({"left": "-=520px"}, "slow");
var pos = $('#thumb_menu').position();
if(pos.left < 0) {
$('#header')
.prepend('<span class="control" id="leftControl">Move left</span>')
}
});
单击#rightControl
一次将#thumb_menu
移动到左侧-450px,这样if
应该运行,但我似乎无法让它工作。
我哪里错了?
$("#rightControl").click(function(){
$("#thumb_menu").animate({"left": "-=520px"}, "slow");
var pos = $('#thumb_menu').position();
if(pos.left < 0) {
$('#header')
.prepend('<span class="control" id="leftControl">Move left</span>')
}
});
Clicking #rightControl
once moves #thumb_menu
to -450px left so the if
should run but I cant seem to get this working.
Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其放置在
.animate()
的回调中,以便它有机会在调用.position()
方法之前更改其位置。尽管如果您知道它正在转向负数位置,我认为您根本不需要
if()
语句。只需在回调中执行.prepend()
即可。Place it in a callback to the
.animate()
so that it has a chance to change its position before the.position()
method is invoked..Although if you know it is moving to a negative position, I wouldn't think you'd need the
if()
statement at all. Just do the.prepend()
in the callback.位置方法是相对于文档的,而不是元素的原始位置:
http://api.jquery.com/position/
所以
.position() 只会是
< 0
如果元素实际上不在屏幕上。您应该做的是在移动元素之前捕获元素的原始位置,然后查看它是否小于该位置。The position method is relative to the document, not the element's original position:
http://api.jquery.com/position/
So
.position()
will only be< 0
if the element is actually off the screen. What you should do is capture the original position of the element before you move it and then see if it's less than that.