English 中文(简体)
Changing <img src="XXX”/>, js event when new pornography has concludingloading?
原标题:Changing <img src="XXX" />, js event when new image has finished loading?

我有一幅光彩的网页:<img src='XXX”/>要素ssrc 改为(点击),以显示下一个形象,即贫穷男子 s子一gues。 当新形象几乎马上出现时,就能够更快地取得联系。 即便是用几秒钟装载,每台浏览器一盘检测器在新图像完全装满之前,仍保持旧形象。

但我很想知道,在新形象被装满时是否发生了大火,使我能够用一点working...... an夫或同时出现的事情。

我知道,我可以把AJAX用于实际工作(如果已经使用 j子的话),但这是一个非常简单的解决办法。 除了这一滞后之外,我是否应该放弃改变形象的做法?

最佳回答

你可以就“载荷”活动设立一个手稿。

$( img.whatever )
  .load(function() { /* do stuff */ })
  .attr( src , newURL);

http://www.un.org

$( img.reloadable ).live( load , function() { $(this).show(); });
// ...
$( img#someId ).hide().attr( src , newURL);

edit — whoa, where did that year go? Well, it turns out that one problem with that "live" approach I typed in way back when is that the "load" event does not bubble. Now what you can do, however, is leverage the way that "Image" objects (as opposed to <img> DOM elements) behave. Basically, the function that changes the URL can use an "Image" element as the place to keep the handler. The code that changes the actual "src" attribute of the real <img> tag would then also update the "src" of the "Image" object instance. The browser will only really load the image once (assuming cache control is all cool), but the browser will still call the "onload" handler of the "Image":

(function() {
  var imageObj = new Image();

  imageObj.onload = function() {
    // code to run when image loads from server 
  };

  $( #hypotheticalButton ).click(function() {
    $( #imgToUpdate ).attr( src , newURL);
    imageObj.src = newURL;
  });
})();
问题回答

你只是把图像带上 j,这样,当用户点击时,下一个形象就已经装上了,而且有误了......除非用户进入你的网页,在装上图像之前就开始点击。

http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript”rel=“nofollow noreferer”>http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript

var slideimg = $( #slideimage );
slideimg.click(function(){
    var img = new Image();
    var url =  url_to_next_image.jpg ;
    $(img).bind( load ,function(){
        $( #loading ).hide();
        slideimg.attr( src ,url);
    }).attr( src ,url);
    $( #loading ).show();
});

这甚至应当与IE的摇摇摇摇摇摇摇摇篮进行处理。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签