Skip to content

JavaScript:Example:DetectOverflow

ResizeObserver와 Vue를 사용하여 텍스트가 Overflow 되는지 확인하는 예제 코드.

HTML

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<div id="app">
  <h3>Please resize the text box below!</h3>
  <div ref="textBox" 
       class="text-box">
    <div ref="textBoxInner" 
         class="text-box__inner">
      <div ref="text" 
           class="text-box__text">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </div>
    </div>
    <div v-show="isOverflow"
         v-cloak
         class="text-box__overflow-ellipsis"
         @click="unfoldDown">
      <i class="material-icons">more_horiz</i>
    </div>
  </div>
</div>

SCSS

body {
  padding: 30px;
}
[v-cloak] {
  display: none;
}

h3 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 6px;
}
.text-box {
  width: 550px;
  height: 280px;
  padding: 20px;
  border: 4px solid;
  box-sizing: border-box;
  position: relative;
  &::after {
    content: "";
    display: block;
    position: absolute;
    bottom: 0;
    right: 0;
    width: 0;
    height: 0;
    border-width: 7px;
    border-style: solid;
    border-top-color: transparent;
    border-left-color: transparent;
  }
  &__inner {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  &__text {
    font-size: 20px;
  }
  &__overflow-ellipsis {
    width: 40px;
    height: 16px;
    border: 4px solid;
    border-radius: 4px;
    background: white;
    position: absolute;
    bottom: -14px;
    left: 0;
    right: 0;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    i {
      font-size: 30px;
      animation: twinkle 1s infinite alternate;
    }
  }
}

@keyframes twinkle {
  0% {
    opaicty: 1;
  }
  100% {
    opacity: .2;
  }
}

JavaScript

// In general, you need polyfill,
// The recommended module is 'https://github.com/juggle/resize-observer'.

// import ResizeObserver from '@juggle/resize-observer'

new Vue({
  el: '#app',
  data () {
    return {
      isOverflow: false
    }
  },
  mounted () {
    this.initInteractTextBox()
    this.observeTextBoxSize()
  },
  methods: {
    observeTextBoxSize () {
      const ro = new ResizeObserver(entries => {
        const { width, height } = entries[0].contentRect

        if (
          // 일반적인 경우.
          (entries[1] && height < entries[1].contentRect.height)
          // $refs.textBox의 높이만 변경될 경우 $refs.text의 크기는 변하지 않기 때문에 entries[1] is undefined,
          // getBoundingClientRect의 Reflow를 최소화.
          || (height < this.$refs.text.getBoundingClientRect().height)
        ) {
          this.isOverflow = true
        } else {
          this.isOverflow = false
        }
      })

      ro.observe(this.$refs.textBoxInner)
      ro.observe(this.$refs.text)
    },
    initInteractTextBox () {
      // https://interactjs.io/docs/
      interact(this.$refs.textBox)
        .resizable({
          edges: {
            right: true,
            bottom: true
          }
        })
        .on('resizemove', event => {
          event.target.style.width = `${event.rect.width}px`
          event.target.style.height = `${event.rect.height}px`
        })
    },
    unfoldDown () {
      this.$refs.textBox.style.height = `${this.$refs.text.getBoundingClientRect().height + 48}px` // 48 is $refs.textBox의 Border-box(padding + border) 크기.
    }
  }
})

See also

Favorite site