更新子 div 的图像大小,使其永远不超过父 div



我想在将图像放置在主图像中时检测图像大小,如果它高于某个高度,则使图像达到一定高度。 我对此有问题,因为现在,它似乎只检测到第一张图像,并且当我通过单击缩略图选择另一张图像时,目前没有响应

放在主图像区域的所有图像都应居中。

javascript不起作用,但我认为我希望我正朝着正确的方向前进。

$('.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>

您的

.click()函数中有几个问题:

  1. .main-image不是thumb-box的父级,因此您需要使用parents()(1(而不是parent()来选择更高级别的父级,然后导航到大图像 - 它将parents('.image-wrapper').find('.main-image')
  2. .attr('src', '.thumb-box > img')会将字符串".thumb-box> img"注入到大图像的src属性中,而不是将.thumb-box中的图像源注入到大图像src属性中。

将其更改为此

JS 小提琴 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();
});

对于第二个问题">最大高度为 550px",您可以简单地在 CSS 中执行此操作,而无需在 javascript 中调整大小代码,因此删除不必要的代码 (2(,只需将其添加到您的 CSS (3( 中:

JS 小提琴 2

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

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

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

(2(除了从CSS中删除//Resize image $('.main-image').each(...) .higher-than-max.not-higher-than-max类之外,您不需要它们中的任何一个。

(3(在小提琴中看得更清楚 max-height: 550px; 250px300px

最新更新