点击查看html编辑器说明文档

幻灯片层叠效果(纯JS+CSS实现)edit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk
提交反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <div class="slide">
  <div id="img1" class="item item1 position1">1</div>
  <div id="img2" class="item item2 position2">2</div>
  <div id="img3" class="item item3 position3">3</div>
  <div id="img4" class="item item4 position4">4</div>
</div>
        
</body>
CSS
格式化
            
            .slide {
  position: relative;
}

.slide .item {
  width: 150px;
  height: 100px;
  position: absolute;
  transition: transform 0.8s, z-index 0.6s;
}

.item1 { background-color: #c3aa8a; }
.item2 { background-color: #982029; }
.item3 { background-color: #4f92c7; }
.item4 { background-color: #7f9489; }

.position1 {
  z-index: 4;
  transform: translate(75%,70%) scale(1);
}
.position2 {
  z-index: 3;
  transform: translate(150%,40%) scale(0.8);
}
.position3 {
  z-index: 2;
  transform: translate(75%,20%) scale(0.7);
}
.position4 {
  z-index: 1;
  transform: translate(0,40%) scale(0.8);
}
        
JS
格式化
            
            let slideData = [
  { el: document.getElementById('img1'), index: 1},
  { el: document.getElementById('img2'), index: 2},
  { el: document.getElementById('img3'), index: 3},
  { el: document.getElementById('img4'), index: 4},
]

function next() {
  slideData.forEach(item => {
    item.el.classList.remove('position' + item.index)
    if(item.index < 4) {
      item.index ++;
    } else {
      item.index = 1;
    }
    item.el.classList.add('position' + item.index);
  })
}

//每2秒循环执行
setInterval(() => {
  next();
}, 2000)


        
预览
控制台