javascript - 通过屏幕旋转更改图像宽度和高度
问题描述
请告诉我如何使用 javascript 通过方向更改来更改图像的宽度和高度。不幸的是,我的代码不起作用,这里是:
<script>window.addEventListener("orientationchange", function screenrotation() {
var firstresposniverowphoto = document.getElementById("#firstresposniverowphoto");
var secondresposniverowphoto = document.getElementById("#secondresposniverowphoto");
if(screen.orientation=="portrait"){
firstresposniverowphoto.width = '100%';
secondresposniverowphoto.width = '100%';
}if(screen.orientation=="landscape"){
firstresposniverowphoto.width = '40vw';
secondresposniverowphoto.width = '40vw';
}
}, false);
</script>
解决方案
谢谢大家的答案!我知道我做了一件非常愚蠢的事情,要求用 JavaScript 解决我之前描述的问题。相反,我意识到使用 CSS 比使用 JavaScript 更好。这是我的解决方案:
@media screen and (orientation: landscape) {
.contentOne{
box-shadow: 0 1vw 3vw 0 rgba(0,0,0,0.2);
transition: 0.3s;
background-repeat: no-repeat;
background-size: cover;
height:40vh;
width:40vw;
}
.contentOne:hover{
box-shadow: 0 2vw 4vw 0 rgba(0,0,0,0.4);
}
}
@media screen and (orientation: portrait) {
.contentOne{
box-shadow: 0 1vw 3vw 0 rgba(0,0,0,0.2);
transition: 0.3s;
background-repeat: no-repeat;
background-size: cover;
width:100%;
height:40vh;
margin:auto;
}
.contentOne:hover{
box-shadow: 0 2vw 4vw 0 rgba(0,0,0,0.4);
}
}
推荐阅读
- tsql - 带有结果集的 SQL 中的存储过程
- javascript - 如何从静态输入文本创建电子邮件正文?
- python - Python - 编辑 CSV 文件中的特定单元格
- c# - 使用 C# 和 SqlKata 查询引擎的多个 where 子句
- c - 在低级别写入文件
- regex - 如何使用正则表达式删除这部分文本?
- python - 检查列表元素接近什么值的最佳方法是什么?
- asp.net - 为本地和 AWS 弹性 beanstalk 配置不同的 Web.config
- python - Pandas:计算过去 48 小时内每个帐户看到的设备数量
- github - GitHub 使用 fork 公开到私有