OpenX 中 javascript jQuery 脚本的问题

发布于 2024-10-09 17:38:47 字数 814 浏览 3 评论 0原文

我在工作中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-10-16 17:38:47

您的循环设置超时只是在循环运行后设置 89 个计时器一秒,并且循环将以毫秒为单位运行 - 因此它们将在大约一秒后全部触发。这听起来不像你想做的事。

有两种选择:

1. 使用 animate

jQuery 的 animate 函数 看起来它符合你的要求。您可以告诉 jQuery 以动画方式显示大小更改,并告诉它需要多长时间才能执行此操作:

jQuery('#trial_center').animate({
    height: "500px" // Or whatever the desired ending height is
}, 1000);

这将在 1,000 毫秒的过程中以动画方式将容器的高度从代码运行时的高度更改为 500px (一秒钟)。显然,您可以将持续时间更改为您喜欢的任何时间。

2. 手动设置定时器循环

如果出于某种原因你不想使用animate,你可以手动执行此操作(当然可以;jQuery 不能做任何你不能做的事情你自己,这只会让事情变得更容易)。设置计时器循环的方法如下:

jQuery("#trial_center").mouseleave(function(){    
  var c = 89;

  // Do the first one right now, which will schedule the next
  iteration();

  // Our function here lives on until all the iterations are
  // complete
  function iteration() {
    // Do one
    retro();

    // Schedule this next unless we're done
    if (--c > 0 {
      setTimeout(iteration, 100); // 100ms = 1/10th second
    }
  }
});

之所以有效,是因为 iteration 是对 c 的闭包(除其他外)。如果“闭包”这个词不熟悉,请不要担心,闭包并不复杂


单独:您稍后使用鼠标悬停来设置 Trial_center 元素的高度;您可能想要 mouseneter 而不是 mouseover。当鼠标移过它时,mouseover 会重复。


跑题

setTimeout最好不要使用字符串;只需传递一个函数引用即可。例如,对于

setTimeout("retro()", 1000);

可以使用

setTimeout(retro, 1000); // No quotes, and no ()

您正在使用的其他地方,您

setTimeout("jQuery('#trial_center').css('height', '500px')", 1000);

And 而不是使用

setTimeout(function() {
    jQuery('#trial_center').css('height', '500px');
}, 1000);

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:

jQuery('#trial_center').animate({
    height: "500px" // Or whatever the desired ending height is
}, 1000);

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:

jQuery("#trial_center").mouseleave(function(){    
  var c = 89;

  // Do the first one right now, which will schedule the next
  iteration();

  // Our function here lives on until all the iterations are
  // complete
  function iteration() {
    // Do one
    retro();

    // Schedule this next unless we're done
    if (--c > 0 {
      setTimeout(iteration, 100); // 100ms = 1/10th second
    }
  }
});

That works because iteration is a closure over c (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 wanted mouseneter rather than mouseover. 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 of

setTimeout("retro()", 1000);

you'd use

setTimeout(retro, 1000); // No quotes, and no ()

And for the other place you're using, instead of

setTimeout("jQuery('#trial_center').css('height', '500px')", 1000);

you'd use

setTimeout(function() {
    jQuery('#trial_center').css('height', '500px');
}, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文