练习1_表单控制.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. /* css reset */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. li {
  14. list-style: none;
  15. }
  16. /* 工具类 */
  17. .main-width {
  18. width: 1226px;
  19. margin: 0 auto;
  20. }
  21. body {
  22. background-color: rgb(249, 250, 251);
  23. }
  24. /* header start */
  25. header {
  26. height: 100px;
  27. background-color: #4F46E5;
  28. color: #fff;
  29. }
  30. header .main-width {
  31. display: flex;
  32. flex-direction: column;
  33. justify-content: center;
  34. height: 100%;
  35. }
  36. /* header end */
  37. /* content start */
  38. .content {
  39. padding-top: 80px;
  40. display: flex;
  41. justify-content: space-between;
  42. }
  43. .content aside h2 {
  44. margin-bottom: 30px;
  45. }
  46. .content aside {
  47. width: 480px;
  48. height: 830px;
  49. background-color: #fff;
  50. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  51. border-radius: 10px;
  52. padding: 30px 20px;
  53. box-sizing: border-box;
  54. }
  55. .content aside p {
  56. margin: 10px 0;
  57. }
  58. .content aside input[type="range"] {
  59. width: 100%;
  60. background-color: rgb(229, 231, 235);
  61. /* -webkit-appearance:none; */
  62. }
  63. .content .color-content {
  64. display: flex;
  65. }
  66. .content .color-content input[type="color"] {
  67. width: 40px;
  68. height: 40px;
  69. margin-right: 10px;
  70. }
  71. .content .color-content input[type="text"] {
  72. width: 100px;
  73. height: 36px;
  74. flex-grow: 1;
  75. border: 1px solid rgb(209, 213, 219);
  76. border-radius: 5px;
  77. }
  78. .content .inp9 button {
  79. width: 100%;
  80. height: 50px;
  81. background-color: rgb(243, 244, 246);
  82. border: none;
  83. font-size: 20px;
  84. font-weight: bold;
  85. margin-top: 20px;
  86. }
  87. .content select {
  88. width: 100%;
  89. height: 36px;
  90. border: 1px solid rgb(209, 213, 219);
  91. border-radius: 5px;
  92. }
  93. .content article {
  94. width: 710px;
  95. height: 830px;
  96. background-color: #fff;
  97. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  98. border-radius: 10px;
  99. padding: 30px 20px;
  100. box-sizing: border-box;
  101. }
  102. .content article .bg {
  103. background-color: #aaa;
  104. width: 100%;
  105. height: 650px;
  106. margin-top: 30px;
  107. border-radius: 10px;
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. }
  112. .content article .box {
  113. width: 200px;
  114. height: 200px;
  115. background-color: gray;
  116. border: 1px solid blue;
  117. display: flex;
  118. justify-content: center;
  119. align-items: center;
  120. transition: all 0.5s linear;
  121. border-radius: 10px;
  122. }
  123. /* content end */
  124. </style>
  125. </head>
  126. <body>
  127. <div class="container">
  128. <header>
  129. <div class="main-width">
  130. <h1>交互式元素编辑器</h1>
  131. <p>通过表单控件实时修改元素样式和内容</p>
  132. </div>
  133. </header>
  134. <div class="content main-width">
  135. <aside>
  136. <form>
  137. <h2>控制面板</h2>
  138. <div class="inp1">
  139. <p>元素大小: 200px</p>
  140. <input type="range" min="100" max="400">
  141. </div>
  142. <div class="inp2">
  143. <p>圆角大小: 10px</p>
  144. <input type="range" min="0" max="50">
  145. </div>
  146. <div class="inp3">
  147. <p>背景颜色</p>
  148. <div class="color-content">
  149. <input type="color">
  150. <input type="text">
  151. </div>
  152. </div>
  153. <div class="inp4">
  154. <p>元素文本</p>
  155. <div class="color-content">
  156. <input type="text">
  157. </div>
  158. </div>
  159. <div class="inp5">
  160. <p>文本颜色</p>
  161. <div class="color-content">
  162. <input type="color">
  163. <input type="text">
  164. </div>
  165. </div>
  166. <div class="inp6">
  167. <p>边框样式</p>
  168. <select>
  169. <option value="solid">实线</option>
  170. <option value="dashed">虚线</option>
  171. <option value="dotted">点线</option>
  172. </select>
  173. </div>
  174. <div class="inp7">
  175. <p>边框宽度: 2px</p>
  176. <input type="range" min="1" max="10">
  177. </div>
  178. <div class="inp8">
  179. <p>阴影效果</p>
  180. <input type="checkbox" id="is-shadow">
  181. <label for="is-shadow">开启阴影</label>
  182. </div>
  183. <div class="inp9">
  184. <button type="reset">重置样式</button>
  185. </div>
  186. </form>
  187. </aside>
  188. <article>
  189. <h2>预览区域</h2>
  190. <div class="bg">
  191. <div class="box">
  192. hello world
  193. </div>
  194. </div>
  195. </article>
  196. </div>
  197. <footer>
  198. </footer>
  199. </div>
  200. <script>
  201. // 获取元素
  202. var oBox = document.querySelector(".box");
  203. // 绑定事件
  204. // 元素大小
  205. var inp1 = document.querySelector(".inp1");
  206. var inp1Text = inp1.querySelector("p");
  207. inp1 = inp1.querySelector("input");
  208. inp1.oninput = function () {
  209. console.log(this.value);
  210. inp1Text.innerText = "元素大小:" + this.value + "px";
  211. oBox.style.width = this.value + "px";
  212. oBox.style.height = this.value + "px";
  213. }
  214. // 圆角大小
  215. var inp2 = document.querySelector(".inp2 input");
  216. var inp2Text = document.querySelector(".inp2 p");
  217. console.log(inp2)
  218. inp2.oninput = function () {
  219. oBox.style.borderRadius = this.value + "px";
  220. inp2Text.innerText = "圆角大小:" + this.value + "px";
  221. }
  222. // 修改背景色
  223. var inp3 = document.querySelector(".inp3 input[type='color']");
  224. var inp3Text = document.querySelector(".inp3 input[type='text']");
  225. inp3.oninput = function () {
  226. oBox.style.backgroundColor = this.value;
  227. inp3Text.value = this.value;
  228. }
  229. // 改变文本内容
  230. var inp4 = document.querySelector(".inp4 input");
  231. inp4.oninput = function () {
  232. oBox.innerText = this.value;
  233. }
  234. // 改变文本颜色
  235. var inp5 = document.querySelector(".inp5 input[type='color']");
  236. var inp5Text = document.querySelector(".inp5 input[type='text']");
  237. inp5.oninput = function () {
  238. oBox.style.color = this.value;
  239. inp5Text.value = this.value;
  240. }
  241. // 改变边框样式
  242. var inp6 = document.querySelector(".inp6 select");
  243. inp6.onchange = function () {
  244. console.log(this.value);
  245. oBox.style.borderStyle = this.value;
  246. }
  247. // 改变边框宽度
  248. var inp7 = document.querySelector(".inp7 input");
  249. var inp7Text = document.querySelector(".inp7 p");
  250. inp7.oninput = function(){
  251. oBox.style.borderWidth = this.value + "px";
  252. inp7Text.innerText = "边框宽度:" + this.value + "px";
  253. }
  254. // 改变阴影效果
  255. var inp8 = document.querySelector(".inp8 input");
  256. inp8.onchange = function(){
  257. console.log(this.checked)
  258. if(this.checked){
  259. oBox.style.boxShadow = "0 0 10px rgba(0,0,0,0.5)";
  260. }else{
  261. oBox.style.boxShadow = "none";
  262. }
  263. }
  264. // 重置样式
  265. var inp9 = document.querySelector(".inp9 button");
  266. inp9.onclick = function(){
  267. oBox.style = "";
  268. }
  269. </script>
  270. </body>
  271. </html>