jQuery If 语句和变量
基本上,我有一个顶部栏,当鼠标悬停在上面时会向下滚动。我添加了顶部蒙版层以使其变暗。我希望当 css top 值为 = -180 时,顶部蒙版可见。
目前的问题应该是变量(css top value)没有一直更新。
$('.topmask').hover(function () {
$('.topbar').stop().animate({"top":"0px"});
visible1 = $('.topbar').css('top');
});
$('.topbar').mouseleave(function () {
$(this).stop().animate({"top":"-180px"});
});
var visible1 = $('.topbar').css('top');;console.log(visible1);
var visible = parseFloat(visible1);
if (visible = -180) {
$('.topmask').show();
} else {
$('.topmask').hide();
}
这是我快速制作的 JSfiddle 的链接,以更好地演示该问题,因为我显然不擅长解释。 http://jsfiddle.net/Fregos/DyrK5/
Basically, I have a top bar that scrolls down when hovered on. I added a top mask layer to make it darker. I want the top mask to be visible when css top value is = -180.
The current problem should be that the variable (css top value) is not updating all the time.
$('.topmask').hover(function () {
$('.topbar').stop().animate({"top":"0px"});
visible1 = $('.topbar').css('top');
});
$('.topbar').mouseleave(function () {
$(this).stop().animate({"top":"-180px"});
});
var visible1 = $('.topbar').css('top');;console.log(visible1);
var visible = parseFloat(visible1);
if (visible = -180) {
$('.topmask').show();
} else {
$('.topmask').hide();
}
And here's a link to a JSfiddle I quickly did to demonstrate the problem better, as I am obviously bad at explaining. http://jsfiddle.net/Fregos/DyrK5/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
动画是异步执行的,因此当您计算
top
时,动画尚未完成。基于动画完成后触发的回调来显示和隐藏顶栏怎么样?工作示例位于 http://jsfiddle.net/DyrK5/1/。
Animations execute asynchronously, so when you're calculating the
top
, the animation hasn't yet completed. How about showing and hiding the topbar based on a callback fired after the animation is complete?Working example at http://jsfiddle.net/DyrK5/1/.
我在这里看到了很多问题。首先也是最重要的,您必须将操作设置为显示或隐藏蒙版作为动画函数的回调。以下是页面首次加载时发生的情况:
您真正想要的是:
因此,您需要在绑定事件或动画回调中有条件地隐藏或显示遮罩。有关设置 animate 函数回调的更多信息,请查看:http://api.jquery。 com/animate/
我看到的另一个明显的错误是您当前在条件语句中将可见值设置为 -180。
不等于:
There are quite a few problems I see here. First and foremost, you must set the action to either show or hide the mask as a callback of the animation function. Here's what happens when the page first loads for you:
What you really want is this:
So either you need to conditionally hide or show the mask within the bound event or within the animation callback. For more information on setting a callback to the animate function, take a look at this: http://api.jquery.com/animate/
The other noticeable glaring error I see is that you are currently setting the value of visible to -180 in your conditional statement.
is not the same as:
为什么不直接做:
Why not just do: