首页 > 其他分享 >html grid 布局与拖动

html grid 布局与拖动

时间:2022-11-18 00:11:54浏览次数:49  
标签:container 拖动 text color html grid auto bubble

  1 <!DOCTYPE html>
  2 <html lang="en">
  3     <head>
  4         <meta charset="UTF-8" />
  5         <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7         <title>Document</title>
  8     </head>
  9     <script>
 10         /*
 11          * 项目与容器
 12          *
 13          * 1. 容器是一个 BFC,容器的内部不会影响到容器的外部
 14          * 2. 容器可以划分网格,为项目提供默认值,但是并不关心项目的实际位置
 15          * 3. 不允许 float,但是可以 position
 16          */
 17 
 18         /*
 19          * 容器属性:
 20          *
 21          * 1. 定义一个容器
 22          *        display: grid; inline-grid;
 23          * 2. 定义网格线
 24          *         grid-template-colums: ;
 25          *         grid-template-rows: ; 可以给网格线添加名字 [c1, other-name] 100px [c2] 100px [c3] auto [c4],
 26          * 3. 定义间隔
 27          *         column-gap: ;
 28          *         row-gap: ;
 29          *         gap: row-gap column-gap ;
 30          * 4. 定义区域,项目会用到区域名
 31          *         grid-template-areas:
 32          *             "header header header"
 33          *            "main main silder"
 34          *            "footer footer footer";
 35          * 5. 定义文档流,dense 可以使项目紧凑
 36          *         grid-auto-flow: row; column; row dense; column dense;
 37          * 6. 定义项目在轴线上的默认排版方式
 38          *         justify-items: ;
 39          *         align-items: ;
 40          *         place-items: 简写
 41          * 7. 定义轴线在容器的排版方式
 42          *         justify-content: ;
 43          *         align-content: ;
 44          *        place-content: 简写
 45          * 8. 如果项目溢出容器,可以用这个定义
 46          *         grid-auto-rows: ;
 47          * 9. grid-tempate:简写
 48          * 10. grid:简写
 49          */
 50 
 51         /* 项目属性:
 52          *
 53          * 1. 定义占用的网格,通过边界来定义;以下表示占用了左上角的四个网格
 54          *         grid-column-start: 1;
 55          *         grid-column-end: 3;
 56          *         grid-row-start: 1;
 57          *         grid-row-end: 3;
 58          * 2. 简写
 59          *         grid-column: 1 / 3 ;
 60          *         grid-row: 1 / 3;
 61          * 3. 简写
 62          *         grid-area: 1 / 1 / 3 / 3;
 63          * 4. 其余值示例一:表示从默认位置开始,横跨两列,纵跨两列,即占用四个网格
 64          *         grid-area: auto / auto / span 2 / span 2;
 65          * 5. 其余值示例二:表示占用名为 header 的全部网格
 66          *         grid-area: header
 67          * 6. 定义单个项目的排版,和 flex 相同
 68          *         justify-self: ;
 69          *         align-self: ;
 70          *         place-self: ;
 71          */
 72 
 73         /* 单位
 74          *
 75          * 1. 以上和 flex 值相同的部分省略掉了;以下是单位说明
 76          * 2. 纵横百分比:以容器的纵横为单位
 77          * 3. fr:这个是剩余空间的百分比,存在一些智能行为
 78          * 4. px:
 79          * 5. repeat(times, mode):mode 可以是多个值,比如 repeat(3, 1fr 2fr)
 80          * 6. auto-fill:项目大小确定,但是容器大小不确定的时候用,示例 repeat(auto-fill, 100px),表示尽量填充
 81          * 7. minmax(min, max)
 82          *  */
 83     </script>
 84     <style>
 85         html {
 86             height: 100%;
 87         }
 88         body {
 89             height: 100%;
 90             margin: 0;
 91         }
 92         .container {
 93             width: 100%;
 94             height: 100%;
 95             background-color: #333;
 96             color: white;
 97             display: grid;
 98             grid-template-columns: repeat(10, 10%);
 99             grid-template-rows: repeat(10, 10%);
100             grid-auto-flow: row dense;
101         }
102 
103         .item {
104             border-radius: 20px;
105             border: 4px solid #aaa;
106             margin: 2px;
107             display: flex;
108             justify-content: center;
109             align-items: center;
110         }
111 
112         .move {
113             border: 4px dashed red;
114         }
115     </style>
116     <body></body>
117 </html>
118 <script>
119     class Game {
120         constructor(option) {
121             this.init();
122             this.createBubble(option || new Array(20).fill({}));
123         }
124         init() {
125             const container = document.createElement("div");
126 
127             container.className = "container";
128 
129             container.addEventListener("dragstart", (e) => {
130                 e.target.classList.add("move");
131                 e.dataTransfer.setData("id", e.target.id);
132             });
133 
134             container.addEventListener("drag", (e) => {});
135 
136             container.addEventListener("dragend", (e) => {
137                 e.target.classList.remove("move");
138             });
139 
140             container.addEventListener("dragenter", (e) => {});
141 
142             container.addEventListener("dragover", (e) => {
143                 e.preventDefault();
144             });
145 
146             container.addEventListener("dragleave", (e) => {});
147 
148             container.addEventListener("drop", (e) => {
149                 e.preventDefault();
150                 const curDom = document.getElementById(
151                     e.dataTransfer.getData("id"),
152                 );
153                 container.insertBefore(curDom, e.target);
154             });
155 
156             this.container = container;
157         }
158         getArea() {
159             const times = 2,
160                 random = Math.random,
161                 ceil = Math.ceil;
162             return `auto / auto / span ${ceil(random() * times)} / span ${ceil(
163                 random() * times,
164             )}`;
165         }
166         createBubble(option) {
167             const container = this.container;
168             option.forEach((item, index) => {
169                 const bubble = document.createElement("div");
170                 bubble.draggable = true;
171                 bubble.id = `bubble-${index}`;
172                 bubble.className = "item";
173                 const gridArea = this.getArea();
174                 bubble.style.gridArea = gridArea;
175                 bubble.style.backgroundColor = item.color;
176                 bubble.innerText = item.text || index;
177                 container.appendChild(bubble);
178             });
179         }
180         mount(app) {
181             app.appendChild(this.container);
182         }
183     }
184 
185     const option = [
186         { text: "向钱看,向厚看", color: "#dd6b66" },
187         { text: "早睡早起", color: "#759aa0" },
188         { text: "天冷要加衣", color: "#e69d87" },
189         { text: "博观而约取", color: "#8dc1a9" },
190         { text: "厚积而薄发", color: "#ea7e53" },
191         { text: "海纳百川", color: "#eedd78" },
192         { text: "有容乃大", color: "#73a373" },
193         { text: "大风起兮云飞扬", color: "#73b9bc" },
194         { text: "今日事,今日毕", color: "#7289ab" },
195         { text: "勤能补拙是良训", color: "#91ca8c" },
196         { text: "一分辛苦一分才", color: "#f49f42" },
197         { text: "宝剑锋从磨砺出", color: "#ff715e" },
198         { text: "梅花香自苦寒来", color: "#ffaf51" },
199         { text: "纸上得来终觉浅", color: "#ffee51" },
200         { text: "绝知此事要躬行", color: "#8c6ac4" },
201         { text: "", color: "#715c87" },
202         { text: "", color: "#c12e34" },
203         { text: "", color: "#e6b600" },
204         { text: "", color: "#0098d9" },
205         { text: "", color: "#2b821d" },
206         { text: "", color: "#005eaa" },
207         { text: "", color: "#339ca8" },
208         { text: "", color: "#cda819" },
209         { text: "", color: "#32a487" },
210     ];
211 
212     new Game(option).mount(document.body);
213 </script>

标签:container,拖动,text,color,html,grid,auto,bubble
From: https://www.cnblogs.com/aurora-power/p/16901869.html

相关文章

  • 教你快速使用VSCode编写HTML文件
    我的小站——半生瓜のblog教你快速使用VSCode编写HTML文件​​1.配置​​​​2.更改默认打开浏览器​​1.配置首先在官网下载VSCODE并完成安装,[vscode官网]。(https://code.......
  • js中innerHTML和createElement的效率比较
    结合js中字符串不可变的特性单次执行innerHTML和createElement时效率差不多如果重复执行,因为innerHTML=字符串,需要多次开辟空间存储字符串,所以createElement效率更高.......
  • Caliburn.Micro框架在DataGrid列中添加按钮
    Caliburn.Micro框架在DataGrid列中添加按钮在使用Caliburn.Micro框架时,想在DataGrid列中添加按钮,走了很多弯路,记录一下。前端代码<DataGrid><DataGridTemplateColu......
  • JavaScript_语法_与html结合方式与JavaScript_语法_注释&数据类型
    JavaScript_语法_与html结合方式EMCAScript:客户端脚本语言的标准1.基本语法1.......
  • JavaScript语法-与html结合方式、注释和数据类型
    JavaScript语法-与html结合方式与HTML结合方式:1、内部JS:定义<script>,标签体内容就是js代码2、外部JS:定义<script>,通过src属性引入外部的js文件注意:1、<script>可以......
  • url的hash和html5的history
    通过以下可以改变浏览器中的url跳转到home路径下:location.hash='home'跳转到about路径下:history.pushState({},'','about')向上回退:history.back()向前进:history.f......
  • 用html5 实现断点续传 (HTTP)
    ​ 总结一下大文件分片上传和断点续传的问题。因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况。http的网络请求中本身就已经具备了分片上传功能,当传输的文件比......
  • 2021-07-17 从零开始的一日HTML
    <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>狗子的工作站</title></head><body><divalign="left"><img......
  • CSS概述和CSS_与html结合方式
    CSS概述:css:页面美化和布局控制:1.概念:cascadingstylesheets层叠样式表层叠:多个样式可以作用在同一个html的元秦上,同时生效2.好处∶1.功能强大2.将内容展示和样式控......
  • WPF 点击DataGrid中按钮无触发
    上干货<DataGridTemplateColumnHeader="操作"Width="1*"><DataGridTemplateColumn.CellTemplate>......