使用CSS在表格栏中创建类似字幕的效果

时间:2019-03-30 04:52:37

标签: html css animation

我有这样的桌子:

<table>
    <tr>
        <td>Column1 Static</td>
        <td>Column2 Static</td>
        <td>Column3 with some long moving text</td>
    </tr>
</table>

如何使用CSS为第三列创建类似字幕的效果?

2 个答案:

答案 0 :(得分:0)

通过在表格上放置一些其他样式,JavaScript <marquee/>上的@Tats_innit's post提供了一个很好的解决方案(JQuery)。

(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();
  };
})(jQuery);

$('.marquee').marquee();
td {
  min-width: 150px;
  max-width: 150px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Column1 Static</td>
    <td>Column2 Static</td>
    <td>
      <div class="marquee">Column3 with some long moving text</div>
    </td>
  </tr>
</table>

希望这会有所帮助,

答案 1 :(得分:0)

这是一个纯CSS解决方案。演示中使用了CSS属性transform: translateX()@keyframesCSS animation。如果出于某种不可思议的原因,您的浏览器是2017年之前的版本,则提供JavaScript prefix function

var animation = false,
    animationstring = 'animation',
    keyframeprefix = '',
    domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
    pfx  = '',
    elem = document.createElement('div');

if( elem.style.animationName !== undefined ) { animation = true; }    

if( animation === false ) {
  for( var i = 0; i < domPrefixes.length; i++ ) {
    if( elem.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
      pfx = domPrefixes[ i ];
      animationstring = pfx + 'Animation';
      keyframeprefix = '-' + pfx.toLowerCase() + '-';
      animation = true;
      break;
    }
  }
}
:root {
  font: 400 16px/1.5 Verdana;
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  overflow: hidden;
}

.table {
  table-layout: fixed;
  border-collapse: collapse;
  width: 90%;
  margin: 5px auto;
}

td {
  width: 15%;
  text-align: center;
  border: 3px solid rgba(233, 233, 233, 0.3);
}

td:last-of-type {
  width: 70%;
}

td>b {
  display: block;
  margin: -15px auto 0;
  font-size: 1.5rem;
  color: tomato;
}

.marquee {
  width: 90%;
  /* Required on Parent */
  overflow: hidden;
  background: rgba(0, 0, 0, 0.7);
  padding-left: 15px;
  margin: 20px auto;
  font-size: 2rem;
}

.scroll {
  /* Required on Child*/
  white-space: nowrap;
  display: table-cell;
  color: cyan;
  vertical-align: baseline;
  /* Infinite Loops */
  animation: rightToLeft 12s linear infinite;
  /* Right to left direction */
  animation-fill-mode: backwards;
  /* Set to 0s in order to have a point of reference */
  animation-delay: 0s;
}

.scroll::before {
  content: ' ';
}

.scroll::after {
  content: ' ';
}

.scroll a {
  color: gold
}


/* Required for complex CSS animation */

@keyframes rightToLeft {
  0% {
    transform: translateX(20%);
  }
  100% {
    transform: translateX(-100%);
  }
}
<table class='table'>
  <tbody>
    <tr>
      <td>15%<b>⟺</b></td>
      <td>15%<b>⟺</b></td>
      <td>
        <figure class='marquee'>
          <figcaption class='scroll'>You should read <i>“how to ask”</i>:&nbsp;<a href="https://stackoverflow.com/help/how-to-ask">https://stackoverflow.com/help/how-to-ask</a></figcaption>
        </figure>
      </td>
    </tr>
    <tr>
      <td>15%<b>⟺</b></td>
      <td>15%<b>⟺</b></td>
      <td>
        <figure class='marquee'>
          <figcaption class='scroll'></figcaption>
        </figure>
      </td>
    </tr>
  </tbody>
</table>