【发布时间】:2020-07-15 00:18:07
【问题描述】:
我想根据“宽度”和“高度”参数显示图像。 如何计算图像的比例? 例如,30/45,我需要根据用户的选择来改变宽度和高度。它有什么公式吗?
我的代码
.wall {
background-image: url(living-room-2.jpg);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
height: 100%;
display: flex;
justify-content: end;
/*transform: rotateY(40deg);*/
}
div.photo {
position: relative;
-webkit-transform: perspective(1000px) rotateY(30deg);
transform: perspective(1000px) rotateY(00deg);
margin: 50px 10px;
background-size: cover;
background-image:url('http://placekitten.com/g/600/200');
width: 500px;
height: 300px;
background-origin: border-box;
border-left: 5px solid rgba(0, 0, 0, 0.5);
}
div.photo:before,
div.photo:after {
content: "";
position: absolute;
top: 0;
width: 0;
height: 100%;
box-sizing: border-box;
background: transparent;
/*border-top: 4px solid white;*/
/*border-bottom: 4px solid white;*/
/*border-left: 10px solid white;*/
/*border-right: 6px solid rgba(0, 0, 0, 0.5);*/
}
@media only screen and (max-width: 600px) {
div.photo {
transform: perspective(1000px) rotateY(10deg);
height: 150px;
width: 150px;
}
}
我想通过用户选择来计算宽度和高度。
【问题讨论】:
-
height = width * ratio,例如比率 === 50/100 === 0.5,因此 900 像素宽的图像将是 450 像素高:900 * 0.5 = 450。 -
一种方法是使用javascript获取select元素的值,然后根据
<select>的当前值做一些数学运算,找出宽度和高度是多少 -
@rayhatfield 用户选择的尺寸以厘米为单位。所以我希望能够根据他选择的内容找到图像的宽度和高度。例如 30/45,新的图像宽度和高度是多少。我认为 450px 不适合手机屏幕。
-
所以让宽度适合可用空间(
100vw或100%或其他),并让高度width * ratio。也许我不明白你在问什么。
标签: jquery html css css-transforms css-shapes