SoFunction
Updated on 2025-03-03

JS+CSS implements 3D cutting carousel diagram

This article shares the specific code for JS+CSS to implement 3D cutting carousel diagrams for your reference. The specific content is as follows

first step:We first need to complete a basic layout through CSS and set it to 3D using transform-style: preserve-3d.

Below is the CSS part code

* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}
li {
 list-style: none;
}
/* Carousel picture */
.homePage {
 width: 800px;
 height: 200px;
 margin: 150px auto;
 background-color: pink;
 position: relative;
 /* overflow: hidden; */
}

.homePage>ul {
 width: 100%;
 height: 100%;
}

.homePage>ul>li {
 width: 200px;
 height: 100%;
 float: left;
 transform-style: preserve-3d;
 position: relative;
 transition: all 1s;
}

.homePage>ul>li>span {
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}

.homePage>ul>li>span:nth-child(1) {
 background-image: url(../images/);
 transform: translateZ(100px);
}
.homePage>ul>li>span:nth-child(2) {
 background-image: url(../images/);
 transform: rotateX(90deg) translateZ(100px);
}
.homePage>ul>li>span:nth-child(3) {
 background-image: url(../images/);
 transform: rotateX(180deg) translateZ(100px);
}
.homePage>ul>li>span:nth-child(4) {
 background-image: url(../images/);
 transform: rotateX(270deg) translateZ(100px);
}

/* Location of pictures */
.homePage>ul>li:nth-child(2) span {
 background-position: -200px 0;
}
.homePage>ul>li:nth-child(3) span {
 background-position: -400px 0;
}
.homePage>ul>li:nth-child(4) span {
 background-position: -600px 0;
}

/* Small button */
a {
 position: absolute;
 width: 30px;
 height: 70px;
 background-color: rgba(0,0,0,.2);
 text-decoration: none;
 color: #fff;
 top: 50%;
 margin-top: -35px;
 line-height: 70px;
 text-align: center;
 display: none;
}
.right {
 right: 0;
}

Step 2:We use JS to adjust the cutting speed and direction of the image when moving.

Below is the code for js.

$(function(){
 var index = 0;
 var flag = true;
 var time = setInterval(move, 700);

 // Next function move(){
 if(!flag) return;
 flag = false;
 index++;
 $(".homePage>ul>li").css("transform","rotateX(" + (-90 * index) + "deg)").each(function(index,item){
  $(item).css("transition-delay",index * 0.2 + "s");
 });
 }

 // Move in and out $(".homePage").mouseenter(function(){
 clearInterval(time);
 $(".homePage>a").css("display","block");
 })
 $(".homePage").mouseleave(function(){
 time = setInterval(move, 700);
 $(".homePage>a").css("display","none");
 })

 // Click the button on the left: Previous $(".left").on("click",function(){
 if(!flag) return;
 flag = false;
 index--;
 $(".homePage>ul>li").css("transform","rotateX(" + (-90 * index) + "deg)").each(function(index,item){
  $(item).css("transition-delay",index * 0.2 + "s");
 });
 })

 // Click the faced button: Next $(".right").click(move);

 // After the entire transition of the animation is completed, it will be triggered when the transitionend transition is completed. $("li:last").on("transitionend",function(){
 flag = true;
 })
})

Finally: the body area code is as follows

 <div class="homePage">
 <ul>
  <li>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  </li>
  <li>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  </li>
  <li>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  </li>
  <li>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  </li>
 </ul>
 <a class="left" href="javascript:;" ><</a>
 <a class="right" href="javascript:;" >></a>
 </div>
 
<script src="js/jquery-1.8."></script>
<script src="js/"></script>

Note: Use overflow: hidden; to hide the excess part when cutting. The final 3D cutting carousel is completed.

Wonderful topic sharing:jQuery picture carousel JavaScript picture carousel Bootstrap picture carousel

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.