web前端|js教程
js,预加载,图片
web前端-js教程
本文实例汇总了js预加载图片方法。分享给大家供大家参考。具体分析如下:
jphp在线考试系统源码,vscode终端的切换,Ubuntu 远程 图形,tomcat不工作了,安卓开源sqlite数据库,用爬虫爬某个平台的评论然后分析,yum 更新 php,电器seo优化哪家好,地板网站代码,后台管理系统模板 htmllzw
1. 纯CSS:
米拓源码,vscode弹窗怎么设置,ubuntu 内核编译,tomcat 配置应用,宝塔面板安装sqlite,史上最全网页设计配色方案,php写入mysql数据库,服务器与虚拟主机的区别,bootstrap tab插件下载,微前端如何兼容不同框架,多网站爬虫,yaf php,教育seo,租户springboot,wordpress 标签 边栏,html5网站后台制作,html5 网页制作,ecstore模板,建站之星 后台,editplus主页面,网站模板管理系统,atm柜员机模拟程序lzw
#preload-01 {background: url(http://domain.tld/image-01.jpg) no-repeat -9999px -9999px;}#preload-02 {background: url(http://domain.tld/image-02.jpg) no-repeat -9999px -9999px;}#preload-03 {background: url(http://domain.tld/image-03.jpg) no-repeat -9999px -9999px;}
2. JS+CSS优化:
唧唧帝源码,vscode c 求和,ubuntu 宝塔,tomcat lib 路径,sqlite3管理,web插件源码下载,前端实现在线word框架,和爬虫有关的节日,php 解析js,seo网络推广专业,菠菜网站源代码,ajax网页聊天,织梦专题单页面模板下载lzw
// better image preloading @ /press//12/28/3-ways-preload-images-css-javascript-ajax/function preloader() { if (document.getElementById) { document.getElementById("preload-01").style.background = "url(http://domain.tld/image-01.jpg) no-repeat -9999px -9999px"; document.getElementById("preload-02").style.background = "url(http://domain.tld/image-02.jpg) no-repeat -9999px -9999px"; document.getElementById("preload-03").style.background = "url(http://domain.tld/image-03.jpg) no-repeat -9999px -9999px"; }}function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != function) { window.onload = func; } else { window.onload = function() {if (oldonload) { oldonload();}func(); } }}addLoadEvent(preloader);
3. JS代码1:
<!--var images = new Array()function preload() { for (i = 0; i <!]]>
4. JS代码2:
<!]]>
5. JS代码优化2:
function preloader() { if (document.images) { var img1 = new Image(); var img2 = new Image(); var img3 = new Image(); img1.src = "http://domain.tld/path/to/image-001.jpg"; img2.src = "http://domain.tld/path/to/image-002.jpg"; img3.src = "http://domain.tld/path/to/image-003.jpg"; }}function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != function) { window.onload = func; } else { window.onload = function() {if (oldonload) { oldonload();}func(); } }}addLoadEvent(preloader);
6. Ajax代码1:
window.onload = function() { setTimeout(function() { // XHR to request a JS and a CSS var xhr = new XMLHttpRequest(); xhr.open(GET, http://domain.tld/preload.js); xhr.send(\); xhr = new XMLHttpRequest(); xhr.open(GET, http://domain.tld/preload.css); xhr.send(\); // preload image new Image().src = "http://domain.tld/preload.jpg"; }, 1000);};
7. Ajax代码2:
window.onload = function() { setTimeout(function() { // reference tovar head = document.getElementsByTagName(head)[0]; // a new CSS var css = document.createElement(link); css.type = "text/css"; css.rel = "stylesheet"; css.href = "http://domain.tld/preload.css"; // a new JS var js = document.createElement("script"); js.type = "text/javascript"; js.src = "http://domain.tld/preload.js"; // preload JS and CSS head.appendChild(css); head.appendChild(js); // preload image new Image().src = "http://domain.tld/preload.jpg"; }, 1000);};
如果觉得《js预加载图片方法汇总【javascript】》对你有帮助,请点赞、收藏,并留下你的观点哦!