OpenX 中 javascript jQuery 脚本的问题
我在工作中使用 OpenX,我老板的要求之一是可扩展的横幅。为此(并对整个故事进行了可怕的简化)我制作了这个脚本。
function retro(){
var acs = jQuery('#trial_center').height() - 5;
jQuery('#trial_center').css('height', acs + 'px');
}
jQuery(document).ready(function(){
jQuery("#trial_center").mouseover(function(){
setTimeout("jQuery('#trial_center').css('height', '500px')", 1000);
})
jQuery("#trial_center").mouseleave(function(){
var c = 89;
while (c > 0) {
setTimeout("retro()", 1000);
c--;
}
})
});
我遇到的问题是在 mouseleave 事件中:最初的想法是多次循环(89 次),并且每次都减小横幅的高度,直到达到原始大小。为什么会这样?因为我的老板想要一个“效果”,而且这个效果必须与客户的闪光灯同步。 问题在于,脚本显然没有逐渐减小其大小,而是将所有操作都设置为 setTimeout 调用总和“之后”,并更新了页面。因此,结果与横幅从展开尺寸缩小一倍到原始尺寸完全相同。
我不知道这有什么问题,或者是否存在其他更智能的解决方案。
任何帮助将非常感激。
提前致谢
I'm using OpenX at work, and one of my boss requirements is a expandable banner. For that (and made a horrible simplification of the whole story) I made this script.
function retro(){
var acs = jQuery('#trial_center').height() - 5;
jQuery('#trial_center').css('height', acs + 'px');
}
jQuery(document).ready(function(){
jQuery("#trial_center").mouseover(function(){
setTimeout("jQuery('#trial_center').css('height', '500px')", 1000);
})
jQuery("#trial_center").mouseleave(function(){
var c = 89;
while (c > 0) {
setTimeout("retro()", 1000);
c--;
}
})
});
The problem I have is in the mouseleave event: the original idea was to made this loop several times (89 times), and each time, decrease the height of the banner until it get his original size. Why this? Because my boss want an "effect", and this effect must be in sync with the customer's flash.
The problem is that instead of decrease his size progressively, apparently the script made all the operations an "after" the sum of setTimeout calls, updated the page. So, the result is exactly as the banner shrinks one time from the expanded size to the original size.
I don't know what is wrong with this, or if exists other more intelligent solution.
Any help will be very appreciate.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的循环设置超时只是在循环运行后设置 89 个计时器一秒,并且循环将以毫秒为单位运行 - 因此它们将在大约一秒后全部触发。这听起来不像你想做的事。
有两种选择:
1. 使用
animate
jQuery 的
animate
函数 看起来它符合你的要求。您可以告诉 jQuery 以动画方式显示大小更改,并告诉它需要多长时间才能执行此操作:这将在 1,000 毫秒的过程中以动画方式将容器的高度从代码运行时的高度更改为 500px (一秒钟)。显然,您可以将持续时间更改为您喜欢的任何时间。
2. 手动设置定时器循环
如果出于某种原因你不想使用
animate
,你可以手动执行此操作(当然可以;jQuery 不能做任何你不能做的事情你自己,这只会让事情变得更容易)。设置计时器循环的方法如下:之所以有效,是因为
iteration
是对c
的闭包(除其他外)。如果“闭包”这个词不熟悉,请不要担心,闭包并不复杂。单独:您稍后使用
鼠标悬停
来设置 Trial_center 元素的高度;您可能想要mouseneter
而不是mouseover
。当鼠标移过它时,mouseover
会重复。跑题:
setTimeout
最好不要使用字符串;只需传递一个函数引用即可。例如,对于可以使用
您正在使用的其他地方,您
And 而不是使用
Your loop setting the timeout is just setting 89 timers for one second later than the loop runs, and the loop will run in milliseconds — so they'll all fire about a second later. That doesn't sound like what you want to do.
Two options for you:
1. Use
animate
jQuery's
animate
function seems like it does what you want. You can tell jQuery to animate the size change, and you tell it how long to take to do so:That will animate changing the height of the container from whatever it is at the point that code runs to 500px, across the course of 1,000 milliseconds (one second). Obviously you can change the duration to whatever you like.
2. Set up the timer loop manually
If for whatever reason you don't want to use
animate
, you can do this manually (of course you can; jQuery can't do anything you can't do yourself, it just makes things easier). Here's how to set up a timer loop:That works because
iteration
is a closure overc
(amongst other things). Don't worry about the term "closure" if it's unfamiliar, closures are not complicated.Separately: You're using
mouseover
to set the height of the trial_center element a second later; you probably wantedmouseneter
rather thanmouseover
.mouseover
repeats as the mouse moves across it.Off-topic:
It's best not to use strings with
setTimeout
; just pass it a function reference instead. For example, instead ofyou'd use
And for the other place you're using, instead of
you'd use