CSS实现自动播放相册------Sestid

效果展示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ew53xciF-1572680082129)(CSS实战笔记(五)] 动态相册/fade_image.gif)

以下是两种实现方式(CSS或者js) 

CSS代码:

<!DOCTYPE html>
<html>

<head>
  <style>
    html {
      background-color: black;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    .frame {
      width: 500px;
      min-width: 500px;
      height: 300px;
      min-height: 300px;
      border-width: 8px;
      border-style: solid;
      border-color: goldenrod darkgoldenrod;
      background-color: black;
      position: relative;
      overflow: hidden;
    }
    .photo {
      opacity: 0;
      position: absolute;
      animation: fade 12s infinite;
    }
    @keyframes fade {
      25% { opacity: 1; }
      50% { opacity: 0; }
    }
    .photo:nth-child(1) {
      animation-delay: 0s;
    }
    .photo:nth-child(2) {
      animation-delay: 4s;
    }
    .photo:nth-child(3) {
      animation-delay: 8s;
    }
  </style>
</head>

<body>
  <div class="frame">
    <img class='photo' src="https://cdn.pixabay.com/photo/2018/01/03/05/33/the-sun-3057622__340.jpg" alt="" />
    <img class='photo' src="https://cdn.pixabay.com/photo/2016/12/09/06/22/the-beach-1893714__340.jpg" alt="" />
    <img class='photo' src="https://cdn.pixabay.com/photo/2017/12/05/01/12/sunset-2998295__340.jpg" alt="" />
  </div>
</body>

</html>

JavaScript实现的代码:

<!DOCTYPE html>
<html>

<head>
  <style>
    html {
      background-color: black;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    .frame {
      width: 500px;
      min-width: 500px;
      height: 300px;
      min-height: 300px;
      border-width: 8px;
      border-style: solid;
      border-color: goldenrod darkgoldenrod;
      background-color: black;
      position: relative;
      overflow: hidden;
    }
    .photo {
      opacity: 0;
      position: absolute;
    }
    @keyframes fade {
      25% { opacity: 1; }
      50% { opacity: 0; }
    }
  </style>
  <script>
    function setAnimation(){
      let frame = document.getElementById('frame')
      let imgWrapper = document.createElement('div')
      let imgSrc = [
        'https://cdn.pixabay.com/photo/2018/01/03/05/33/the-sun-3057622__340.jpg',
        'https://cdn.pixabay.com/photo/2016/12/09/06/22/the-beach-1893714__340.jpg',
        'https://cdn.pixabay.com/photo/2017/12/05/01/12/sunset-2998295__340.jpg'
      ]
      let animationInterval = 4
      for (let currIndex = 0, imgNum = imgSrc.length; currIndex < imgNum; currIndex++) {
        let imgElem = document.createElement('img')
        imgElem.src = imgSrc[currIndex]
        imgElem.alt = ''
        imgElem.classList.add('photo')
        imgElem.style['animation-name'] = 'fade'
        imgElem.style['animation-duration'] = (imgNum * animationInterval) + 's'
        imgElem.style['animation-iteration-count'] = 'infinite'
        imgElem.style['animation-delay'] = (currIndex * animationInterval) + 's'
        imgWrapper.appendChild(imgElem)
      }
      frame.appendChild(imgWrapper)
    }
  </script>
</head>

<body onload="setAnimation()">
  <div id="frame" class="frame"></div>
</body>

</html>

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值