In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要为大家展示了"ajax如何异步加载图片",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"ajax如何异步加载图片"这篇文章吧。
具体如下:
图片一般比较大,所以他们都是在基本网页加载后才逐渐加载上的,整个加载的过程非常不雅观,或者是从模糊逐渐变清晰,或者是从上往下拓展开(当然你也可以认为这些都是不错的特效)。如果是通过定时更换img的src属性来实现图片的动态更换,由此带来的闪烁更让它难以接受,这可不是用alt属性就能让人愉快的。
联系时下比较热门的,号称"无"刷新的AJAX技术,利用XMLHttpRequest对象发起异步请求,待图像加载完毕再动态插入到"前台"的HTML页面上。应该可以满足需求,不过XMLHttpRequest对象返回的对象只有两个属性responseXML和responseText,前者是XML对象,后者是返回的纯文本内容,似乎没有图片所需要的二进制数据...退一步,即使能用responseText取回图片的二进制数据,我们又如何能够将它插入到前台页面呢?将img的src属性换成请求的url?
说干就干,写个图像幻灯片的代码来验证自己的想法:
Image Slide function makeAsyncRequest(url, callback){ var httpRequest; if (window.XMLHttpRequest) { // Mozilla, Safari, ... httpRequest = new XMLHttpRequest(); if (httpRequest.overrideMimeType) { httpRequest.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } httpRequest.onreadystatechange = function () { if (httpRequest.readyState == 4 && httpRequest.status == 200) callback(url); }; httpRequest.open('GET', url, true); httpRequest.send('');}var i = 0;var max_i = 10;function displayImage(){ var url = "./" + i + ".jpg"; makeAsyncRequest(url, function (url) { var div = document.getElementById("image"); var img = div.getElementsByTagName("img"); if (img.length == 0) { img = document.createElement("img"); while (div.childNodes.length > 0) div.removeChild(div.childNodes[0]); div.appendChild(img); } else img = img.item(0); img.src = url; if (i == max_i) i = 0; else i ++; window.setTimeout("displayImage();", 1000); });}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.