gpt4 book ai didi

CSS变换比例截断图像

转载 作者:行者123 更新时间:2023-11-28 01:19:56 25 4
gpt4 key购买 nike

我在容器内有一个图像,它有溢出滚动,我要实现缩放功能,所以为了放大我想放大图像。在下面给出的代码片段中,有两张图片,第一张的缩放值为 1,您可以通过垂直和水平滚动来查看整个图像。但是当它的scale 值为2 时,我无法通过沿x 或y 方向滚动来查看整个图像。似乎放大图像会截断图像。克服这个问题的解决方案是什么。

div {
height: 200px;
width: 500px;
overflow: scroll;
}

img {
transform: scale(1)
}

.div2 {
margin-top: 5px;
}

.div2 img {
transform: scale(2)
}
<div>
<img src="https://www.tennisworldusa.org/imgb/57551/roger-federer-to-take-a-threemonth-break-i-won-t-play-roland-garros-.jpg" />
</div>

<div class="div2">
<img src="https://www.tennisworldusa.org/imgb/57551/roger-federer-to-take-a-threemonth-break-i-won-t-play-roland-garros-.jpg" />
</div>

最佳答案

Transform: scale(2) 无法为 .div2 定义 img 的宽度/高度值

但是您可以使用jquery 进行缩放。

示例:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Test</title>
<style>
div {
height: 200px;
width: 500px;
overflow: scroll;
}

img {
transform: scale(1)
}

.div2 {
margin-top: 5px;
}
</style>
</head>

<body>

<div>
<img src="https://www.tennisworldusa.org/imgb/57551/roger-federer-to-take-a-threemonth-break-i-won-t-play-roland-garros-.jpg" />
</div>

<div class="div2">
<img src="https://www.tennisworldusa.org/imgb/57551/roger-federer-to-take-a-threemonth-break-i-won-t-play-roland-garros-.jpg" />
</div>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var originImgWidth = $('.div2 > img').width();
var originImgHeight = $('.div2 > img').height();
var ratio = 2;

//Check img orgin size
//console.log('originSize:', originImgWidth, originImgHeight);

$('.div2 > img').css({
'width': originImgWidth * ratio + 'px',
'height': originImgHeight * ratio + 'px'
})
</script>
</body>

</html>

关于CSS变换比例截断图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51559729/

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