【发布时间】:2022-01-03 18:25:02
【问题描述】:
我正在尝试设置一个基本功能,以平滑地将点击时的 img 从比屏幕长切换到显示(适合窗口大小)(来回)。它已经使用百分比等工作了。
我的问题是我希望在 2 个状态之间进行平滑的动画转换,但 img 被粗暴地缩放。 此外,每当我尝试使用“过渡”或“动画”时,当 img 恢复到其原始大小时,它会阻止滚动。我尝试使用关键帧后也出现了同样的问题。
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<style>
img {
margin: auto;
display: block;
padding: 2%;
width: 90vw;
box-sizing: border-box;
}
.scaled {
width: auto;
height: 100vh;
}
</style>
</head>
<body>
<img class="item" src="images/kitten1.png">
<img class="item" src="images/kitten2.png">
<img class="item" src="images/kitten3.png">
</body>
<script>
$(".item").click(function() {
$(this).toggleClass('scaled');
$(this).scrollIntoView();
});
</script>
</html>
我还想让窗口视图(我的意思是页面上滚动的位置)在缩放时以 img 为中心。我目前正在尝试为此目的使用 scrollIntoView,但似乎没有任何反应。
提前谢谢你。第一次在这里发帖。我不觉得这应该太难,但可能与我现在能想出的水平不同ଘ(੭ˊᵕˋ)੭ ̀ˋ
还尝试了以下方法,但 img 停留在 90vw 和 100vh ...
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<style>
img {
margin: auto;
display: block;
padding: 2%;
width: 90vw;
box-sizing: border-box;
object-fit: contain;
}
</style>
</head>
<body>
<img class="item" src="images/kitten1.png">
<img class="item" src="images/kitten2.png">
<img class="item" src="images/kitten3.png">
</body>
<script>
$(".item").click(function() {
if ($(this).hasClass('scaled')) {
$(this).animate({
height: "none",
width: "90vw"
}, 1000);
$(this).removeClass('scaled');
}
else {
$(this).animate({
width: "none",
height: "100vh"
}, 1000);
$(this).addClass('scaled');
}
});
</script></html>
【问题讨论】:
标签: javascript html css animation toggle