当我调整窗口的大小时,如何更改px中固定宽度的元素?我需要使用一个绝对单位,我不能使用相对单位,如%或vw.Every时间窗口被调整为1px,我需要将元素宽度减少0.2px。
我尝试使用window调整大小的eventListener,但我不知道需要做什么计算。
发布于 2021-07-02 12:27:44
您想要的可以通过使用javascript来实现。我创造了一个逻辑来做到这一点:
<script>
function myFunction() {
var initialscreenwidth = window.innerWidth; //Set Initial Screen Width
setInterval(function() { //looping the script
var screenwidth = window.innerWidth;
var difference = initialscreenwidth - screenwidth; //Calculating the change in screen-size
if (difference != 0) { //Checking if there is a change in width
var element = document.getElementById('demo');
var style = window.getComputedStyle(element, null).getPropertyValue('font-size'); //Getting default font-size of the element
var initialfontsize = parseFloat(style);
var fontdifference = -(parseFloat(difference) / 5); //1px change in screen-size = 0.2px change in font-size
var newfontsize = initialfontsize + fontdifference;
var newfontsizepx = newfontsize + "px";
if (newfontsize > 1) {
document.getElementById("demo").style.fontSize = newfontsizepx;
}
}
initialscreenwidth = window.innerWidth;
}, 300); //reloads in every 300ms
}
myFunction();
</script>粘贴在你的身体部分的末尾,不知何故,在头部部分使用这是不工作的。
https://stackoverflow.com/questions/68224433
复制相似问题