仅在溢出时在悬停时选取文字

时间:2019-01-15 05:14:08

标签: javascript html css hover marquee

如果文本不适合其父组件,我想将文本悬停在悬停上,否则就不应悬停。

当用户将鼠标悬停在文本上时,我可以将其选框或滚动,但我想设置溢出条件。

这是我的示例代码:

if mywriteform.is_valid():
        confirmform = mywriteform.save(commit=False)
        confirmform.save()
        mywriteform.save_m2m()
        return redirect('MyDetail', pk=confirmform.pk)

我也已将其上传到小提琴:https://jsfiddle.net/ydt46n1u/4/

在上面的示例中,如何停止第二格的字幕?

3 个答案:

答案 0 :(得分:0)

min-width:200px添加到.labelContainer:hover span

widthauto时,跨度小于容器的问题。

不再。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为这是更好的版本,希望它有用

$(".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>