代码之家  ›  专栏  ›  技术社区  ›  toadly

在新的YouTube设计中看到的延迟加载样式的文本?

  •  13
  • toadly  · 技术社区  · 7 年前

    似乎找不到这是什么图书馆,也找不到关于它的任何东西,但我最近看到它的次数越来越多。通过lazyload(带灰色bg占位符)查询动态文本。例如:cloudflare。com,youtube。com,upwork。通用域名格式。 enter image description here

    有人知道这是什么吗?谢谢

    1 回复  |  直到 7 年前
        1
  •  17
  •   Chris    5 年前

    不需要库,实际上它非常简单,只需HTML和CSS动画即可完成,请参见下面的示例。

    /*
    The javascript here is only for demonstration purpose in order to switch 
    between 'pulse' and 'wave' animation effect, it is not actually required.
    */
    
    jQuery(document).ready(function ($){
      $('#pulse').click(function(){
          $('.placeholder').removeClass('wave').addClass('pulse');
      })
      
      $('#wave').click(function(){
          $('.placeholder').removeClass('pulse').addClass('wave');
      })
    
      $('#stop').click(function(){
          $('.placeholder').removeClass('pulse wave');
      })
    });
    .placeholder{
      margin:15px;
      padding:10px;
      height: 115px;
      border: 1px solid lightgrey;
      border-radius: 5px;
    }
    
    .placeholder div{background:#E8E8E8;}
    
    .placeholder .square{
      float:left;
      width: 90px;
      height:56px;
      margin:0 0 10px;
    }
    
    .placeholder .line{height:12px;margin:0 0 10px 105px;}
    .placeholder .line:nth-child(2){width: 120px;}
    .placeholder .line:nth-child(3){width: 170px;}
    .placeholder .line:nth-child(4){width: 150px;}
    
    .placeholder .circle{
      float:left;
      width: 15px;
      height:15px;
      margin:0 15px 10px 0;
      border-radius:15px;
    }
    
    /* 
      --------------
      Pulse effect animation 
      Activated by adding a '.pulse' class to the placeholder
      --------------
    */
    
    .placeholder.pulse div{
      animation: pulse 1s infinite ease-in-out;
      -webkit-animation:pulse 1s infinite ease-in-out;
    }
    
    @keyframes pulse
    {
      0%{
        background-color: rgba(165,165,165,.1);
      }
      50%{
        background-color: rgba(165,165,165,.3);
      }
      100%{
        background-color: rgba(165,165,165,.1);
      }
    }
    
    @-webkit-keyframes pulse
    {
      0%{
        background-color: rgba(165,165,165,.1);
      }
      50%{
        background-color: rgba(165,165,165,.3);
      }
      100%{
        background-color: rgba(165,165,165,.1);
      }
    }
    
    /* 
      --------------
      Wave effect animation 
      Activated by adding a '.wave' class to the placeholder
      --------------
    */
    
    .placeholder.wave div{
        animation: wave 1s infinite linear forwards;
        -webkit-animation:wave 1s infinite linear forwards;
        background: #f6f7f8;
        background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
        background-size: 800px 104px;
    }
    
    @keyframes wave{
        0%{
            background-position: -468px 0
        }
        100%{
            background-position: 468px 0
        }
    }
    
    @-webkit-keyframes wave{
        0%{
            background-position: -468px 0
        }
        100%{
            background-position: 468px 0
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <button id="pulse">Pulse Effect</button>
    <button id="wave">Wave Effect</button>
    <button id="stop">Stop Animation</button>
    
    <div class="placeholder pulse">
    
      <div class="square"></div>
    
      <div class="line"></div>
      <div class="line"></div>
      <div class="line"></div>
    
      <div class="circle"></div>
      <div class="circle"></div>
      <div class="circle"></div>
    
    </div>

    以上代码的灵感来自以下文章: