How to display image inside <img> tag always in full size

I have an img element with a dynamically changing image. I always want to show this image in its full size.

So, I think I need to dynamically change the width and height attributes in the img tag.

How can I do this in php or javascript?

+3
source share
5 answers

You just need to leave the width and height (and style) attributes. If you need to know this, you can grab it from computed CSS.

+11
source

? , , . .

+1

, alt, height width ( ), javascript . , , jQuery, , - .., .

$(document).ready(function() {

  var imageArray = new Array ();
  imageArray[0] = new Array ( "jungle.jpg",   "http://rmportal.net", "Jungle", "400", "300");
  imageArray[1] = new Array ( "mountain.jpg", "http://rmportal.net", "Mountain" , "350", "290");
  imageArray[2] = new Array ( "ocean.jpg",    "http://rmportal.net", "Ocean" , "442", "530");

  var max = imageArray.length;
  var num = Math.floor((Math.random() * max));

  var hstring = "<a href='" + imageArray[num][1] + "'><img src='" + imageArray[num][0] + "' alt='" + imageArray[num][2] +  "' height='" imageArray[num][3] +  "' width='" + imageArray[num][4] + "' /></a>"

  $('#my-image').html(hstring);

});
+1

width height HTML, PHP getimagesize GD.

+1

, , removeAttribute('width') 'height' .

If you don’t want the page to collapse during slow loading, preload the image and change src after img.complete , it’s true that immediately if it is cached or after the image.

0
source

Source: https://habr.com/ru/post/1736909/


All Articles