简体   繁体   English

jQuery使用Ajax淡入和淡出图像

[英]jQuery Fade in and Fade out image with Ajax

I have a simple job I am trying to do. 我正在尝试做一份简单的工作。 I have a series of thumbnails and a main picture. 我有一系列缩略图和主图片。

http://activate5.info/index.php#portfolio http://activate5.info/index.php#portfolio

Upon clicking a thumbnail, I need to do an ajax call to look up an array, which returns a new image and text info. 单击缩略图后,我需要执行ajax调用来查找数组,该数组将返回新的图像和文本信息。 I then need to fade the main picture out, and replace it with the new information which comes from the ajax call. 然后,我需要淡出主画面,并用来自ajax调用的新信息替换它。

It is working, but its jerky. 它正在工作,但是生涩。 What is happening (I think) is the fadeout is smooth, but the fade IN jerks because the image is new content to the page, and it displays in a jerky way, like when pictures normally load on a page. 发生的事情(我认为)是淡入淡出,但是淡入淡出是抖动的,因为图像是页面的新内容,并且以不稳定的方式显示,就像通常在页面上加载图片时一样。

I'd like it smooth - so ideally, I need some kind of alert / trigger for when the image has totally loaded, before I fade the image in. I'm sure it's a simple answer - but would appreciate help. 我希望它平滑-因此,理想情况下,在图像完全淡入之前,我需要某种警报/触发器,然后再淡入图像。我敢肯定这是一个简单的答案-但我们将不胜感激。

I've put the fadeout / fadein stuff in the 'success' portion of the ajax call... 我已经将淡出/淡入内容放入了ajax调用的“成功”部分中...

$.ajax({
 url: 'resources/portfolio/ajax_portfolio_data.php',
 data: { client: clientCode,
          slide: slideNumber
       },
 success: function(data) {
      $('#slide'+slideNumber+'_portfolio').fadeOut('slow',function(){
         $('#slide'+slideNumber+'_portfolio').html($.parseJSON(data));
         $('#slide'+slideNumber+'_portfolio').fadeIn('slow');
      }); 
  }
});

I really don't want to preload ALL available images, as that would slow an already large page down more - anyone have a better way to do this? 我真的不想预加载所有可用的图像,因为那样会使原本已经很大的页面浏览速度进一步降低-任何人都有更好的方法来执行此操作?

Example of what is returned by the PHP file for the (data) is: PHP文件返回的(数据)示例如下:

$html = "<div class='slideMainImage'><img src='" 
   . $portfolio[$slide][$client]['image'] . "' /></div>";
$html .= "<p>" . $portfolio[$slide][$client]['text'] . "</p>";
echo json_encode($html);

Thanks in advance. 提前致谢。

You could add the image on ajax success , hide it and attach an onload event handler . 您可以在ajax success添加图像,将其隐藏并附加onload事件处理程序 When it's loaded, you execute the code that currently executes on success . 加载后,您将执行success当前执行的代码。

Note that the effect may be more smooth that way, but the overall process is slower because the effect only starts when the image is fully loaded. 请注意,这种效果可能会更平滑,但是整个过程会变慢,因为效果仅在图像完全加载后才开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM