CSS/JQuery 支持悬停时横向滚动文本
我在我运行的网站上有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最后,这是我的条目: http://jsfiddle.net/sdleihssirhc/AYYQe/3/
很酷的东西:
text-overflow:ellipsis
而不是添加任何DOM元素At last, here's my entry: http://jsfiddle.net/sdleihssirhc/AYYQe/3/
Cool stuff:
text-overflow:ellipsis
instead of adding any DOM elements这是一个相当简单的方法。首先,设置一个包含长文本的 div:
您可以使用一些 css 来自动处理截断和省略号:
如果您随后确定内容的本机未截断宽度,则可以使用 jQuery 的
.animate() 以一致的速度移动该内容——即使您直到运行时才知道文本的长度(就像 Twitter feed 的情况一样)。下面是一种获取测量结果的机械方法:
最后,一些动画:
无论文本长度如何,您都会获得每 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:
You can use some css to automatically handle the truncation and 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:Finally, some animation:
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/
我在 jsfiddle 或最后的解决方案,它使用 CSS3 动画。我借用了此处和这里。我的想法是让容器
div
(即div.s
)增长到足够宽,以便它可以包含所有文本,从而可以使用百分比来表示
属性,因此:@keyframes
规则中的 >left这里是代码:
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 theleft
property in the@keyframes
rule, hence the:Here is the code:
有一些插件可以执行此操作(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
Steffen Rusitschka 为此编写了一个 jQuery 脚本 RUZEE.Ellipsis。
Steffen Rusitschka has written a jQuery script, RUZEE.Ellipsis, for doing this.
您可以查看 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.