Marquee text on hover only when overflow

Viewed 1919

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?

3 Answers

.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>

Add min-width:200px to .labelContainer:hover span

The issue that when the width is auto the span becomes smaller then the container.

Not anymore.

this is better version i suppose,hope it is useful

$(".labelContainer span").each(function() {
         var lengthVal=($(this).text().length);
          if(lengthVal>26){
          $(".labelContainer span").addClass("makeit");
          }
     });
.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;
}

span.makeit:hover {
    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 Should Noy</span>
</div>
<div class="labelContainer">
  <span>The long text should scroll when user hovers on it.</span>
</div>
<div class="labelContainer">
  <span>The  scroll when user hovers on it.</span>
</div>

Related