评论

收藏

[Html/CSS] 用Html写一个在线取名的小工具

开发技术 开发技术 发布于:2025-09-21 20:43 | 阅读数:200 | 评论:0

利用AI工具写的一个纯HTML在线取名工具,输入你的姓氏即随机生成10个名字!

Snipaste_2025-09-21_20-51-56.png

html代码:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>姓名生成器</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Microsoft YaHei', Arial, sans-serif;
      background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
      min-height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 20px;
      color: white;
    }

    .container {
      background: rgba(255, 255, 255, 0.1);
      border-radius: 20px;
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
      padding: 40px;
      max-width: 800px;
      width: 100%;
      backdrop-filter: blur(10px);
      border: 1px solid rgba(255, 255, 255, 0.2);
    }

    .title {
      text-align: center;
      color: white;
      font-size: 28px;
      margin-bottom: 30px;
      font-weight: bold;
      text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }

    .form-group {
      margin-bottom: 25px;
    }

    .form-group label {
      display: block;
      margin-bottom: 8px;
      color: white;
      font-weight: 500;
      font-size: 16px;
    }

    .form-group input, .form-group select {
      width: 100%;
      padding: 12px 15px;
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 10px;
      font-size: 16px;
      transition: all 0.3s ease;
      color: white;
    }

    .form-group input::placeholder {
      color: rgba(255, 255, 255, 0.5);
    }

    .form-group input:focus, .form-group select:focus {
      outline: none;
      border-color: rgba(255, 255, 255, 0.4);
      background: rgba(255, 255, 255, 0.15);
      box-shadow: 0 0 10px rgba(102, 126, 234, 0.5);
    }

    /* 修复下拉框选项样式 */
    .form-group select option {
      background: #2c5364;
      color: white;
      padding: 10px;
    }

    .radio-group {
      display: flex;
      gap: 20px;
      margin-top: 10px;
    }

    .radio-item {
      display: flex;
      align-items: center;
      cursor: pointer;
    }

    .radio-item input[type="radio"] {
      width: auto;
      margin-right: 8px;
      cursor: pointer;
    }

    .radio-item label {
      margin-bottom: 0;
      cursor: pointer;
      color: white;
    }

    .generate-btn {
      width: 100%;
      padding: 15px;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      border: none;
      border-radius: 10px;
      font-size: 18px;
      font-weight: bold;
      cursor: pointer;
      transition: all 0.3s ease;
      margin-bottom: 25px;
      box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    }

    .generate-btn:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
    }

    .generate-btn:active {
      transform: translateY(0);
    }

    .results-container {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      gap: 15px;
      margin-bottom: 25px;
      min-height: 200px;
    }

    .result-item {
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 10px;
      padding: 15px;
      text-align: center;
      transition: all 0.3s ease;
      cursor: pointer;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .result-item:hover {
      background: rgba(255, 255, 255, 0.2);
      transform: translateY(-2px);
      box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    }

    .result-text {
      font-size: 20px;
      font-weight: bold;
      color: white;
      letter-spacing: 1px;
      margin-right: 30px;
    }

    .copy-btn {
      position: absolute;
             top: 8px;
      right: 8px;
      background: transparent;
      border: none;
      padding: 6px;
      font-size: 16px;
      color: rgba(255, 255, 255, 0.7);
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 30px;
      height: 30px;
    }

    .copy-btn:hover {
      color: rgba(255, 255, 255, 1);
      transform: scale(1.1);
    }

    .copy-btn:active {
      transform: scale(0.95);
    }

    .copy-btn.copied {
      color: rgba(76, 175, 80, 1);
    }

    .copy-icon::before {
      content: "📋";
    }

    .copied .copy-icon::before {
      content: "✓";
    }

    .result-empty {
      color: rgba(255, 255, 255, 0.5);
      font-size: 16px;
      text-align: center;
      grid-column: 1 / -1;
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 200px;
    }

    .history {
      margin-top: 30px;
      max-height: 200px;
      overflow-y: auto;
    }

    .history-title {
      color: white;
      font-size: 18px;
      margin-bottom: 15px;
      font-weight: bold;
      text-shadow: 0 1px 2px rgba(0,0,0,0.2);
    }

    .history-item {
      background: rgba(255, 255, 255, 0.1);
      padding: 10px 15px;
      margin-bottom: 8px;
      border-radius: 8px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      animation: slideIn 0.3s ease;
      border: 1px solid rgba(255, 255, 255, 0.1);
    }

    @keyframes slideIn {
      from {
        opacity: 0;
        transform: translateX(-20px);
      }
      to {
        opacity: 1;
        transform: translateX(0);
      }
    }

    .history-item-name {
      font-weight: 500;
      color: white;
    }

    .history-item-time {
      font-size: 12px;
      color: rgba(255, 255, 255, 0.7);
    }

    .clear-history {
      background: rgba(255, 255, 255, 0.1);
      color: white;
      border: 1px solid rgba(255, 255, 255, 0.2);
      padding: 8px 15px;
      border-radius: 6px;
      cursor: pointer;
      font-size: 14px;
      margin-top: 10px;
      transition: all 0.3s ease;
    }

    .clear-history:hover {
      background: rgba(255, 255, 255, 0.2);
      box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
    }

    .stats {
      display: flex;
      justify-content: space-around;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid rgba(255, 255, 255, 0.1);
    }

    .stat-item {
      text-align: center;
    }

    .stat-number {
      font-size: 24px;
      font-weight: bold;
      color: white;
      text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    }

    .stat-label {
      font-size: 14px;
      color: rgba(255, 255, 255, 0.7);
      margin-top: 5px;
    }

    @media (max-width: 480px) {
      .container {
        padding: 30px 20px;
      }
      
      .title {
        font-size: 24px;
      }
      
      .radio-group {
        flex-direction: column;
        gap: 10px;
      }
      
      .results-container {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body>
  <div class="container">
    <h1 class="title">🎯 姓名生成器</h1>
    
    <div class="form-group">
      <label for="surname">请输入姓氏:</label>
      <input type="text" id="surname" placeholder="例如:张、李、王..." maxlength="10">
    </div>

    <div class="form-group">
      <label>选择性别:</label>
      <div class="radio-group">
        <div class="radio-item">
          <input type="radio" id="male" name="gender" value="male" checked>
          <label for="male">👨 男性</label>
        </div>
        <div class="radio-item">
          <input type="radio" id="female" name="gender" value="female">
          <label for="female">👩 女性</label>
        </div>
      </div>
    </div>

    <div class="form-group">
      <label for="nameLength">名字字数:</label>
      <select id="nameLength">
        <option value="1">1个字</option>
        <option value="2" selected>2个字</option>
        <option value="3">3个字</option>
      </select>
    </div>

    <button class="generate-btn" onclick="generateNames()">🎲 生成10个姓名</button>

    <div class="results-container" id="resultsContainer">
      <div class="result-empty">点击上方按钮生成姓名</div>
    </div>

    <div class="stats">
      <div class="stat-item">
        <div class="stat-number" id="totalCount">0</div>
        <div class="stat-label">总生成数</div>
      </div>
      <div class="stat-item">
        <div class="stat-number" id="todayCount">0</div>
        <div class="stat-label">今日生成</div>
      </div>
      <div class="stat-item">
        <div class="stat-number" id="historyCount">0</div>
        <div class="stat-label">历史记录</div>
      </div>
    </div>

    <div class="history">
      <div class="history-title">📋 生成历史</div>
      <div id="historyList"></div>
      <button class="clear-history" onclick="clearHistory()">清空历史</button>
    </div>
  </div>

  <script>
    // 扩展的名字库
    const nameDatabase = {
      male: {
        1: ['伟', '强', '磊', '洋', '勇', '军', '杰', '涛', '超', '峰', '鹏', '斌', '宇', '浩', '凯', '鑫', '毅', '霖', '彬', '哲', '明', '辉', '华', '刚', '健', '忠', '良', '龙', '虎', '豹', '骏', '骐', '麒', '麟', '鸿', '鹰', '翔', '飞', '跃', '腾', '达', '富', '贵', '福', '禄', '寿', '喜', '庆', '祥', '瑞', '兆', '吉', '利', '安', '康', '宁', '泰', '和', '平', '顺', '通', '畅', '达', '远', '宏', '大', '广', '博', '深', '厚', '重', '实', '业', '功', '成', '立', '建', '树', '立', '建', '创', '造', '作', '为', '行', '动', '力', '势', '能', '量', '光', '热', '火', '焰', '炎', '焱', '燚', '水', '泉', '溪', '河', '江', '海', '洋', '波', '涛', '浪', '潮', '流', '涌', '澎', '湃', '汹', '涌', '澎', '湃', '山', '岳', '岭', '峰', '峦', '岩', '石', '礁', '岛', '屿', '岸', '坡', '坝', '堤', '岸', '坡', '坝', '堤', '田', '野', '原', '园', '林', '森', '木', '松', '柏', '槐', '榆', '柳', '杨', '榕', '梧', '桐', '枫', '桦', '樟', '楠', '梓', '橡', '樱', '桃', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬'],
        2: ['子轩', '浩然', '宇轩', '子涵', '雨泽', '浩宇', '俊杰', '博文', '天佑', '致远', '明轩', '天佑', '文昊', '修洁', '黎昕', '鸿涛', '伟祺', '荣轩', '越泽', '浩宇', '瑾瑜', '皓轩', '擎苍', '擎宇', '志泽', '子睿', '宇辰', '翊宸', '逸宸', '铭轩', '锦程', '嘉熙', '鹏飞', '煜祺', '懿轩', '烨伟', '苑博', '伟泽', '熠彤', '鸿煊', '烨霖', '宸赫', '昊天', '思聪', '展鹏', '笑愚', '志强', '炫明', '雪松', '思源', '智渊', '思淼', '晓啸', '天宇', '浩然', '文轩', '鹭洋', '振家', '乐驹', '晓博', '文博', '昊焱', '立果', '金鑫', '锦程', '嘉熙', '鹏飞', '子默', '思远', '浩轩', '语堂', '聪健', '明杰', '梓豪', '梓轩', '皓轩', '浩宇', '宇航', '俊宇', '铭宇', '辰宇', '晨曦', '晨轩', '晨宇', '煜城', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿', '煜城', '懿轩', '烨华', '煜祺', '智宸', '正豪', '昊然', '明辉', '弘文', '峻熙', '嘉懿']
      },
      female: {
        1: ['娜', '婷', '雪', '敏', '静', '丽', '芳', '艳', '娟', '霞', '莉', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '婷', '媛', '琳', '玲', '霞', '莹', '颖', '欣', '怡', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬', '梅', '兰', '竹', '菊', '荷', '莲', '茉', '莉', '玫', '瑰', '牡', '丹', '芍', '药', '丁', '香', '桂', '花', '桃', '李', '杏', '梨', '樱', '桃', '春', '夏', '秋', '冬'],
        2: ['雨萱', '诗涵', '思雅', '梦瑶', '婉婷', '静怡', '欣怡', '雅静', '梦洁', '雨婷', '思琪', '雪怡', '婉茹', '雅婷', '梦瑶', '雨薇', '诗琪', '思怡', '梦雨', '婉君', '静雅', '欣妍', '雅芙', '梦菲', '雨欣', '诗雅', '思雨', '梦雪', '婉琳', '静香', '欣悦', '雅馨', '梦露', '雨琴', '诗琴', '思琴', '梦琴', '婉琴', '静琴', '欣琴', '雅琴', '梦琴', '雨嫣', '诗嫣', '思嫣', '梦嫣', '婉嫣', '静嫣', '欣嫣', '雅嫣', '梦嫣', '雨妍', '诗妍', '思妍', '梦妍', '婉妍', '静妍', '欣妍', '雅妍', '梦妍', '雨婧', '诗婧', '思婧', '梦婧', '婉婧', '静婧', '欣婧', '雅婧', '梦婧', '雨媛', '诗媛', '思媛', '梦媛', '婉媛', '静媛', '欣媛', '雅媛', '梦媛', '雨娜', '诗娜', '思娜', '梦娜', '婉娜', '静娜', '欣娜', '雅娜', '梦娜', '雨婷', '诗婷', '思婷', '梦婷', '婉婷', '静婷', '欣婷', '雅婷', '梦婷', '雨琳', '诗琳', '思琳', '梦琳', '婉琳', '静琳', '欣琳', '雅琳', '梦琳', '雨玲', '诗玲', '思玲', '梦玲', '婉玲', '静玲', '欣玲', '雅玲', '梦玲', '雨霞', '诗霞', '思霞', '梦霞', '婉霞', '静霞', '欣霞', '雅霞', '梦霞', '雨莹', '诗莹', '思莹', '梦莹', '婉莹', '静莹', '欣莹', '雅莹', '梦莹', '雨颖', '诗颖', '思颖', '梦颖', '婉颖', '静颖', '欣颖', '雅颖', '梦颖', '雨欣', '诗欣', '思欣', '梦欣', '婉欣', '静欣', '欣怡', '雅欣', '梦欣', '雨怡', '诗怡', '思怡', '梦怡', '婉怡', '静怡', '欣怡', '雅怡', '梦怡'],
        3: ['雨萱儿', '诗涵雅', '思雅静', '梦瑶雨', '婉婷雅', '静怡欣', '欣怡然', '雅静怡', '梦洁雅', '雨婷儿', '诗琪雅', '思雨静', '梦雪雅', '婉琳雅', '静香雅', '欣悦雅', '雅馨静', '梦露雅', '雨琴雅', '诗琴雅', '思琴雅', '梦琴雅', '婉琴雅', '静琴雅', '欣琴雅', '雅琴静', '梦琴雅', '雨嫣雅', '诗嫣雅', '思嫣雅', '梦嫣雅', '婉嫣雅', '静嫣雅', '欣嫣雅', '雅嫣静', '梦嫣雅', '雨妍雅', '诗妍雅', '思妍雅', '梦妍雅', '婉妍雅', '静妍雅', '欣妍雅', '雅妍静', '梦妍雅', '雨婧雅', '诗婧雅', '思婧雅', '梦婧雅', '婉婧雅', '静婧雅', '欣婧雅', '雅婧静', '梦婧雅', '雨媛雅', '诗媛雅', '思媛雅', '梦媛雅', '婉媛雅', '静媛雅', '欣媛雅', '雅媛静', '梦媛雅', '雨娜雅', '诗娜雅', '思娜雅', '梦娜雅', '婉娜雅', '静娜雅', '欣娜雅', '雅娜静', '梦娜雅', '雨婷雅', '诗婷雅', '思婷雅', '梦婷雅', '婉婷雅', '静婷雅', '欣婷雅', '雅婷静', '梦婷雅', '雨琳雅', '诗琳雅', '思琳雅', '梦琳雅', '婉琳雅', '静琳雅', '欣琳雅', '雅琳静', '梦琳雅', '雨玲雅', '诗玲雅', '思玲雅', '梦玲雅', '婉玲雅', '静玲雅', '欣玲雅', '雅玲静', '梦玲雅', '雨霞雅', '诗霞雅', '思霞雅', '梦霞雅', '婉霞雅', '静霞雅', '欣霞雅', '雅霞静', '梦霞雅', '雨莹雅', '诗莹雅', '思莹雅', '梦莹雅', '婉莹雅', '静莹雅', '欣莹雅', '雅莹静', '梦莹雅', '雨颖雅', '诗颖雅', '思颖雅', '梦颖雅', '婉颖雅', '静颖雅', '欣颖雅', '雅颖静', '梦颖雅', '雨欣雅', '诗欣雅', '思欣雅', '梦欣雅', '婉欣雅', '静欣雅', '欣怡雅', '雅欣静', '梦欣雅', '雨怡雅', '诗怡雅', '思怡雅', '梦怡雅', '婉怡雅', '静怡雅', '欣怡雅', '雅怡静', '梦怡雅']
      }
    };

    // 初始化统计数据
    let stats = JSON.parse(localStorage.getItem('nameGeneratorStats')) || {
      total: 0,
      today: 0,
      lastDate: new Date().toDateString()
    };

    // 初始化历史记录
    let history = JSON.parse(localStorage.getItem('nameGeneratorHistory')) || [];

    // 页面加载时更新显示
    window.onload = function() {
      updateStats();
      updateHistoryDisplay();
      checkNewDay();
    };

    // 检查是否是新的一天
    function checkNewDay() {
      const today = new Date().toDateString();
      if (stats.lastDate !== today) {
        stats.today = 0;
        stats.lastDate = today;
        saveStats();
      }
    }

    // 复制到剪贴板功能
    async function copyToClipboard(text, button) {
      try {
        await navigator.clipboard.writeText(text);
        
        // 视觉反馈
        button.classList.add('copied');
        
        setTimeout(() => {
          button.classList.remove('copied');
        }, 1500);
        
      } catch (err) {
        // 降级方案
        const textArea = document.createElement('textarea');
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);
        
        button.classList.add('copied');
        
        setTimeout(() => {
          button.classList.remove('copied');
        }, 1500);
      }
    }

    // 生成10个姓名
    function generateNames() {
      const surname = document.getElementById('surname').value.trim();
      const gender = document.querySelector('input[name="gender"]:checked').value;
      const nameLength = parseInt(document.getElementById('nameLength').value);

      if (!surname) {
        alert('请输入姓氏!');
        return;
      }

      const resultsContainer = document.getElementById('resultsContainer');
      resultsContainer.innerHTML = '';

      const names = [];
      const nameCount = 12;

      // 生成10个不重复的姓名
      for (let i = 0; i < nameCount; i++) {
        let randomName;
        let fullName;
        let attempts = 0;
        
        do {
          randomName = nameDatabase[gender][nameLength][Math.floor(Math.random() * nameDatabase[gender][nameLength].length)];
          fullName = surname + randomName;
          attempts++;
        } while (names.includes(fullName) && attempts < 50);
        
        names.push(fullName);
        
        const resultItem = document.createElement('div');
        resultItem.className = 'result-item';
        resultItem.innerHTML = `
          <div class="result-text">${fullName}</div>
          <button class="copy-btn" onclick="copyToClipboard('${fullName}', this)" title="复制姓名">
            <span class="copy-icon"></span>
          </button>
        `;
        resultItem.onclick = function(e) {
          if (e.target.closest('.copy-btn')) return;
          addToHistory(fullName);
          // 添加点击效果
          this.style.background = 'rgba(102, 126, 234, 0.3)';
          setTimeout(() => {
            this.style.background = 'rgba(255, 255, 255, 0.1)';
          }, 200);
        };
        
        resultsContainer.appendChild(resultItem);
      }

      // 更新统计数据
      updateStats();
    }

    // 添加到历史记录
    function addToHistory(name) {
      const now = new Date();
      const timeString = now.toLocaleTimeString('zh-CN', { 
        hour: '2-digit', 
        minute: '2-digit' 
      });
      
      // 检查是否已存在
      const exists = history.some(item => item.name === name);
      if (!exists) {
        history.unshift({
          name: name,
          time: timeString,
          date: now.toDateString()
        });
        
        // 只保留最近20条记录
        if (history.length > 20) {
          history = history.slice(0, 20);
        }
        
        localStorage.setItem('nameGeneratorHistory', JSON.stringify(history));
        updateHistoryDisplay();
        
        // 更新统计数据
        stats.total++;
        stats.today++;
        saveStats();
      }
    }

    // 更新历史记录显示
    function updateHistoryDisplay() {
      const historyList = document.getElementById('historyList');
      
      if (history.length === 0) {
        historyList.innerHTML = '<div style="text-align: center; color: #999; padding: 20px;">暂无历史记录</div>';
        return;
      }
      
      historyList.innerHTML = history.map(item => `
        <div class="history-item">
          <span class="history-item-name">${item.name}</span>
          <span class="history-item-time">${item.time}</span>
        </div>
      `).join('');
      
      document.getElementById('historyCount').textContent = history.length;
    }

    // 清空历史记录
    function clearHistory() {
      if (confirm('确定要清空所有历史记录吗?')) {
        history = [];
        localStorage.removeItem('nameGeneratorHistory');
        updateHistoryDisplay();
      }
    }

    // 更新统计数据
    function updateStats() {
      document.getElementById('totalCount').textContent = stats.total;
      document.getElementById('todayCount').textContent = stats.today;
      document.getElementById('historyCount').textContent = history.length;
    }

    // 保存统计数据
    function saveStats() {
      localStorage.setItem('nameGeneratorStats', JSON.stringify(stats));
    }

    // 回车键生成姓名
    document.getElementById('surname').addEventListener('keypress', function(e) {
      if (e.key === 'Enter') {
        generateNames();
      }
    });

    // 添加一些额外的功能
    document.addEventListener('DOMContentLoaded', function() {
      // 为输入框添加焦点效果
      const inputs = document.querySelectorAll('input, select');
      inputs.forEach(input => {
        input.addEventListener('focus', function() {
          this.parentElement.style.transform = 'scale(1.02)';
        });
        input.addEventListener('blur', function() {
          this.parentElement.style.transform = 'scale(1)';
        });
      });
    });
  </script>
</body>
</html>

以上是姓名生成小工具的实现代码,这份代码实现了:

  • 用户界面设计
    • 采用现代化UI设计,包含渐变背景、卡片式布局和圆角元素
    • 响应式布局适配不同设备屏幕
    • 包含动画效果增强用户体验
  • 核心功能实现
    • 姓氏自定义输入功能
    • 性别选择(男/女/中性)功能
    • 名字字数选择(1-3个字)功能
    • 随机姓名生成算法
  • 数据管理
    • 分类名字数据库(按性别和字数分类)
    • 本地存储历史记录(最近20条)
    • 统计数据持久化(总生成数/今日生成数)
  • 附加功能
    • 生成历史展示
    • 一键清空历史记录
    • 回车键快捷生成
    • 输入框焦点动画效果
  • 代码结构
    • HTML/CSS/JavaScript分离但集中在一个文件中
    • 使用localStorage实现数据持久化
    • 模块化函数设计(生成/历史/统计功能分离)

这个工具适合直接保存为HTML文件使用,无需额外依赖,所有功能开箱即用。








112281 经验
221 文档
售价:0金币
  • 普通用户购买价格 : 0 金币
  • VIP购买价格 : 免费
  • 文件名称: dict.rar
  • 文件大小: 6.2 KB
  • 下载次数:12
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中