1

Firstly I have a html marque:

<marquee behavior="scroll" direction="left" scrollamount="1">  

But it wasn't smooth, and was too slow.
The higher scrollamount - the lower the smoothness.

Then i tried with css

marquee{
  width: 200px; height: 50px; white-space: nowrap;
  overflow: hidden;
  overflow-x:-webkit-marquee;
  -webkit-marquee-direction: forwards;
  -webkit-marquee-style: scroll;
  -webkit-marquee-speed: normal; //I changed this to `slow` - without effect
  -webkit-marquee-increment: small;
  .-webkit-marquee-repetition: 5;
  overflow-x: marquee-line;
  marquee-direction: forward;
  marquee-style: scroll;
  marquee-speed: normal;  //I changed this to `slow` - without effect
}

I suppose, if I could change the speed to slow the above marque will be smooth.
So, I want a simple, smooth and speed-adjustable marque.

4

1 回答 1

3
 (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.css('overflow')!="hidden") { 
                    that.css('text-indent', width + 'px'); 
                    return false;
                }
                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();
$("h1").marquee();
    $("h1").mouseover(function () {     
        $(this).removeAttr("style");
   }).mouseout(function () {
        $(this).marquee();
   });
 })(jQuery);

​演示

于 2012-12-10T09:21:50.750 回答