简体   繁体   English

Css - 如果它到达屏幕上的某个位置,如何更改元素大小?

[英]Css - how to change element size if it reached to certain spot on screen?

I've dynamically created some divs with random color who are scattered over the page randomly. 我已经动态地创建了一些随机颜色的div,这些div随机分散在页面上。 I want to use css to give them a condition that says that if the div is located above let's say 600px on the screen - his size will change. 我想用css给他们一个条件,说如果div位于上面让我们说屏幕上600px - 他的大小会​​改变。 I know the "if" statement for css is @media but I didn't figure how to use it right in this situation. 我知道css的“if”语句是@media但我没有想到如何在这种情况下正确使用它。 Can you help me? 你能帮助我吗?

Example of a div (they all have the same class - "frog") div的例子(它们都有相同的类 - “青蛙”)

<div id="frog" class="fas fa-ad" style="width: 66px; height: 66px; 
background-color: rgb(87, 58, 55); position: absolute; left: 312px; top: 
93px; display: block;"></div>

You can't do that with CSS only. 你不能只用CSS做到这一点。 The only way you can have dynamic styling based on what happens in the windows in CSS is to use media queries. 根据CSS中窗口中发生的事情,您可以拥有动态样式的唯一方法是使用媒体查询。 However, the docs precise that you can only detect window-level data , like the device width, or whether the page is displayed on a screen or on a printed paper, etc. 但是, 文档确切地说您只能检测窗口级数据 ,例如设备宽度,或者页面是显示在屏幕上还是打印在纸上等。

You'll have to change your style with JS. 你必须用JS改变你的风格。 This is often a bad way to have dynamic styling, because the only way to do so is to 'probe' the DOM (using for example setInterval ). 这通常是动态样式的一种不好的方法,因为这样做的唯一方法是“探测”DOM(使用例如setInterval )。 Luckily your case is an exception - since you update your divs position with JS, you can check directly after that the position of your divs, and update your styles accordingly! 幸运的是你的情况是一个例外 - 因为你用JS更新你的div位置,你可以直接检查你的div的位置,并相应地更新你的样式!

Example using the very useful getBoundingClientRect : 使用非常有用的getBoundingClientRect的示例:

 // select the frog element let frog = document.getElementById('frog'); let count = 0; setInterval(() => { // update the frog position frog.style.top = `${count}px`; count = (count+2)%200; // check if the frog is after 100px from the top of the window if (frog.getBoundingClientRect().top>100) { frog.className = 'over-100'; } else { frog.className = ''; } }, 1000/30); 
 #frog { position: fixed; top: 0; left: 0; width: 50px; height: 50px; background-color: green; } #frog.over-100 { background-color: olive; } 
 <div id="frog"></div> 

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

相关问题 当检测到一定的最小屏幕分辨率大小时,如何通过Javascript更改CSS属性 - How can I change a CSS attribute by Javascript when a certain minimum screen resolution size is detected 如何根据 CSS 中的屏幕大小更改文本(不是字体大小)? - How to change text (not font size) according to screen size in CSS? ReactJS 改变滚动元素的颜色,当达到某个 position - ReactJS change color of element on scroll and when certain position is reached 如何将图像附加到特定位置的另一个元素上? - How to attach an image onto another element in certain spot? 特定尺寸屏幕的CSS(在我的情况下为iframe) - CSS for a certain size of the screen (iframe in my case) 如何将事件分配给可能根据屏幕尺寸而变化的元素 - how to assign an Event to an element that may change based on screen size 如何将数组分成两半,直到达到一定大小的块? - How to split an array in half until chunks of a certain size are reached? 如何在一个元素出现在屏幕上时立即更改它的 css 属性 - How to change css properties of one element as soon it shows up on the screen 检测屏幕大小的脚本,并根据屏幕大小更改css - Script for detecting the screen size,and change css according to screen size 根据屏幕尺寸更改html元素的顺序 - Change order of html element depending on screen size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM