javascripthtmlcsshovermarquee

Marquee text on hover only when overflow


I want to marquee a text on hover if it doesn't fit in to its parent component else it should not hover.

I 'm able to marquee or scroll the text when user hovers on it, but i want to put condition of overflow.

This is my sample code :

<div class="labelContainer">
  <span>The long text should scroll when user hovers on it.</span>
</div>

<div class="labelContainer">
  <span>Should Not Scroll</span>
</div>

.labelContainer {
  height: 30px;
  border: 1px solid #000;
  border-radius: 3px;
  border-radius: 3px;
  display: flex;
  width: 200px;
  overflow: hidden;
  position: relative;
  padding : 5px;
}

.labelContainer span {
  height: 30px;
  width: 200px;
  color: #000;
  text-overflow: ellipsis;
  display: block;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  transform: translateX(0);
  transition: 1s;
}

.labelContainer:hover span {
  width: auto;
  transform: translateX(calc(200px - 100%));
}

I have also uploaded it on fiddle : https://jsfiddle.net/ydt46n1u/4/

In the above example how can i stop marquee for second div?


Solution

  • .labelContainer {
      height: 30px;
      border: 1px solid #000;
      border-radius: 3px;
      border-radius: 3px;
      display: flex;
      width: 200px;
      overflow: hidden;
      position: relative;
      padding : 5px;
    }
    
    .labelContainer span {
      height: 30px;
      width: 200px;
      color: #000;
      text-overflow: ellipsis;
      display: block;
      overflow: hidden;
      position: absolute;
      white-space: nowrap;
      transform: translateX(0);
      transition: 1s;
    }
    
    .labelContainer:first-of-type:hover span {
      width: auto;
      transform: translateX(calc(200px - 100%));
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="labelContainer">
      <span>The long text should scroll when user hovers on it.</span>
    </div>
    
    <div class="labelContainer">
      <span>Should Not Scroll</span>
    </div>