我想基于“宽度”和“高度”参数显示图像。如何计算图像的比例?例如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;
}
}
而且我想通过用户选择来计算宽度和高度。
不确定我是否理解您的要求,但是您可以根据宽度计算高度。看看这一点,看看在select中更改值时会发生什么:
const onChange = e => {
const preview = document.querySelector('.preview')
const previewHeight = `${parseFloat(e.target.value) * 100}%`;
preview.style = `padding-bottom: ${previewHeight};`;
}
document.querySelector('select').addEventListener('change', onChange)
.preview {
width: 100%;
height: 0;
position: relative;
padding-bottom: 66.6666%;
background: url('http://placekitten.com/g/1200/400') center no-repeat;
background-size: cover;
margin-top: 1rem;
}
<div class="demo">
<select>
<option value="0.666">30/45</option>
<option value="0.5">50/100</option>
<option value="0.5625">9/16</option>
<option value="0.1">1/10</option>
<option value="2">2/1</option>
</select>
<div class="preview"></div>
</div>