Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>从右至左</h2>
<div class="marquee-wrap" type="right-to-left">
  <ul class="marquee-content">
    <li><a href="#">别什么都往圈子扯,换谁写那么几篇破文章都会被怼。</a></li>
    <li><a href="#">Lorem ipsum dolor.</a></li>
    <li>我没有朋友,最好的朋友就是我的手机</li>
    <li>唉,不光是 angular,这么下去整个前端界的名声都要被这人搞臭了</li>
  </ul>
</div>

<h2>从左至右</h2>
<div class="marquee-wrap" type="left-to-right">
  <ul class="marquee-content">
    <li>唉,不光是 angular,这么下去整个前端界的名声都要被这人搞臭</li>
  </ul>
</div>

<h2>从下至上</h2>
<div class="marquee-wrap" type="bottom-to-top">
  <ul class="marquee-content">
    <li><a href="#">别什么都往圈子扯,换谁写那么几篇破文章都会被怼。ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd</a></li>
    <li><a href="#">Lorem ipsum dolor.</a></li>
    <li>我没有朋友,最好的朋友就是我的手机</li>
    <li>唉,不光是 angular,这么下去整个前端界的名声都要被这人搞臭了... ​​​​</li>
  </ul>
</div>

<h2>禁止不动</h2>
<div class="marquee-wrap" type="static">
  <ul class="marquee-content">
    <li><a href="#">挺纳闷的是,为什么一篇明显有不少偏见的文章,能在微博、知乎引起如此大的传播</a></li>
  </ul>
</div>
              
            
!

CSS

              
                  .marquee-wrap{
    position:relative;
    overflow: hidden;
    width: 300px;
    border: 5px solid red;
    margin-bottom: 20px;
  }
  .marquee-wrap .marquee-content{
    position: relative;
    margin: 0;
    padding: 0;
  }
  .marquee-wrap ul.right-to-left li, .marquee-wrap ul.left-to-right li{
    float: left;
    list-style: none;
    padding-right: 20px;
    white-space: nowrap;
  }

  .marquee-wrap ul.static li{
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .marquee-wrap ul.bottom-to-top{
    height: 21px;
  }
  .marquee-wrap ul.bottom-to-top li{
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    text-align: left;
  }
              
            
!

JS

              
                (function ($) {
    $.fn.marquee = function( setting ){

      function rightToLeft( wrap, content, speed ){
        var width = 0;

        // 将内容复制四份,已解决单条公告循环滚动的问题
        content.append( content.html() );
        content.append( content.html() );


        content.find('li').each(function () {
          width += $(this).outerWidth();
        })

        content.width( width );


        function run() {
            if( wrap.scrollLeft() - (content.width() / 2) >= 0 ){
                wrap.scrollLeft( 0 );
            }else{
                wrap.scrollLeft( wrap.scrollLeft() + 1 );
            }
        }

        var timer = setInterval( run, speed );

        wrap.mouseover( function () {
            clearInterval( timer );
        } )

        wrap.mouseout( function () {
            timer = setInterval( run, speed );
        } )

      }

      function bottomToTop( wrap, content, speed ) {
          var noticeLength = content.find( 'li' ).length;
          var i = 0;

          function run() {
              content.animate({
                  top: -i * wrap.height()
              }, function () {
                  if( i < noticeLength-1 ){
                      i++;
                  }else{
                      i = 0;
                  }
              })
          }

          var timer = setInterval( run, speed );
          wrap.mouseover( function () {
              clearInterval( timer );
          } )
          wrap.mouseout( function () {
              timer = setInterval( run, speed );
          } )
      }

      function leftToRight( wrap, content, speed ){
        var width = 0;

        // 将内容复制四份,已解决单条公告循环滚动的问题
        content.append( content.html() );
        content.append( content.html() );

        content.find('li').each(function () {
          width += $(this).outerWidth();
        })

        content.width( width );
        wrap.scrollLeft( content.width() )

        function run() {
            if( wrap.scrollLeft() - (content.width() / 2) == 0 ){
              wrap.scrollLeft( content.width() );
            }else{
              wrap.scrollLeft( wrap.scrollLeft() - 1 );
            }
        }

        var timer = setInterval( run, speed );

        wrap.mouseover( function () {
            clearInterval( timer );
        } )

        wrap.mouseout( function () {
            timer = setInterval( run, speed );
        } )
      }

      return this.each(function(){
        var wrap = $(this);
        var content = wrap.find( setting.container );

        var animateType = wrap.attr('type');

        if ( animateType == 'right-to-left' ){
          content.addClass( animateType );
          rightToLeft( wrap, content, setting.speed );
        }

        if ( animateType == 'left-to-right' ){
          content.addClass( animateType );
          leftToRight( wrap, content, setting.speed );
        }

        if ( animateType == 'static' ){
            content.addClass( animateType );
        }

        if ( animateType == 'bottom-to-top' ){
            content.addClass( animateType );
            bottomToTop( wrap, content, setting.speed );
        }

      })
    }
  })(jQuery)

  var setting = {
    container: '.marquee-content',
    speed: 50
  }
  $('.marquee-wrap').marquee( setting );
              
            
!
999px

Console