对原生textarea加上:当前输入字数/最大输入字数

源码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Textarea Character Counter with Draggable Resizing</title>
<style>
  .container {
    position: relative;
    width: 100%;
    max-width: 280px; /* Adjust as needed */
  }
  #inputText {
    width: 100%;
    min-width: 280px; /* Minimum width to prevent shrinking */
    height: 100px; /* Adjust initial height as needed */
    min-height: 100px; /* Minimum height to prevent shrinking */
    box-sizing: border-box;
    padding: 10px;
    font-size: 14px; /* Adjust font size as needed */
    overflow: auto; /* Handle text overflow */
  }
  .counter {
    position: absolute;
    bottom: 10px;
    right: 10px;
    color: black;
    font-size: 12px; /* Adjust font size as needed */
  }
  .counter.max {
    color: red;
  }
</style>
</head>
<body>
<div class="container">
  <textarea id="inputText" rows="6" maxlength="150"></textarea>
  <div id="charCount" class="counter">0/150</div>
</div>

<script>
  const textArea = document.getElementById('inputText');
  const charCount = document.getElementById('charCount');
  const minWidth = 280; // Minimum width for the textarea

  textArea.addEventListener('input', function() {
    const textLength = textArea.value.length;
    charCount.textContent = `${textLength}/150`;

    if (textLength >= 150) {
      charCount.classList.add('max');
    } else {
      charCount.classList.remove('max');
    }
  });

  // Function to handle mousemove event for resizing
  function handleResize(event) {
    const mouseX = event.clientX;
    const textareaRect = textArea.getBoundingClientRect();
    const containerRect = textArea.parentNode.getBoundingClientRect();

    let newWidth = mouseX - textareaRect.left;

    // Ensure new width respects minimum width
    if (newWidth < minWidth) {
      newWidth = minWidth;
    }

    // Set new width to textarea
    textArea.style.width = newWidth + 'px';

    // Adjust counter position based on textarea width change
    charCount.style.right = `${containerRect.right - textareaRect.right + 10}px`;
  }

  // Listen for mousedown event on textarea for starting resize
  textArea.addEventListener('mousedown', function(event) {
    if (event.offsetX > textArea.offsetWidth - 10) {
      // Only start resize if mouse is near the right edge
      document.addEventListener('mousemove', handleResize);
    }
  });

  // Stop resizing on mouseup event
  document.addEventListener('mouseup', function() {
    document.removeEventListener('mousemove', handleResize);
  });

  // Initial check on load
  window.addEventListener('load', function() {
    const currentWidth = textArea.clientWidth;
    if (currentWidth < minWidth) {
      textArea.style.width = minWidth + 'px';
    }
  });
</script>
</body>
</html>

运行效果:

相关推荐
问心无愧05131 天前
ctf show web入门31
前端·笔记
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_31:(AbortSignal 深入解析与高级中止模式)
前端·ui·html·音视频·视频编解码
UXbot1 天前
2026年文字转原型AI工具推荐:输入一句需求描述,自动生成多页面可交互界面
前端·低代码·ui·交互·ai编程·原型模式
im_AMBER1 天前
Browser Agent 开发:从浏览器插件到Electron CDP
前端·javascript·架构·electron·agent
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_30:(AbortController 实现可取消的异步请求)
前端·ui·html·edge浏览器·媒体
前端若水1 天前
选择器的威力 —— :has()、@layer、原生嵌套
前端·css·css3
nashane1 天前
HarmonyOS 6学习:Web组件本地资源跨域访问全解析与实战
前端·学习·harmonyos·harmonyos 5
小陈同学,,1 天前
地图第一次进来慢的问题二
前端
阿赛工作室1 天前
基于Vue3和TensorFlow.js的数字图像识别应用HTML单文件
javascript·html·tensorflow
万少1 天前
公测期 0 元/月!商汤 SenseNova 免费 Token 再不领就没了
前端·javascript·后端