简体   繁体   English

如何创建js和html css

[英]How to create js and html css

Responsive width and height for a group of squares一组正方形的响应宽度和高度

I'm creating a grid of 1:1 squares.我正在创建一个 1:1 正方形的网格。 The user can keep adding squares and I want the size of the squares to be maintained at their aspect ratio but resize accordingly.用户可以继续添加正方形,我希望正方形的大小保持其纵横比,但相应地调整大小。 The tricky part is I want the square to always be visible on the page - that is to say that there is no scrolling and the webpage would be responsive with width and height.棘手的部分是我希望正方形始终在页面上可见 - 也就是说没有滚动并且网页将响应宽度和高度。

I have created an example that adds a square every second while testing this.我创建了一个示例,在测试时每秒添加一个正方形。 However, I am unable to get it working with the height part.但是,我无法让它与高度部分一起工作。 I have been able to get it working with the width.我已经能够让它与宽度一起工作。

 setInterval(() => { console.log(document.getElementsByClassName('square-container')[0]); document.getElementsByClassName('square-container')[0].innerHTML += (" <div class = 'square' > < /div>"); }, 1000);
 .square-container { display: flex; flex-wrap: wrap; } .square { position: relative; flex-basis: calc(33.333% - 10px); margin: 5px; box-sizing: border-box; background: red; transition: background 1s; } .square::before { content: ""; display: block; padding-top: 100%; }
 <div class="square-container"> <div class="square"></div> </div>

I'm not using any ui libraries like bootstrap, just vanilla html, css and javascript.我没有使用任何像 bootstrap 这样的 ui 库,只是使用 vanilla html、css 和 javascript。

One way would be to automatically scroll to the bottom on the screen as each square is added.一种方法是在添加每个方块时自动滚动到屏幕底部。 I added this line window.scrollTo(0,document.body.scrollHeight);我添加了这一行window.scrollTo(0,document.body.scrollHeight); to your code which makes it scroll down as a new line is created.到您的代码,这使它在创建新行时向下滚动。

 setInterval(() => { //console.log(document.getElementsByClassName('square-container')[0]); document.getElementsByClassName('square-container')[0].innerHTML += (" <div class = 'square' > < /div>"); window.scrollTo(0,document.body.scrollHeight); }, 1000);
 .square-container { display: flex; flex-wrap: wrap; } .square { position: relative; flex-basis: calc(33.333% - 10px); margin: 5px; box-sizing: border-box; background: red; transition: background 1s; } .square::before { content: ""; display: block; padding-top: 100%; }
 <div class="square-container"> <div class="square"></div> </div>

Otherwise if you meant that you want the squares to resize so that all of they would all fit to the height of the window that would be different.否则,如果您的意思是您希望正方形调整大小,以便所有正方形都适合不同的窗口高度。 I had a look around to see if something like this could be done but could not figure it out.我环顾四周,看看是否可以完成这样的事情,但无法弄清楚。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM