jQuery 鼠标悬停时水平滚动文本

发布于 2024-12-03 05:34:52 字数 1351 浏览 1 评论 0 原文

我非常感谢基于以下图像的一些帮助:

在此处输入图像描述

此外,我正在尝试完成(无限)滚动 div.title-holder 中的绿色文本。我的目标是仅在 mouseOver 上开始滚动,然后在 div.containermouseOut 上重置。我以为只要使用一点 CSS 和 jQuery,这就是一个简单的任务,但显然并非如此。这是我的 HTML、CSS 和随附的 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>

相关 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' });
    }
);

现在,这可以正常工作,除了两个问题:

  1. 它不会无限滚动,并且
  2. 滚动操作非常非常跳跃/抖动

我真的希望有人过去有过类似的要求,并且能为我解答这个问题。 谢谢!

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!

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

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

发布评论

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

评论(4

葬花如无物 2024-12-10 05:34:52

我更喜欢使用 setInterval。也许这个 fiddle 会有所帮助。

I prefer using setInterval. Maybe this fiddle will help.

时光病人 2024-12-10 05:34:52

不要这样做!像这样滚动文本并分发信息是一个巨大的可用性 问题
滚动文本不好!!!

使用它,你会惹恼页面 80% 的受害者用户,大多数人也不会阅读/查看或忽略标题。

编写标题时牢记 前 2 个单词,如果它们绝对有效,则像这样显示它们一定是太长了。 :

...“更多”链接会弹出一个带有完整标题的div。

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.

ζ澈沫 2024-12-10 05:34:52

开始

工作演示

希望这对您有帮助。

Here you go

Working demo

Hope this helps you.

千仐 2024-12-10 05:34:52

你的 CSS 有问题!让它发挥作用非常简单。只需添加一行 CSS:

white-space:nowrap;

这是完整的代码: http://jsfiddle.net/sMmkX/115 /

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/

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