我知道这是一个热门话题,我知道以前的问题都有相同的标题,但我尝试了所有的东西而且有些东西不合适。出于某种原因,我的Firefox不会预装图像。图像DO在IE7 / 8和Chrome中预加载(应该如此)。但不是在Firefox中。
编辑:
我创造了一个新的小提琴:http://jsfiddle.net/Z2W7r/ 如果有人可以修改它并添加正确的jQuery或Javascript代码来使图像预加载,我将非常感激。
我甚至使用以下插件:
jQuery.preloadCssImages = function(){
var allImgs = [];//new array for all the image urls
var k = 0; //iterator for adding images
var sheets = document.styleSheets;//array of stylesheets
for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
var cssPile = '';//create large string of all css rules in sheet
var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
var baseURLarr = csshref.split('/');//split href at / to make array
baseURLarr.pop();//remove file path from baseURL array
var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
if(baseURL!="") baseURL+='/'; //tack on a / if needed
if(document.styleSheets[i].cssRules){//w3
var thisSheetRules = document.styleSheets[i].cssRules; //w3
for(var j = 0; j<thisSheetRules.length; j++){
cssPile+= thisSheetRules[j].cssText;
}
}
else {
cssPile+= document.styleSheets[i].cssText;
}
//parse cssPile for image urls and load them into the DOM
var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
var arr = jQuery.makeArray(imgUrls);//create array from regex obj
jQuery(arr).each(function(){
allImgs[k] = new Image(); //new img obj
allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this; //set src either absolute or rel to css dir
k++;
});
}
}//loop
return allImgs;
}
并且这样称呼它:
$(document).ready(function() {
$.preloadCssImages();
});
所以......有没有人知道为什么这个脚本(或者那个问题的任何脚本)不能仅在Firefox中运行?如果需要,我可以提供网站的地址。
答案 0 :(得分:2)
很老但是威胁,但问题仍然存在!! 我已经尝试过不同的脚本来存档(包括这个)并调试它何时以及为什么会发生。
我发现只有当这三个条件出现时脚本才会失败 锅:
Firefox browser
上运行 Linux OS
,当您进入CSS文件时, cross domain URL
用于图像。
原因,HTTP_access_control ...但是必须要对Linux
做一些事情......因为即使你试着
<{1}}在代码中,它失败了。
我对此的实际解决方案(不优雅,但至少避免了问题)是根据以下条件加载脚本:
Access-Control-Allow-
答案 1 :(得分:1)
可能是因为您在本地使用它,FF在访问本地CSS文件时存在安全问题。尝试将项目上传到Web服务器或启动本地Apache,看看问题是否仍然存在。
答案 2 :(得分:0)
我猜人们说这些图片是使用问题中提到的预加载插件为他们预加载的。
如果有人想提供更多建议或更简单的预加载脚本,我会全力以赴。否则,我会把这个答案作为正确答案......
只是为了正确,这是我正在谈论的插件:
jQuery.preloadCssImages = function(){
var allImgs = [];//new array for all the image urls
var k = 0; //iterator for adding images
var sheets = document.styleSheets;//array of stylesheets
for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
var cssPile = '';//create large string of all css rules in sheet
var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
var baseURLarr = csshref.split('/');//split href at / to make array
baseURLarr.pop();//remove file path from baseURL array
var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
if(baseURL!="") baseURL+='/'; //tack on a / if needed
if(document.styleSheets[i].cssRules){//w3
var thisSheetRules = document.styleSheets[i].cssRules; //w3
for(var j = 0; j<thisSheetRules.length; j++){
cssPile+= thisSheetRules[j].cssText;
}
}
else {
cssPile+= document.styleSheets[i].cssText;
}
//parse cssPile for image urls and load them into the DOM
var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
var arr = jQuery.makeArray(imgUrls);//create array from regex obj
jQuery(arr).each(function(){
allImgs[k] = new Image(); //new img obj
allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this; //set src either absolute or rel to css dir
k++;
});
}
}//loop
return allImgs;
}
并且这样称呼它:
$(document).ready(function() {
$.preloadCssImages();
});
谢谢! 阿米特