Is there any way to read out the “naturalWidth” of an image with jquery?

前端 未结 8 2134
时光说笑
时光说笑 2020-12-05 10:59

I tried with something like this:

var Height = $(this).naturalHeight;

But it doesn\'t work. Is there any way to do that

greez

相关标签:
8条回答
  • 2020-12-05 11:16
    $this.css('height', 'auto');
    var height = $this.height();
    
    0 讨论(0)
  • 2020-12-05 11:26

    I use the native javascript properties after applying jQuery and then stripping jQuery

    .naturalWidth / .naturalHeight

    On a jQuery object i use

    var width = this.naturalWidth; 
    var height = this.naturalHeight;
    

    or

    var width = $("selector").get(0).naturalWidth; 
    var height = $("selector").get(0).naturalHeight;
    

    if no jQuery object is selected.

    With get(0) on a jQuery object you can access the associated DOM-element. On the native DOM element you can work with native javascript code (here access the nativ javascript attribute .naturalWidth)

    0 讨论(0)
提交回复
热议问题