xhs graphic production.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>小红书内容卡片</title>
  7. <style>
  8. body {
  9. display: flex;
  10. flex-direction: column;
  11. justify-content: center;
  12. align-items: center;
  13. min-height: 100vh;
  14. background: #f0f0f0;
  15. margin: 0;
  16. font-family: 'Arial', sans-serif;
  17. }
  18. .export-container {
  19. background: #333333;
  20. padding: 10px;
  21. border-radius: 8px;
  22. }
  23. .card {
  24. width: 320px;
  25. background: #000000;
  26. border: 2px solid #666666; /* 改为灰色边框 */
  27. box-shadow: 4px 4px 0 #666666; /* 改为灰色阴影 */
  28. padding: 20px;
  29. box-sizing: border-box;
  30. }
  31. .title {
  32. font-size: 24px;
  33. font-weight: bold;
  34. color: #ffffff;
  35. border-bottom: 2px solid #666666; /* 改为灰色边框 */
  36. padding-bottom: 10px;
  37. margin-bottom: 15px;
  38. word-wrap: break-word;
  39. background-color: #000000;
  40. }
  41. .content {
  42. font-size: 16px;
  43. color: #ffffff;
  44. line-height: 1.5;
  45. word-wrap: break-word;
  46. background-color: #000000;
  47. }
  48. .editable {
  49. position: relative;
  50. min-height: 1em;
  51. }
  52. .editable:empty:before {
  53. content: attr(data-placeholder);
  54. color: #666666;
  55. position: absolute;
  56. pointer-events: none;
  57. }
  58. .editable:focus {
  59. outline: none;
  60. background-color: #000000;
  61. }
  62. [contenteditable] {
  63. -webkit-user-select: text;
  64. user-select: text;
  65. background-color: #000000 !important;
  66. }
  67. [contenteditable]:focus {
  68. background-color: #000000 !important;
  69. }
  70. ::selection {
  71. background-color: #333333;
  72. color: #ffffff;
  73. }
  74. .button-container {
  75. margin-top: 20px;
  76. text-align: center;
  77. }
  78. .export-btn {
  79. padding: 10px 20px;
  80. background: #000000;
  81. color: #ffffff;
  82. border: 2px solid #666666; /* 添加灰色边框保持一致性 */
  83. cursor: pointer;
  84. font-size: 16px;
  85. }
  86. .export-btn:hover {
  87. background: #333333;
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <div class="export-container" id="export-container">
  93. <div id="card" class="card">
  94. <div class="title editable" contenteditable="true" data-placeholder="点击输入标题"></div>
  95. <div class="content editable" contenteditable="true" data-placeholder="点击输入内容"></div>
  96. </div>
  97. </div>
  98. <div class="button-container">
  99. <button class="export-btn" onclick="exportToPng()">导出为PNG</button>
  100. </div>
  101. <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
  102. <script>
  103. function exportToPng() {
  104. // 清除选中状态
  105. window.getSelection().removeAllRanges();
  106. const container = document.getElementById('export-container');
  107. html2canvas(container, {
  108. backgroundColor: '#333333',
  109. scale: 2,
  110. useCORS: true,
  111. logging: false,
  112. removeContainer: true,
  113. foreignObjectRendering: false,
  114. onclone: (document) => {
  115. const editables = document.querySelectorAll('.editable');
  116. editables.forEach(el => {
  117. // 确保克隆的元素保持相同的样式
  118. el.style.backgroundColor = '#000000';
  119. if (el.classList.contains('title')) {
  120. el.style.borderBottom = '2px solid #666666';
  121. }
  122. });
  123. // 确保卡片样式在克隆中保持一致
  124. const card = document.querySelector('.card');
  125. card.style.border = '2px solid #666666';
  126. card.style.boxShadow = '4px 4px 0 #666666';
  127. }
  128. }).then(canvas => {
  129. const link = document.createElement('a');
  130. link.download = 'xiaohongshu_card.png';
  131. link.href = canvas.toDataURL('image/png');
  132. link.click();
  133. });
  134. }
  135. // 监听编辑事件,保持样式一致性
  136. document.querySelectorAll('.editable').forEach(element => {
  137. element.addEventListener('input', function() {
  138. this.style.backgroundColor = '#000000';
  139. });
  140. });
  141. </script>
  142. </body>
  143. </html>