Find out what the width of an image is using jQuery

前端 未结 2 803
难免孤独
难免孤独 2021-01-25 02:03

I have these images that I am getting off my server and a database using PHP

相关标签:
2条回答
  • 2021-01-25 02:19

    Try using .width function. something like $('img').width();

    More reading.. http://api.jquery.com/width/

    Further more, there are .innerWidth and .outerWidth functions to include/exclude padding,border and margins. See below image for details,

    enter image description here

    0 讨论(0)
  • 2021-01-25 02:20

    I would try .innerWidth() instead of .width(), as .width() would include the margin.

    $('img').innerWidth();
    

    If your image is really that big, you should consider scaling it down it server-side.

    Update

    Actually .width() is not adding the margin and furthermore its not even adding the padding. That makes .width() more suitable than .innerWidth(). Thanks SKS.

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