如何在鼠标悬停时将div内的文本滚动到左侧(How to scroll text within a div to left when hovering the div)

编程入门 行业动态 更新时间:2024-10-12 05:46:09
如何在鼠标悬停时将div内的文本滚动到左侧(How to scroll text within a div to left when hovering the div)

我有一个不适合div标签的文本, .textBox 。 所以我想创建一个函数,在悬停父div的情况下,文本开始从左向右滚动。 因此用户可以阅读完整内容。 当它没有悬停时,它会退回到默认位置。

我不想使用选框。 我想保持结构不变,并使用CSS或JavaScript来解决它。

这是一个没有函数的例子:

.textBox {
  width: 200px;
  height: 30px;
  border: 1px solid #000;
  overflow: hidden;
  line-height: 30px;
  padding: 5px;
} 
  
<div class="textBox">Some Content here and some more here</div> 
  
 

知道怎么做吗?

I have a text that does not fit in a div tag, .textBox. So I want to make a function that in case of hovering the parent div, the text starts scrolling from left to right. So the user can read the full content. And when it is not hovering it fades back to the default position.

I do not want to use marquee. I want do keep the structure as it is and use CSS, or JavaScript to solve it.

Here is an example without the function:

.textBox {
  width: 200px;
  height: 30px;
  border: 1px solid #000;
  overflow: hidden;
  line-height: 30px;
  padding: 5px;
} 
  
<div class="textBox">Some Content here and some more here</div> 
  
 

Any idea how to do this?

最满意答案

看到这个纯CSS解决方案,添加了一个span标签以使其成为可能。

关键概念是:使用框的宽度 - 跨度宽度值将span标记向左移动。 换句话说,它使其在悬停时滚动到文本的末尾。

的jsfiddle

.textBox {
  width: 200px;
  height: 30px;
  border: 1px solid #000;
  overflow: hidden;
  line-height: 30px;
  padding: 5px;
  position: relative;
}
.textBox span {
  position: absolute;
  white-space: nowrap;
  transform: translateX(0);
  transition: 1s;
}
.textBox:hover span {
  transform: translateX(calc(200px - 100%));
} 
  
<div class="textBox"><span>The quick brown fox jumps over the lazy dog</span></div> 
  
 

See this pure CSS solution, added a span tag to make it possible.

The key concept is: move the span tag to the left with the value of box's width - span's width. In the other word that makes it to scroll to the end of the text on hover.

jsfiddle

.textBox {
  width: 200px;
  height: 30px;
  border: 1px solid #000;
  overflow: hidden;
  line-height: 30px;
  padding: 5px;
  position: relative;
}
.textBox span {
  position: absolute;
  white-space: nowrap;
  transform: translateX(0);
  transition: 1s;
}
.textBox:hover span {
  transform: translateX(calc(200px - 100%));
} 
  
<div class="textBox"><span>The quick brown fox jumps over the lazy dog</span></div> 
  
 

更多推荐

本文发布于:2023-08-08 00:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   时将   文本   如何在   left

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!