링크 --> http://programmingsummaries.tistory.com/325
'자바스크립트' 카테고리의 다른 글
NaturalWidth and NaturalHeight in IE(익스플로러 8이하에서 이미지 원래값 얻기) (0) | 2017.01.25 |
---|
링크 --> http://programmingsummaries.tistory.com/325
NaturalWidth and NaturalHeight in IE(익스플로러 8이하에서 이미지 원래값 얻기) (0) | 2017.01.25 |
---|
Modern browsers (including IE9) provide naturalWidth and naturalHeight properties to IMG elements. These properties contain the actual, non-modified width and height of the image.
var nWidth = document.getElementById('example').naturalWidth;
var nHeight = document.getElementById('example').naturalHeight;
The naturalWidth and naturalHeight properties are not supported in IE8 or lower. The height and width property will give the apparent width and height after stylesheet and inline styles, inline width and height attributes, and the display property of the image and the image's parent elements have been applied. The simplest way to get the unmodified width and height is to create a new image element, and use its width and height property.
function getNatural (DOMelement) {
var img = new Image();
img.src = DOMelement.src;
return {width: img.width, height: img.height};
}
var natural = getNatural(document.getElementById('example')),
nWidth = natural.width,
nHeight = natural.height;
Here is a short jQuery (any version) plugin that adds two methods: naturalWidth() and naturalHeight(). It uses branching to determine if naturalWidth and naturalHeight are supported by the browser. If supported, the method just becomes a getter for the naturalWidth or naturalHeight property. If not supported, the method creates a new unstyled image element and returns that element's actual width and height.
// adds .naturalWidth() and .naturalHeight() methods to jQuery
// for retreaving a normalized naturalWidth and naturalHeight.
(function($){
var
props = ['Width', 'Height'],
prop;
while (prop = props.pop()) {
(function (natural, prop) {
$.fn[natural] = (natural in new Image()) ?
function () {
return this[0][natural];
} :
function () {
var
node = this[0],
img,
value;
if (node.tagName.toLowerCase() === 'img') {
img = new Image();
img.src = node.src,
value = img[prop];
}
return value;
};
}('natural' + prop, prop.toLowerCase()));
}
}(jQuery));
// Example usage:
var nWidth = $('img#example').naturalWidth();
var nHeight = $('img#example').naturalHeight();
Follow me on Twitter and Github, that's where I'm most active these days. I welcome email (), but I'm afraid I no longer have time to answer personal requests for help regarding my plugins or posts. Thanks!
출저 : http://www.jacklmoore.com/notes/naturalwidth-and-naturalheight-in-ie/, Jack Moore
promise 강의 (0) | 2017.05.10 |
---|