Joe*_*oey 9 jquery image preloading
我需要在给定特定来源的情况下获得图像的原始宽度和高度.我目前的方法是:
img.tag = "<img style='display:none;' src='" + img.src + "' />";
img.Owidth = 0;
img.Oheight = 0;
$(img.tag).load(function() {
img.Owidth = $(this).width();
img.Oheight = $(this).height();
}).appendTo(img.parent());
Run Code Online (Sandbox Code Playgroud)
使用Owidth并Oheight成为加载图像的原始尺寸.我想知道是否有更好的方法来做到这一点:
Rok*_*jan 13
$("<img/>").load(function(){
var width = this.width,
height = this.height;
alert( 'W='+ width +' H='+ height);
}).attr("src", "image.jpg");
Run Code Online (Sandbox Code Playgroud)
如果要调查所有HTMLImageElement 属性:https://developer.mozilla.org/en/docs/Web/API/HTMLImageElement
许多这些属性已经在现代的HTML5兼容浏览器中可用,并且可以使用jQuery的metod访问.prop()
var $img = $("#myImage");
console.log(
$img.prop("naturalWidth") +'\n'+ // Width (Natural)
$img.prop("naturalHeight") +'\n'+ // Height (Natural)
$img.prop("width") +'\n'+ // Width (Rendered)
$img.prop("height") +'\n'+ // Height (Rendered)
$img.prop("x") +'\n'+ // X offset
$img.prop("y") // Y offset ...
);
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 13
对于Chrome和Firefox(很快就会推荐IE),您可以使用......
var width = $('img').prop('naturalWidth');
var height = $('img').prop('naturalHeight');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18704 次 |
| 最近记录: |