表情选择.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <EmojiPicker />
  19. </div>
  20. )
  21. }
  22. class EmojiPicker extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. // 选中的表情索引,-1表示未选择
  27. selectedIndex: -1,
  28. // 悬停的表情索引,-1表示无悬停
  29. hoverIndex: -1
  30. };
  31. // 绑定事件处理函数
  32. this.handleSelect = this.handleSelect.bind(this);
  33. this.handleHover = this.handleHover.bind(this);
  34. this.handleLeave = this.handleLeave.bind(this);
  35. this.clearSelection = this.clearSelection.bind(this);
  36. }
  37. // 表情数据
  38. emojis = [
  39. { symbol: '😊', name: '开心' },
  40. { symbol: '😍', name: '爱慕' },
  41. { symbol: '😂', name: '大笑' },
  42. { symbol: '😢', name: '难过' },
  43. { symbol: '😠', name: '生气' },
  44. { symbol: '😲', name: '惊讶' },
  45. { symbol: '😴', name: '困倦' },
  46. { symbol: '🤔', name: '思考' }
  47. ];
  48. // 选择表情
  49. handleSelect(index) {
  50. this.setState({
  51. selectedIndex: index
  52. });
  53. }
  54. // 鼠标悬停表情
  55. handleHover(index) {
  56. this.setState({
  57. hoverIndex: index
  58. });
  59. }
  60. // 鼠标离开表情
  61. handleLeave() {
  62. this.setState({
  63. hoverIndex: -1
  64. });
  65. }
  66. // 清除选择
  67. clearSelection() {
  68. this.setState({
  69. selectedIndex: -1
  70. });
  71. }
  72. render() {
  73. const { selectedIndex, hoverIndex } = this.state;
  74. const selectedEmoji = selectedIndex !== -1 ? this.emojis[selectedIndex] : null;
  75. return (
  76. <div style={containerStyle}>
  77. <h2 style={titleStyle}>表情选择器</h2>
  78. {/* 表情选择区域 */}
  79. <div style={emojiGridStyle}>
  80. {this.emojis.map((emoji, index) => (
  81. <div
  82. key={index}
  83. onClick={() => this.handleSelect(index)}
  84. onMouseEnter={() => this.handleHover(index)}
  85. onMouseLeave={this.handleLeave}
  86. style={{
  87. ...emojiItemStyle,
  88. // 选中或悬停时的样式
  89. transform: (selectedIndex === index || hoverIndex === index)
  90. ? 'scale(1.2)'
  91. : 'scale(1)',
  92. backgroundColor: selectedIndex === index ? '#e3f2fd' : 'transparent',
  93. borderColor: selectedIndex === index ? '#2196F3' : '#eee'
  94. }}
  95. >
  96. <span style={emojiSymbolStyle}>{emoji.symbol}</span>
  97. <span style={emojiNameStyle}>{emoji.name}</span>
  98. </div>
  99. ))}
  100. </div>
  101. {/* 选择结果 */}
  102. {selectedEmoji && (
  103. <div style={selectionResultStyle}>
  104. <p>你选择了:{selectedEmoji.symbol} {selectedEmoji.name}</p>
  105. <button
  106. onClick={this.clearSelection}
  107. style={clearButtonStyle}
  108. >
  109. 清除选择
  110. </button>
  111. </div>
  112. )}
  113. {!selectedEmoji && (
  114. <p style={instructionStyle}>请点击一个表情来表达你的心情</p>
  115. )}
  116. </div>
  117. );
  118. }
  119. }
  120. // 样式定义
  121. const containerStyle = {
  122. maxWidth: '600px',
  123. margin: '20px auto',
  124. padding: '20px',
  125. textAlign: 'center',
  126. fontFamily: 'Arial, sans-serif',
  127. border: '1px solid #e0e0e0',
  128. borderRadius: '10px',
  129. boxShadow: '0 2px 8px rgba(0,0,0,0.1)'
  130. };
  131. const titleStyle = {
  132. color: '#333',
  133. marginBottom: '25px'
  134. };
  135. const emojiGridStyle = {
  136. display: 'grid',
  137. gridTemplateColumns: 'repeat(4, 1fr)',
  138. gap: '15px',
  139. marginBottom: '25px'
  140. };
  141. const emojiItemStyle = {
  142. padding: '15px 10px',
  143. border: '1px solid #eee',
  144. borderRadius: '8px',
  145. cursor: 'pointer',
  146. transition: 'all 0.2s ease',
  147. };
  148. const emojiSymbolStyle = {
  149. fontSize: '32px',
  150. display: 'block',
  151. marginBottom: '5px'
  152. };
  153. const emojiNameStyle = {
  154. fontSize: '14px',
  155. color: '#666'
  156. };
  157. const selectionResultStyle = {
  158. padding: '15px',
  159. backgroundColor: '#f8f9fa',
  160. borderRadius: '8px',
  161. marginBottom: '10px'
  162. };
  163. const clearButtonStyle = {
  164. padding: '6px 12px',
  165. marginTop: '10px',
  166. backgroundColor: '#f44336',
  167. color: 'white',
  168. border: 'none',
  169. borderRadius: '4px',
  170. cursor: 'pointer',
  171. fontSize: '14px'
  172. };
  173. const instructionStyle = {
  174. color: '#666',
  175. fontStyle: 'italic'
  176. };
  177. root.render(<App />);
  178. </script>
  179. </body>
  180. </html>