dsfdfd1211 2013-07-14 08:06
浏览 43
已采纳

加载图像时间不一致(jQuery / PHP / HTML)

I am using jQuery to create a simple slide show.

I have a slideshow in home.php file. I have a menubar. when I click home in the menu, I load home.php in the content div of main page. There is no problem neither with slideshow nor with menu. When combined, they work fine upon first load. However when loaded many times, the time interval is not perfect and works with unequal irratic load pattern. Here is my code in home.php

<script>
$(function(){
$('.fadein img:gt(0)').hide();
setInterval(function(){$('.fadein :first-child').fadeOut().next('img').fadeIn().end().appendTo('.fadein');}, 3000);
});
</script>

and in menu.php

<li><a href=# onclick="$('#content').load('home.php') ;return false">HOME</a></li>
<li><a href=# onclick="$('#content').load('buyitems.php') ;return false">BUY</a></li>
<li><a href=# onclick="$('#content').load('discuss.php') ;return false">DISCUSS</a></li>
<li><a href=# onclick="$('#content').load('locationnear.php') ;return false">NEAR YOU</a></li>

If you need details, let me know.

Problem They work perfect individually. When I click home on menubar several times, the fade in time is not constant. some images load faster, others slower.

  • 写回答

1条回答 默认 最新

  • douliang7068 2013-07-14 08:10
    关注

    The reason for your delayed animation execution is probably the jQuery animation queue.

    New animations like your fadeOut are queued and wait for previous animations on the same element to finish.

    To cancel the current queue you might use jQuery.stop() <kbd>stop - jQuery API</kbd>

    Description: Stop the currently-running animation on the matched elements.

    So in your case this might look like this:

    $('.fadein :first-child').stop(true, true).fadeOut()
    

    Or you disable queuing for this animation <kbd>fadeOut - jQuery API</kbd>

    A Boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately.

    $('.fadein :first-child').fadeOut({ queue : false })
    

    Edit

    For every time you click your code starts a new setInterval which will also add animations to the queue so maybe clearing the intervall will help:

    if (window.currenTimer) {
      clearInterval(window.currentTimer);
    }
    window.currentTimer = setInterval(function(){
      $('.fadein :first-child')
       .stop(true, true)
       .fadeOut()
       .next('img')
       .stop(true, true)
       .fadeIn()
       .end()
       .appendTo('.fadein');
    }, 3000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行