Firefox和IE处理图像对象的方式与Webkit有什么不同?
In what way does Firefox and IE treat the image-object diffrent from Webkit?
我正在编写一个脚本,根据背景图像是横向的还是纵向的,为图像添加一个类。
函数如下:
function loadImg (mythis, callback) {
var src = getUrl($(mythis).css('background-image'));
var t = new Image();
t.src = src;
console.log(t.height);
var height = t.height;
var width = t.width;
if(width > height){
$(mythis).parent().addClass('panorama');
$(mythis).parent().parent().removeClass('smallpost');
}
这在Webkit中很好,但在Firefox或IE中就不行了。问题是。height和。width在这些浏览器中总是为零。此解决方案的一个挑战是,需要在运行函数之前加载图像。因为图像是背景图像,不在DOM中,所以我最终只是在脚本运行之前添加了延迟。我知道这有点罪恶,但在这种情况下确实是一个可以接受的解决方案。但我不确定这是否与我的问题有关。
javascript:美元(函数(){
setTimeout(function(){
var elems = $('.post .crop'), count = elems.length;
$(elems).each(function(i){
//Rezise image
loadImg(this, function callback(){
//Run masonry afte last image
if (i = count-1){
$('#content').imagesLoaded(function(){
$('#content').masonry({
itemSelector : '.post',
gutterWidth: 15,
columnWidth: 320
});
});
};
});
})
},5000);
//getImgSize('http://25.media.tumblr.com/83c73059a904b46afba332f26e33c5bd/tumblr_mmvwy7XjKq1sqsf7io1_500h.jpg')
function getUrl(styling){
styling = styling.replace('url(','').replace(')','');
return (styling);
}
function loadImg (mythis, callback) {
var src = getUrl($(mythis).css('background-image'));
var t = new Image();
t.src = src;
console.log(t.height);
var height = t.height;
var width = t.width;
if(width > height){
$(mythis).parent().addClass('panorama');
$(mythis).parent().parent().removeClass('smallpost');
}
callback();
}
});
您看到的差异通常与图像的缓存状态有关:如果图像已经可用,则可以立即知道尺寸。
您需要处理load
事件:
var t = new Image();
t.onload = function(){
var height = t.height;
var width = t.width;
if(width > height){
$(mythis).parent().addClass('panorama');
$(mythis).parent().parent().removeClass('smallpost');
}
};
t.src = src;
编辑:你有另一个问题在你的getUrl
函数:它不删除引号,一些浏览器可能会添加到样式。
styling = styling.replace('url(','').replace(')','');
styling = styling.replace('url(','').replace(')','').replace(/"/g,'');
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 全局变量和全局对象的属性之间有什么区别吗
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 在ember/handlers中使用value和valueBinding有什么区别
- 什么'在webpack外部设置中,reactDom和reactDom不同
- 在JavaScript中拆分日期字符串的更好方法是什么
- 为什么$.brower被弃用?还有什么更好的替代方案
- 什么是&&在没有if的行中的变量之间
- 如何在构建node-webkit应用程序后获取外部资源
- 有什么技巧可以让vw调整大小以在webkit浏览器上的:before伪类上工作吗
- Webkit——动态创建的样式表——什么时候真正加载
- 我们应该用什么替代layerX/layerY,因为它们在webkit中已经被弃用了
- Firefox和IE处理图像对象的方式与Webkit有什么不同?
- mozilla和webkit的触摸事件有什么不同?
- webkit 的 '$$' 返回和 jQuery 的 '$' 返回有什么区别?
- 自定义光标代码在webkit中带有一个黑色方块.我错过什么了吗?