Javascript Marquee 文本被剪裁为大字体

标签 javascript jquery html css

我使用来自

的建议示例

Very Simple, Very Smooth, JavaScript Marquee

     (function($) {
    $.fn.textWidth = function(){
         var calc = '<span style="display:none">' + $(this).text() + '</span>';
         $('body').append(calc);
         var width = $('body').find('span:last').width();
         $('body').find('span:last').remove();
        return width;
    };

    $.fn.marquee = function(args) {
        var that = $(this);
        var textWidth = that.textWidth(),
            offset = that.width(),
            width = offset,
            css = {
                'text-indent' : that.css('text-indent'),
                'overflow' : that.css('overflow'),
                'white-space' : that.css('white-space')
            },
            marqueeCss = {
                'text-indent' : width,
                'overflow' : 'hidden',
                'white-space' : 'nowrap'
            },
            args = $.extend(true, { count: -1, speed: 1e1, leftToRight: false }, args),
            i = 0,
            stop = textWidth*-1,
            dfd = $.Deferred();

        function go() {
            if(!that.length) return dfd.reject();
            if(width == stop) {
                i++;
                if(i == args.count) {
                    that.css(css);
                    return dfd.resolve();
                }
                if(args.leftToRight) {
                    width = textWidth*-1;
                } else {
                    width = offset;
                }
            }
            that.css('text-indent', width + 'px');
            if(args.leftToRight) {
                width++;
            } else {
                width--;
            }
            setTimeout(go, args.speed);
        };
        if(args.leftToRight) {
            width = textWidth*-1;
            width++;
            stop = offset;
        } else {
            width--;            
        }
        that.css(marqueeCss);
        go();
        return dfd.promise();
    };
            $('h1').marquee();
})(jQuery);

但是当我将字体大小增加到 100 像素时,文本会被剪裁。看 http://jsfiddle.net/plunje/xYdBj/202/

有什么解决方法的建议吗?

最佳答案

您未获得预期结果的原因是您的计算函数未返回您预期的结果。

这是因为:
创建的跨度正在环绕。要解决此问题,请添加 css 'white-space: nowrap'。还因为与 H1 相比,span 具有不同的字体大小(您抓取 H1 文本并将其放入 span,这不随 H1 字体大小一起提供)。

fiddle :http://jsfiddle.net/xYdBj/210/

这是更新后的 textWidth 函数:

$.fn.textWidth = function(){
            var calc = '<span style="display:none; white-space: nowrap; font-size: '+$(this).css('font-size')+'">' + $(this).text() + '</span>';
             $('body').append(calc);
             var width = $('body').find('span:last').width();
             $('body').find('span:last').remove();
            return width;
        };

关于Javascript Marquee 文本被剪裁为大字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22536231/

相关文章:

jquery - 有人知道为什么我的使用 jquery 的网页在 IE 中格式不正确吗?

javascript - jQuery直接在onclick =""处并且效果UI慢

html - 垂直对齐多行文本

JavaScript 枚举

javascript - 具有多种目标类型的加载器服务

javascript - 无法使用 Express 和 MongoDB/Mongoose 设置 Passport - 示例代码?

html - CSS Transition - 获取返回动画,不同于 start css 属性

javascript - Cheerio.js 没有抓取非内联样式?

javascript - 在 meteor 中,如何创建具有相同类名的动态ID

javascript - 如何在js/jquery中获取html标签的内部html?