gpt4 book ai didi

javascript - 如何在不丢失宽高比的情况下使图像填充 90% 的屏幕

转载 作者:行者123 更新时间:2023-11-30 15:44:42 26 4
gpt4 key购买 nike

请在此处查看代码片段 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>

最佳答案

看看这个 fiddle 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com