请在此处查看代码段https://jsfiddle.net/nc7edzck/

我想在单击时显示图像,并且无论屏幕大小如何,它都应填满屏幕,而不会扭曲纵横比。

.imgclick{
    position: fixed;
    top: 50%;left: 50%;
    transform: translate(-50%,-50%);
    z-index: 100001;
    max-height: 80%;
}


我尝试了所有方法,但如果它在一个屏幕上可以工作,则会因另一屏幕尺寸不同的屏幕而失真。

编辑:发布前,请通过放大和缩小尝试您的解决方案。此外,我们可以将黑色条带不必要地扔掉。我可以稍后将其独立放置。



var ImageTrigger = false;
$(document).on('click', '.image', function() {
  if (!ImageTrigger) {
    var ImgSrc = $("img", this);
    $("#imgFrame img").attr("src", ImgSrc.attr("src"));
    $("#imgFrame img").attr("alt", ImgSrc.attr("alt"));
    $("#imgFrame img").attr("title", ImgSrc.attr("alt"));

    $("#imgFrame .imgclicktext").html(ImgSrc.attr("alt"));


    $("#imgFrame").css('visibility', 'visible');
    ImageTrigger = true;
    return false;
  }


});

$(document).on('click', 'body', function() {
  if (ImageTrigger) {

    $("#imgFrame").css('visibility', 'hidden');
    ImageTrigger = false;
  }
});

.imgclicktext {
  padding: 3px 15px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  text-align: center;
  margin-top: 15px;
}
img {
  /*transition: 0.3s ease-in-out;*/
  max-width: 90%;
  max-height: 400px;
  /*! padding: 10px; */
}
.imgclick {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 100001;
  max-height: 80%;
}
#imgFrame {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0px;
  left: 0px;
  z-index: 100000;
  background-color: rgba(0, 0, 0, 0.5);
  visibility: hidden;
  min-width: 500px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="imgFrame">
  <div class="imgclick">
    <img style="margin: 0 auto;display: table;" src="" alt="" title="" class="">
    <div class="imgclicktext" style=""></div>
  </div>
</div>
<div class="image" data-content="Not so sweet but ok!">
  <img style="margin: 0px auto;display: table;" src="https://upload.wikimedia.org/wikipedia/commons/f/f5/Howlsnow.jpg" alt="Not so sweet but ok!" title="Hottie">
</div>

最佳答案

看看这个小提琴
https://jsfiddle.net/nc7edzck/4/

我添加了一个窗口调整大小并更改了一些CSS:

    var height = $('.image').height()*.9;
    var width = $('.image').width()*.9;

    $('#imgFrame img').css('height',height);
    $('#imgFrame img').css('width',width);

    $("#imgFrame").css('visibility', 'visible');

    $(window).resize(function(){
        height = $('.image').height()*.9;
        width = $('.image').width()*.9;
        $('#imgFrame img').css('height',height);
        $('#imgFrame img').css('width',width);
    });

关于javascript - 如何在不损失宽高比的情况下使图像填充90%的屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40248248/

10-12 12:30
查看更多