根据高度动态重新调整图像
Dynamically readjusting image depending on the height
我正在尝试重做 that example 作为练习。
我试图让图像 div 在我单击其上方或下方的文本时动态重新调整。
所以我画了一张草图试试。但是我被卡住了,因为我不知道如何计算剩余的 space 当 div expanded
打开时图像适合 space.
jQuery(document).ready(function($)
{
$("#ex").click(function()
{
$("#expanded").slideToggle(200)
if ($("#ex").text() == "infos")
{
$("#ex").html("close")
$("#fit-picture").css("height", "20px");
}
else
{
$("#ex").text("infos")
$("#fit-picture").css("height", "100vh");
}
});
});
body{margin:0}
.container{
height: 100%;
min-height: 100%;}
#expanded{
margin-top: 0px;
background: gray;
width: 50vw; height: 50vh;
}
#ex {
display: block;
width: 50vw;
background-color:darkgrey;
text-decoration:none;
}
#ex:hover {
background:black;
}
.container{
height:100vh;
}
#fit-picture{
width: auto;
height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="container">
<div class="expand">
<a href="#" id="ex">infos</a>
</div>
<div id="expanded" style="display: none;">the image is supposed to be reduced proportionally to fit the remaining space of the screen.
</div>
<img id="fit-picture"
src="https://www.wanimo.com/veterinaire/wp-content/uploads/2018/11/chat-jaloux-e1574672723199@2x.jpg">
</div>
在 HTML 中,将您的图片标签替换为以下内容:
<img id="fit-picture" src="https://www.wanimo.com/veterinaire/wp-content/uploads/2018/11/chat-jaloux-e1574672723199@2x.jpg" class="center">
在CSS中粘贴
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
您应该以 % 而不是 px 的形式提及高度和宽度。这将有助于每次调整图像大小以调整 window。这将是完成工作的最简单方法。
所以,它不是真正的自适应,你必须先定义所有高度,但这里有一个解决方案:
jQuery(document).ready(function($)
{
$("#ex").click(function()
{
$("#expanded").slideToggle(200)
if ($("#ex").text() == "infos")
{
$("#ex").html("close")
$("#fit-picture").animate({height:"45vh"}, 200) ;
}
else
{
$("#ex").text("infos")
$("#fit-picture").animate({height:"95vh"}, 200);
}
});
});
我正在尝试重做 that example 作为练习。
我试图让图像 div 在我单击其上方或下方的文本时动态重新调整。
所以我画了一张草图试试。但是我被卡住了,因为我不知道如何计算剩余的 space 当 div expanded
打开时图像适合 space.
jQuery(document).ready(function($)
{
$("#ex").click(function()
{
$("#expanded").slideToggle(200)
if ($("#ex").text() == "infos")
{
$("#ex").html("close")
$("#fit-picture").css("height", "20px");
}
else
{
$("#ex").text("infos")
$("#fit-picture").css("height", "100vh");
}
});
});
body{margin:0}
.container{
height: 100%;
min-height: 100%;}
#expanded{
margin-top: 0px;
background: gray;
width: 50vw; height: 50vh;
}
#ex {
display: block;
width: 50vw;
background-color:darkgrey;
text-decoration:none;
}
#ex:hover {
background:black;
}
.container{
height:100vh;
}
#fit-picture{
width: auto;
height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class ="container">
<div class="expand">
<a href="#" id="ex">infos</a>
</div>
<div id="expanded" style="display: none;">the image is supposed to be reduced proportionally to fit the remaining space of the screen.
</div>
<img id="fit-picture"
src="https://www.wanimo.com/veterinaire/wp-content/uploads/2018/11/chat-jaloux-e1574672723199@2x.jpg">
</div>
在 HTML 中,将您的图片标签替换为以下内容:
<img id="fit-picture" src="https://www.wanimo.com/veterinaire/wp-content/uploads/2018/11/chat-jaloux-e1574672723199@2x.jpg" class="center">
在CSS中粘贴
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
您应该以 % 而不是 px 的形式提及高度和宽度。这将有助于每次调整图像大小以调整 window。这将是完成工作的最简单方法。
所以,它不是真正的自适应,你必须先定义所有高度,但这里有一个解决方案:
jQuery(document).ready(function($)
{
$("#ex").click(function()
{
$("#expanded").slideToggle(200)
if ($("#ex").text() == "infos")
{
$("#ex").html("close")
$("#fit-picture").animate({height:"45vh"}, 200) ;
}
else
{
$("#ex").text("infos")
$("#fit-picture").animate({height:"95vh"}, 200);
}
});
});