Refresh image size of child div to never exceed parent div

I want to determine the size of the image when it is placed in the main image, and if it is above a certain height, make the image a certain height. I have problems with this because now it seems that it only detects the first image and currently does not respond when I select another image by clicking on the thumbnail.

All images placed in the main image area should be centered.

javascript is not working, but I think I hope in the right direction.

$('.thumb-box').click(function() {

  $(this).parent('.main-image').attr('src', '.thumb-box > img').fadeIn();

});

//Resize image
  $('.main-image').each(function() {
  if ($(this).height() > 550) { $(this).addClass('higher-than-max'); }
  else if ($(this).height() <= 550) {$(this).addClass('not-higher-than-max');}
});
.parent{
  border:1px solid purple;
  height:100%;
  width:80%;
  float:Right;
}
.child{
  border:1px solid red;
  height:100%;
  background:gray;
  text-align:center;
}
.child-img{
display:inline-block;
max-width:100%;
margin:0 auto;
}
.image-wrapper{
  width:100%;
  background:orange;
}
.thumbnails img{
width:auto;
height:100%;
width:100%;
}
.thumbnails{
  width:100px;
  height:100px;
  
}
.thumb-box{
  height:40%;
  width:40%;
  display:inline-block;
  background:red;
}
.higher-than-max{
  max-height:500px;
  width:auto;
}
.not-higher-than-max{
  max-height:100%;
  width:auto;
}
<div class="parent">
  <div class="child">
  <div class="image-wrapper">
    <img src="http://vignette2.wikia.nocookie.net/pokemon/images/b/b1/025Pikachu_XY_anime_3.png/revision/latest?cb=20140902050035" alt="374x333" class="main-image">
        <div class="thumbnails">
          <div class="thumb-box"> 
          <img src="http://vignette2.wikia.nocookie.net/pokemon/images/b/b1/025Pikachu_XY_anime_3.png/revision/latest?cb=20140902050035" alt="374x333" class="child-img">        </div>
          <div class="thumb-box"> 
          <img src="https://i.kinja-img.com/gawker-media/image/upload/unnbgkdbmsszmazgxkmr.jpg" alt="800x450" class="child-img"></div>
          <div class="thumb-box">  
          <img src="http://vignette3.wikia.nocookie.net/scratchpad/images/0/02/Pikachu.gif/revision/latest?cb=20150217015901" alt="" class="child-img">
          </div>
          <div class="thumb-box">
                <img src="http://cdn.bulbagarden.net/upload/thumb/0/0d/025Pikachu.png/250px-025Pikachu.png" alt="" class="child-img">
          </div>
        </div>
        

  </div>
</div>
Run codeHide result
+4
source share
1

.click():

  • .main-image thumb-box, parents() (1) parent() , parents('.image-wrapper').find('.main-image').
  • .attr('src', '.thumb-box > img') ".thumb-box > img" src , .thumb-box src.

JS Fiddle 1

$('.thumb-box').click(function() {

  // grab the src of the image nested in the just clicked tumb-box
  var theSRC = $(this).find('img').attr('src');
  // using parents() we grab the .image-wrapper, then the main-image nested inside it
  // and inject the stored src value of the thumb-box image into the main-image source
  $(this).parents('.image-wrapper').find('.main-image').attr('src', theSRC).fadeIn();

});

" 550 " CSS javascript, (2) CSS ( 3):

JS Fiddle 2

.main-image {
  max-height: 550px;
  width: auto;
}

----------------------------------------------- -------------------------------

(1)https://api.jquery.com/parents/

(2) //Resize image $('.main-image').each(...) .higher-than-max .not-higher-than-max CSS, - .

(3) max-height: 550px; 250px 300px

+3

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


All Articles