jQuery horizontal scrolling of text on mouseover

2019-05-07 10:42发布

I would really appreciate some help based on the following image:

enter image description here

Furthermore, I am trying to accomplish (infinite) scrolling of the green text within div.title-holder. My aim is for the scrolling to begin only on mouseOver and then reset on mouseOut of div.container. I thought this would have been a simple task with a bit of CSS and jQuery, but apparently not so. Here is my HTML, CSS and accompanying JS:

<div class="container">
    <div class="title-holder">
        <a href="somelink.html">long text that needs to scroll</a>
    </div>
    <img src="someimage.jpg" />
</div>

Relevant CSS:

div.title-holder {
  width:130px;
  height:20px;
  overflow:hidden;
  white-space:no-wrap;
  text-align:center;
}
div.title-holder a {      
}

jQuery JS:

$('div.container').hover(
    function () {
        var scrollingWidth = $(this).find('div.title-holder').find('a').width();
        $(this).find('div.title-holder').stop(true, true).animate({scrollLeft: scrollingWidth}, { duration: 5000, easing: 'swing' });
    },
    function () {
        $(this).find('div.title-holder').stop(true, true).animate({scrollLeft: 0}, { duration: 5000, easing: 'swing' });
    }
);

Now, this works okay, except for 2 problems:

  1. It does not scroll infinitely, and
  2. The scroll action is very very jumpy/jittery

I really do hope that someone has had a similar requirement in the past and can shed some light on this matter for me. Thank you!

4条回答
ゆ 、 Hurt°
2楼-- · 2019-05-07 10:42

There's a problem with your CSS! It's veery simple to make it work. Just adding one line of CSS:

white-space:nowrap;

Here it is, the complete code: http://jsfiddle.net/sMmkX/115/

查看更多
干净又极端
3楼-- · 2019-05-07 10:50

Don't do this! Scrolling text and doling information out like that is a huge, usability problem. Scrolling text is bad!!!

Use that and you will annoy 80% of the page's victims users, and most will not read/see, or will ignore, the title as well.

Write titles with the First 2 Words in mind, and display them like this if they absolutely must be too long. :

A better pattern

... where the "more" link pops up a div with the full title.

查看更多
Animai°情兽
4楼-- · 2019-05-07 10:59

I prefer using setInterval. Maybe this fiddle will help.

查看更多
放我归山
5楼-- · 2019-05-07 11:08

Here you go

Working demo

Hope this helps you.

查看更多
登录 后发表回答