js判断元素内文字是否超出元素宽度,溢出隐藏

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>文字隐藏</title>
</head>
<style>
  .content-box {
    width: 100px;
    border: 5px solid red;
    padding: 10px;
    box-sizing: content-box;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .border-box {
    width: 100px;
    border: 5px solid red;
    padding: 10px;
    box-sizing: border-box;
    text-overflow: ellipsis;
    overflow: hidden;
    margin-bottom: 10px;
  }
</style>

<body>
  borderBox
  <div class="border-box">
    99999991
  </div>
  contentBox
  <div class="content-box">
    999999999991
  </div>
  <script>
    // 对于borderBox 只要scrollWidth > clientWidth 那么就代表hidden了
    const borderBox = document.querySelector('.border-box')
    const contentBox = document.querySelector('.content-box')
    console.dir(borderBox)
    console.log('borderBox', borderBox.scrollWidth, borderBox.clientWidth) // zdz-log
    console.dir(contentBox)
    console.log('contentBox',contentBox.scrollWidth,contentBox.clientWidth); // zdz-log



  </script>
</body>

</html>

posted @ 2024-04-15 10:59  story.Write(z)  阅读(380)  评论(0编辑  收藏  举报