颜色选择器.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. <script src="./babel.min.js"></script>
  8. <script src="./react.development.js"></script>
  9. <script src="./react-dom.development.js"></script>
  10. </head>
  11. <body>
  12. <div id="root"></div>
  13. <script type="text/babel">
  14. const root = ReactDOM.createRoot(document.getElementById("root"));
  15. function App() {
  16. return (
  17. <div>
  18. <ColorSwitcherContainer />
  19. </div>
  20. )
  21. }
  22. // 纯函数组件 - 负责UI展示
  23. function ColorSwitcher(props) {
  24. const { currentColor, colors, onColorClick } = props;
  25. return (
  26. <div style={containerStyle}>
  27. <h2 style={titleStyle}>颜色点击切换器</h2>
  28. {/* 颜色展示区 */}
  29. <div
  30. style={{
  31. ...colorDisplayStyle,
  32. backgroundColor: currentColor.code
  33. }}
  34. >
  35. <span style={colorNameStyle}>
  36. {currentColor.name}
  37. </span>
  38. <span style={colorCodeStyle}>
  39. {currentColor.code}
  40. </span>
  41. </div>
  42. {/* 颜色选择区 */}
  43. <div style={colorSelectorStyle}>
  44. {colors.map((color, index) => (
  45. <button
  46. key={index}
  47. onClick={() => onColorClick(index)}
  48. style={{
  49. ...colorButtonStyle,
  50. backgroundColor: color.code,
  51. border: currentColor.code === color.code ? '3px solid #333' : 'none'
  52. }}
  53. title={color.name}
  54. />
  55. ))}
  56. </div>
  57. <p style={instructionStyle}>点击下方颜色块切换颜色</p>
  58. </div>
  59. );
  60. }
  61. // 类组件 - 负责状态管理
  62. class ColorSwitcherContainer extends React.Component {
  63. constructor(props) {
  64. super(props);
  65. // 颜色数据
  66. this.colors = [
  67. { name: '红色', code: '#ff4444' },
  68. { name: '绿色', code: '#00C851' },
  69. { name: '蓝色', code: '#33b5e5' },
  70. { name: '黄色', code: '#ffbb33' },
  71. { name: '紫色', code: '#aa66cc' },
  72. { name: '橙色', code: '#ff9900' },
  73. { name: '粉色', code: '#ff4444' },
  74. { name: '青色', code: '#00ffff' }
  75. ];
  76. this.state = {
  77. currentIndex: 0 // 默认选中第一个颜色
  78. };
  79. // 绑定事件处理函数
  80. this.handleColorClick = this.handleColorClick.bind(this);
  81. }
  82. // 处理颜色点击
  83. handleColorClick(index) {
  84. this.setState({
  85. currentIndex: index
  86. });
  87. }
  88. render() {
  89. const currentColor = this.colors[this.state.currentIndex];
  90. return (
  91. <ColorSwitcher
  92. currentColor={currentColor}
  93. colors={this.colors}
  94. onColorClick={this.handleColorClick}
  95. />
  96. );
  97. }
  98. }
  99. // 样式定义
  100. const containerStyle = {
  101. maxWidth: '500px',
  102. margin: '20px auto',
  103. padding: '20px',
  104. textAlign: 'center',
  105. fontFamily: 'Arial, sans-serif'
  106. };
  107. const titleStyle = {
  108. color: '#333',
  109. marginBottom: '20px'
  110. };
  111. const colorDisplayStyle = {
  112. width: '100%',
  113. height: '150px',
  114. borderRadius: '8px',
  115. marginBottom: '25px',
  116. display: 'flex',
  117. flexDirection: 'column',
  118. justifyContent: 'center',
  119. alignItems: 'center',
  120. transition: 'background-color 0.3s ease'
  121. };
  122. const colorNameStyle = {
  123. fontSize: '24px',
  124. fontWeight: 'bold',
  125. color: 'white',
  126. textShadow: '0 1px 2px rgba(0,0,0,0.5)',
  127. margin: '0 0 5px 0'
  128. };
  129. const colorCodeStyle = {
  130. fontSize: '16px',
  131. color: 'white',
  132. textShadow: '0 1px 2px rgba(0,0,0,0.5)',
  133. margin: 0
  134. };
  135. const colorSelectorStyle = {
  136. display: 'flex',
  137. flexWrap: 'wrap',
  138. gap: '10px',
  139. justifyContent: 'center',
  140. marginBottom: '20px'
  141. };
  142. const colorButtonStyle = {
  143. width: '50px',
  144. height: '50px',
  145. borderRadius: '50%',
  146. cursor: 'pointer',
  147. transition: 'transform 0.2s ease',
  148. outline: 'none',
  149. '&:hover': {
  150. transform: 'scale(1.1)'
  151. }
  152. };
  153. const instructionStyle = {
  154. color: '#666',
  155. fontSize: '14px'
  156. };
  157. root.render(<App />);
  158. </script>
  159. </body>
  160. </html>