4.jsx-绑定样式.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- 将jsx转为js -->
  8. <script src="./babel.min.js"></script>
  9. <!-- react核心库 -->
  10. <script src="./react.development.js"></script>
  11. <!-- react中dom操作库 -->
  12. <script src="./react-dom.development.js"></script>
  13. <style>
  14. /* .box {
  15. width: 500px;
  16. height: 500px;
  17. border: 1px solid #000;
  18. margin: 0 auto;
  19. } */
  20. </style>
  21. </head>
  22. <body>
  23. <div id="root"></div>
  24. <!--
  25. className:
  26. className= '类名'
  27. className = {'类名'}
  28. -->
  29. <script type="text/babel">
  30. // 动态渲染样式
  31. // jsx中 style样式是对象Object格式 不能是字符串格式
  32. let newBox = {
  33. width: '500px',
  34. height: '500px',
  35. border: '1px solid #f00',
  36. margin: '0 auto',
  37. display: 'flex',
  38. justifyContent: 'space-between',
  39. alignItem: 'center'
  40. }
  41. let left = {
  42. width: '200px',
  43. height: '200px',
  44. // jsx中动态渲染样式 样式属性若是多单词组成则需要变成驼峰命名法
  45. backgroundColor: 'purple',
  46. color: "#fff"
  47. }
  48. // let box = <div className={'box'}>我的名字叫图图</div>;
  49. // 需求:盒子样式: 大盒子居中 小盒子通过弹性盒分别居左居右
  50. let box = (
  51. <div style={newBox}>
  52. <div style={left}>左侧盒子</div>
  53. <div style={{
  54. width:'200px',
  55. height:'200px',
  56. backgroundColor:'#ff0',
  57. color:'#f00'
  58. }}>右侧盒子</div>
  59. </div>
  60. )
  61. let showPage = document.querySelector("#root");
  62. ReactDOM.createRoot(showPage).render(box);
  63. </script>
  64. </body>
  65. </html>