0

I use the suggested expample from

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);

But when I increase the font size up to 100px, the text gets clipped. See http://jsfiddle.net/plunje/xYdBj/202/

Any suggestions how to fix?

Community
  • 1
  • 1
user1119308
  • 79
  • 2
  • 6

2 Answers2

2

The reason you are not getting the expected results is because your calculate function is not returning what you expect.

This is because:
The span that is created is wrapping. to fix this add the css 'white-space: nowrap'. And also because the span has a different font-size compared to the H1 (you grab the H1 text and put it into the span, this does not come with the H1 font-size).

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

Here is an updated textWidth function:

$.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;
        };
2pha
  • 9,798
  • 2
  • 29
  • 43
1

With

var width = $('body').find('span:last').width();

you getting the the current document size but your text is larger. I put the text in a div and get the scroll width as width http://jsfiddle.net/xYdBj/211/

var width = $('div')[0].scrollWidth;

my example has increased movespeed ... maybe if(width == stop) will never be true but its just an example.

Ra Mon
  • 127
  • 3