简体   繁体   English

调整窗口大小时以 px 为单位更改元素宽度

[英]Change an element width in px when resizing the window

How can I change an element with a fixed width in px when i resize the window?当我调整窗口大小时,如何以 px 为单位更改具有固定宽度的元素? I need to use an absolute unit, i can not use relative units like % or vw.Every time the window is resized with 1 px i need to decrease the element width by 0.2px.我需要使用绝对单位,我不能使用像 % 或 vw 这样的相对单位。每次将窗口大小调整为 1 像素时,我都需要将元素宽度减少 0.2 像素。

I tried to use the window resize eventListener but i don't know what calculations needs to be done.我尝试使用window调整大小事件侦听器,但我不知道需要进行哪些计算。

What you want can be achieved by using javascript.你想要的可以通过使用 javascript 来实现。 I've created a logic to do that :我已经创建了一个逻辑来做到这一点:

<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>

Paste this at the end of your body section, somehow using this in the head section is not working.将其粘贴到正文部分的末尾,不知何故在头部部分使用它不起作用。

设置宽度<div>调整 window 的大小时</div><div id="text_translate"><p>所以我在 Vuejs 中得到了这个应用程序,它被css grid划分为 3 个 div 基本上:一个sidebar 、一个configpanel和一个preview 。 我想根据当前屏幕大小和屏幕大小调整时以px为单位设置preview div 的宽度</p><p>为了澄清起见,我制作了一个网站的 gif 图像,它几乎完全符合我的要求: <a href="https://gyazo.com/d667756474e7f4fa18e2d5d64a0dee5a" rel="nofollow noreferrer">https://gyazo.com/d667756474e7f4fa18e2d5d64a0dee5a</a></p><p> 正如您在 gif 中看到的,每次调整屏幕大小时,特定的&lt;div&gt;都会由于某种原因(不知道为什么或如何)被分配一个新的 class 并且 div 的width会自动更新为新的px值。</p><p> 起初我以为我可以在created()钩子中做这样的事情:</p><pre> window.addEventListener("resize", () =&gt; { let preview = window.document.querySelector(".preview"); preview.style.width = `${preview.offsetWidth}px`; });</pre><p> 但这不起作用。</p><p> 我在<a href="https://codesandbox.io/s/hopeful-feather-ywh6z?file=/src/components/Preview.vue:125-430" rel="nofollow noreferrer">这里</a>的codesandbox中设置了一个非常简单的示例项目。</p><p> 理想情况下,我想知道如何像网站在 gif 中那样动态设置像素width 。</p><p> 此外,出于好奇,我想知道每次调整屏幕大小时该特定网站为何以及如何生成新的 class (我已经看过几次了,我想知道它是什么)。</p></div> - Setting the width of a <div> when resizing the window

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开 <details> 窗口宽度=&gt; 768px时的元素 - Open <details> element when window width => 768px 窗口宽度大于769px时使用jquery追加和删除元素 - Append and remove element with jquery when width of window is greater than 769px 设置宽度<div>调整 window 的大小时</div><div id="text_translate"><p>所以我在 Vuejs 中得到了这个应用程序,它被css grid划分为 3 个 div 基本上:一个sidebar 、一个configpanel和一个preview 。 我想根据当前屏幕大小和屏幕大小调整时以px为单位设置preview div 的宽度</p><p>为了澄清起见,我制作了一个网站的 gif 图像,它几乎完全符合我的要求: <a href="https://gyazo.com/d667756474e7f4fa18e2d5d64a0dee5a" rel="nofollow noreferrer">https://gyazo.com/d667756474e7f4fa18e2d5d64a0dee5a</a></p><p> 正如您在 gif 中看到的,每次调整屏幕大小时,特定的&lt;div&gt;都会由于某种原因(不知道为什么或如何)被分配一个新的 class 并且 div 的width会自动更新为新的px值。</p><p> 起初我以为我可以在created()钩子中做这样的事情:</p><pre> window.addEventListener("resize", () =&gt; { let preview = window.document.querySelector(".preview"); preview.style.width = `${preview.offsetWidth}px`; });</pre><p> 但这不起作用。</p><p> 我在<a href="https://codesandbox.io/s/hopeful-feather-ywh6z?file=/src/components/Preview.vue:125-430" rel="nofollow noreferrer">这里</a>的codesandbox中设置了一个非常简单的示例项目。</p><p> 理想情况下,我想知道如何像网站在 gif 中那样动态设置像素width 。</p><p> 此外,出于好奇,我想知道每次调整屏幕大小时该特定网站为何以及如何生成新的 class (我已经看过几次了,我想知道它是什么)。</p></div> - Setting the width of a <div> when resizing the window 调整窗口大小时更改元素的高度 - Change height of element while resizing window 将屏幕大小调整为 768px 时,导航更改样式 - When resizing screen to 768px the navigation change style 如何检测 JS 中 div 元素宽度何时发生变化(不是窗口调整大小)? - How to detect when the div element width change (not window resize) in JS? 获取px中的元素宽度 - Get element width in px jQuery:如何使用一个元素的宽度百分比作为值来动画化另一个元素,并确保即使在调整窗口大小时它也可以工作? - jQuery: How to use percentage width of one element as a value to animate another element, and make sure it works even when resizing the window? 当 window 宽度等于 600px 时如何禁用 OverlayScrollbars? - How to disable OverlayScrollbars when window width equal 600px? 当我的窗口宽度小于800px时,如何将js滑块效果更改为淡入淡出? - how do i change the js slider effect to fade when my window width is less than 800px?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM