带有溢出的 div 的 jquery marquee-like 动画 :hidden and nowrap?

标签 jquery animation scroll overflow

我有一个用于歌曲标题的 block 元素(具有最大宽度、nowrap 和溢出:隐藏的绝对定位 h1),需要限制为 650 像素宽。如果 H1 的宽度是 650px,我需要启动一个动画,以类似乒乓球的方式水平、前后滚动 div。

我该如何制作滚动动画?

最佳答案

我知道这不是解决这个问题的最有效或最好的方法,但我最终创建了两个 jquery 函数来完成任务:

$.fn.pingpongscroll = function () {
    var delay = 30;
    $(this).wrapInner('<span>');
    var contentWidth = $(this).children('span').width();
    var boxWidth = $(this).width();

    if (contentWidth > boxWidth) {
        var startIndent = parseInt($(this).css('text-indent'));
        var currIndent  = startIndent;
        var left = true;
        $(this).pingpongscrollstep(contentWidth, startIndent, currIndent, left, delay);
    }
};
$.fn.pingpongscrollstep = function (contentWidth, startIndent, currIndent, left, delay) {
    if($(this).length != 0) {
        thisdelay = delay;
        if(left) {
            if(contentWidth + currIndent > $(this).width()) {
                currIndent = currIndent - 1;
                $(this).css('text-indent', currIndent);
            } else {
                left = false;
                thisdelay = thisdelay*20;
            }
        } else {
            if(currIndent < startIndent) {
                currIndent = currIndent + 1;
                $(this).css('text-indent', currIndent);
            } else {
                left = true;
                thisdelay = thisdelay*30;
            }
        }
        var thiselement = this;
        setTimeout(function(){
            $(thiselement).pingpongscrollstep(contentWidth, startIndent, currIndent, left, delay);
        }, thisdelay);
    }
};

虽然这些效果很好,但我确信这不是处理滚动的正常方法。另外,我不知道如何使第二个函数成为第一个函数的私有(private)成员,因此站点无法调用它......有人知道如何做到这一点吗?

关于带有溢出的 div 的 jquery marquee-like 动画 :hidden and nowrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976611/

相关文章:

javascript - 使用 jQuery 的 attr() 编写 if 语句

html - 当宽度/高度太大时背景图像/元素消失

android - 没有AnimationDrawable可以做逐帧动画吗?

jquery - 控制页面滚动时div的滚动速度

jquery - 如何为 yui 和 jQuery 数据表维护相同的 css 样式

javascript - 溢出 :scroll doesn't work when I add content to div

javascript - JavaScript函数来处理无法在$(window).resize()上使用的CSS自定义属性

Javascript 在本地工作,但不在 Github Pages 网站上工作

html - Chrome 滚动到 anchor 标签上的错误位置

WPF ScrollViewer 与 ListBox