This article mainly introduces an example of the automatic arrangement dialog box generated by js+html5. Multiple dialog boxes pop up and can be arranged automatically. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Recently written using js and html5, multiple dialog boxes pop up, and they can be automatically arranged. When the screen is full, it will automatically start from the beginning. Without further ado, just look at the picture:
How to use it It is still very convenient. If you are interested, the code is at the back
The first is the Html page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body style="width: 100vw;height: 100vh;padding: 0;margin: 0"> <input type="button" value="生成p" onclick="creatDialog()" style="position: absolute;z-index: 200;"/> <script src="index.js"></script> </body> </html>
Then js
function creatDialog() { // 获取屏幕的宽度和高度 var wid=document.body.clientWidth; var hei=document.body.clientHeight; //根据已有dialog计算下一个dialog位置 var obj=document.getElementsByClassName("dialog"); //5和10为间距 var top=5; var left=10; if(obj.length!=0){ //不是第一次生成 var h=parseInt(hei/(274+5));//求出总行数 var w=parseInt(wid/(300+10));//求出总列数 var n=parseInt(obj.length/h);//位于第n+1列 if(n+1<=w){ var m=obj.length%h//位于第m+1行 top=(274+5)*m+5; left=(300+10)*n+10; }else { //屏幕满了移除所有对象,从新开始 removeDialog(); top=5; left=10; } } //生成dialog var dialog=document.createElement('p'); dialog.className="dialog"; dialog.id="dialog"+obj.length; dialog.style.position="absolute"; dialog.style.marginLeft=left+"px"; dialog.style.marginTop=top+"px"; dialog.style.width="300px"; dialog.style.height="274px"; dialog.style.border="solid 1px"; dialog.style.backgroundColor="#FF0000"; document.body.appendChild(dialog); } function removeDialog() { var obj=document.getElementsByClassName("dialog"); var num=obj.length; for(var i=0;i<num;i++){ document.body.removeChild(document.getElementById("dialog"+i)); } }
Related recommendations:
WeChat applet uses modal component to pop up dialog box example sharing
js tutorial for making a simple dialog box
10 recommended courses on dialog boxes
The above is the detailed content of js and html5 realize the generation of automatic arrangement dialog box. For more information, please follow other related articles on the PHP Chinese website!