CSS/JQuery 支持悬停时横向滚动文本

发布于 2024-10-13 19:43:12 字数 228 浏览 1 评论 0原文

我在我运行的网站上有一个 Twitter feed。然而,它在小屏幕上会被切断。我有一个足够高的栏,可以容纳 1 行文本,其中有最新的推文。如果推文太长,我希望推文在最后省略或淡出。但在悬停时,我希望文本缓慢向左滑动,从而暴露隐藏的部分。

当您突出显示标题比屏幕宽的歌曲时,在 iPod classic 上会使用此效果。 (我希望你明白我的目的。)

我只是好奇我将如何实施这样的事情?如何强制文本保持在一行上?

I have a twitter feed on a website I run. However, it gets cut off on small screens. I have a bar tall enough for 1 line of text in which I have the latest tweet. I want the tweet to ellipsize or fade out at the end if it is too long. But on hover, I want the text to slide slowly to the left, thus exposing the hidden part.

This effect is used on the iPod classic when you highlight a song that has a title that is wider than the screen. (I hope you understand what I'm going for.)

I'm just curious how I would go about implementing something like this? How can I force the text to stay on one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

も让我眼熟你 2024-10-20 19:43:12

最后,这是我的条目: http://jsfiddle.net/sdleihssirhc/AYYQe/3/

很酷的东西:

  1. 图书馆不可知
  2. 使用scrollLeft而不是绝对定位,因此更流畅、更快
  3. 使用text-overflow:ellipsis而不是添加任何DOM元素

At last, here's my entry: http://jsfiddle.net/sdleihssirhc/AYYQe/3/

Cool stuff:

  1. Library agnostic
  2. Uses scrollLeft instead of absolute positioning, so it's smoother and faster
  3. Uses text-overflow:ellipsis instead of adding any DOM elements
ζ澈沫 2024-10-20 19:43:12

这是一个相当简单的方法。首先,设置一个包含长文本的 div:

<div id="container">
Here is the long content, long long content. So long. Too long.
</div>

您可以使用一些 css 来自动处理截断和省略号:

div#container {
    /* among other settings: see fiddle */
    width: 200px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

如果您随后确定内容的本机未截断宽度,则可以使用 jQuery 的 .animate() 以一致的速度移动该内容——即使您直到运行时才知道文本的长度(就像 Twitter feed 的情况一样)。下面是一种获取测量结果的机械方法:

var true_width = ( function(){
  var $tempobj = $('#container') // starting with truncated text div container
      .clone().contents() // duplicate the text
      .wrap('<div id="content"/>') // wrap it in a container
      .parent().appendTo('body') // add this to the dom
      .css('left','-1000px'); // but put it far off-screen
  var result = $tempobj.width(); // measure it
  $tempobj.remove(); // clean up
  return result;
})();

最后,一些动画:

$('#container').one('mouseenter', function(){ // perhaps trigger once only
  var shift_distance = true_width - $(this).width(); // how far to move
  var time_normalized = parseInt(shift_distance / 100, 10) * 1000; // speed
  $(this).contents().wrap('<div id="content">').parent() // wrap in div
    .animate({
        left: -shift_distance,
        right: 0
    }, time_normalized, 'linear'); // and move the div within its "viewport"
});

无论文本长度如何,您都会获得每 100 像素约一秒的一致速度(根据需要进行调整)。重置或倒带 mouseleave 上的内容作为练习。这种方法有一些幼稚的地方,但可能会给你一些想法。

这是一个工作示例: http://jsfiddle.net/redler/zdvyj/

Here's a fairly simple way to do it. First, set up a div containing your long text:

<div id="container">
Here is the long content, long long content. So long. Too long.
</div>

You can use some css to automatically handle the truncation and ellipsis:

div#container {
    /* among other settings: see fiddle */
    width: 200px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

If you then determine the native, untruncated width of the content, you can use jQuery's .animate() to move that content at a consistent speed -- even if you don't know the length of the text until runtime (as would be the case with a twitter feed). Here's a somewhat mechanical way of getting the measurement:

var true_width = ( function(){
  var $tempobj = $('#container') // starting with truncated text div container
      .clone().contents() // duplicate the text
      .wrap('<div id="content"/>') // wrap it in a container
      .parent().appendTo('body') // add this to the dom
      .css('left','-1000px'); // but put it far off-screen
  var result = $tempobj.width(); // measure it
  $tempobj.remove(); // clean up
  return result;
})();

Finally, some animation:

$('#container').one('mouseenter', function(){ // perhaps trigger once only
  var shift_distance = true_width - $(this).width(); // how far to move
  var time_normalized = parseInt(shift_distance / 100, 10) * 1000; // speed
  $(this).contents().wrap('<div id="content">').parent() // wrap in div
    .animate({
        left: -shift_distance,
        right: 0
    }, time_normalized, 'linear'); // and move the div within its "viewport"
});

Regardless of the length of the text, you'll get a consistent speed of about one second per 100 pixels (adjust as desired). Resetting or rewinding content on mouseleave is left as an exercise. This approach has some naïve bits, but may give you some ideas.

Here's a working example: http://jsfiddle.net/redler/zdvyj/

北风几吹夏 2024-10-20 19:43:12

我在 jsfiddle 或最后的解决方案,它使用 CSS3 动画。我借用了此处这里。我的想法是让容器 div(即 div.s)增长到足够宽,以便它可以包含所有文本,从而可以使用百分比来表示 @keyframes 规则中的 >left 属性,因此:

.s {
   display: inline-block;
}
.t:hover, .s:hover {
  width: auto;
}

这里是代码:

.t {
    white-space: nowrap;
    position: relative;
    overflow: hidden;   
    text-overflow: ellipsis;
    display: block;
    width: 100%;
}
.s {
  display: inline-block;
  width: 100%;
}
.t:hover, .s:hover {
  width: auto;
}
.s:hover .t { 
  animation: scroll 15s;
}
.f {
  width: 100px;
  background: red;
  overflow: hidden;
}
@keyframes scroll {
    0% {left: 0px;}
    100% {left: -100%;}                   
}
<div class="f">
  <div class="s">
    <div class="t">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id impedit accusamus nulla facilis unde sed ipsum maiores adipisci, eius excepturi saepe perspiciatis sequi optio ipsam odio quibusdam quo libero repudiandae.
    </div>
  </div>
</div>

My solution on jsfiddle or here at the end, it uses CSS3 Animations. I borrowed ideas from here and here. My idea was to let the container div, i.e. div.s to grow wide enough so that it can contain all the text, thus enabling the use of percentages for the left property in the @keyframes rule, hence the:

.s {
   display: inline-block;
}
.t:hover, .s:hover {
  width: auto;
}

Here is the code:

.t {
    white-space: nowrap;
    position: relative;
    overflow: hidden;   
    text-overflow: ellipsis;
    display: block;
    width: 100%;
}
.s {
  display: inline-block;
  width: 100%;
}
.t:hover, .s:hover {
  width: auto;
}
.s:hover .t { 
  animation: scroll 15s;
}
.f {
  width: 100px;
  background: red;
  overflow: hidden;
}
@keyframes scroll {
    0% {left: 0px;}
    100% {left: -100%;}                   
}
<div class="f">
  <div class="s">
    <div class="t">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id impedit accusamus nulla facilis unde sed ipsum maiores adipisci, eius excepturi saepe perspiciatis sequi optio ipsam odio quibusdam quo libero repudiandae.
    </div>
  </div>
</div>

面如桃花 2024-10-20 19:43:12

有一些插件可以执行此操作(Remy Sharp:http://remysharp.com/demo/marquee .html 例如),但如果您是从头开始构建的:

正在滚动的项目需要有“white-space: nowrap;” (将其保持在一行上),“position:absolute”(以允许其左右滚动)并包装在具有“overflow:hidden”的相对定位元素中(以使其看起来只显示您想要显示的宽度) 。

使用 jQuery,您可以使用 .animate() 在悬停事件上将滚动项从左向右移动

There are a few plugins out there that do this (Remy Sharp: http://remysharp.com/demo/marquee.html for example), but if you were building from scratch:

The item being scrolled needs to have "white-space: nowrap;" (to keep it on one line), "position:absolute" (to allow it scroll left and right) and wrapped in a relatively positioned element that has "overflow:hidden" (to make it appear like only the width you want shows).

Using jQuery, you could use .animate() to move the scroll item from left to right on the hover event

温暖的光 2024-10-20 19:43:12

Steffen Rusitschka 为此编写了一个 jQuery 脚本 RUZEE.Ellipsis

Steffen Rusitschka has written a jQuery script, RUZEE.Ellipsis, for doing this.

猥︴琐丶欲为 2024-10-20 19:43:12

您可以查看 jRollingNews
使用代码生成器自定义条形图并预览结果。

免责声明:我做到了。

You can give a look to jRollingNews
Use the code generator to customize the bars and to preview the result.

Disclaimer: I made it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文