css實現幻燈片播放效果


用css實現幻燈片播放是最基礎的,閑下來沒事就試着寫了一下,如果有不夠完善或者方法不好的地方還請指點。下面我就用兩種方法實現css花燈片效果。

方法1:定位。通過position屬性改變left值

html代碼:

<section id=box>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>1</li>
  </ul>
</section>

css代碼:

<style>
    * {
      margin: 0;
      padding: 0;
      font-family: 微軟雅黑;
      list-style: none;
    }
    #box{
        width:400px;
        height:200px;
        border: 1px solid #000;
        margin: 50px auto;
        position:relative;
        overflow: hidden;
    }
    ul{
        width: 2000px;
        position: absolute;
        top:0;
        left:0;
      animation: dh 10s infinite ease;
    }
    ul li{
      width:400px;
      height:200px;
      float: left;

    }
    ul li:nth-child(1){
      background:#4b86db;
    }
    ul li:nth-child(2){
      background:#ff9999;
    }
    ul li:nth-child(3){
      background:olivedrab;
    }
    ul li:nth-child(4){
      background:skyblue;
    }
    ul li:nth-child(5){
      background:#4b86db;
    }
        @keyframes dh {
          0%{
            left:0px;
      }
          25%{
            left:-400px;
          }
          50%{
            left:-800px;
          }
          75%{
            left:-1200px;
          }
          100%{
            left:-1600px;
          }
    }
</style>

 

方法2:2D轉換。通過transfrom屬性

html代碼:

<section id=box>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>1</li>
  </ul>
</section>

 

css代碼:

<style>
    * {
      margin: 0;
      padding: 0;
      font-family: 微軟雅黑;
      list-style: none;
    }
    #box{
        width:400px;
        height:200px;
        border: 1px solid #000;
        margin: 50px auto;
        overflow: hidden;
    }
    ul{
      width: 2000px;
      animation: dh 10s infinite ease;
    }
    ul li{
      width:400px;
      height:200px;
      float: left;

    }
    ul li:nth-child(1){
      background:#4b86db;
    }
    ul li:nth-child(2){
      background:#ff9999;
    }
    ul li:nth-child(3){
      background:olivedrab;
    }
    ul li:nth-child(4){
      background:skyblue;
    }
    ul li:nth-child(5){
      background:#4b86db;
    }
 
@keyframes dh {
      0%{transform: translateX(0)}
      25%{transform: translateX(-400px)}
      50%{transform: translateX(-800px)}
      75%{transform: translateX(-1200px)}
      100%{transform: translateX(-1600px)}
}
  </style>

以上兩種方法是我想到最簡單的方法,如果有更好的方法還請朋友們留言指教。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM