js to achieve 3D carousel effect

js to achieve 3D carousel effect

This article shares the specific code for implementing 3D carousel effects with js for your reference. The specific content is as follows

There are mainly translation and rotation to form a 3D effect carousel. I am a newbie, so please give me more advice if there are any shortcomings. The code is as follows

CSS code:

 *{
            padding: 0;
            margin: 0;
        }
        .box{
            position: relative;
            width: 100%;
            height: 100%;
            top: 200px;
            margin: auto;
        }
        .warpper{
            position: absolute;
            width: 100%;
            height: 100%;
            perspective: 800px;
            transform-style:preserve-3d;
 
        }
        .box .warpper img{
            width: 300px;
            height: 200px;
            float: left;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            border-radius: 8px;
            transition: all 1s ease-in-out;
        }
        .btn{
            position: relative;
            top: 50%;
            left: 50%; 
            width: 1200px;
            transform: translate(-50%,-20px);
        }
        .btn .left,.btn .right{
            height: 50px;
            width: 50px;
            font-size: 30px;
            text-align: center;
            line-height: 50px;
            background-color: black;
            color: white;
            border-radius: 15%;
            position: absolute;
        }
        .btn .left{
            left: 0;
        }
        .btn .right{
            right: 0;
        }
        .btn span:hover{
            background-color: rgba(0,0,0,0.8);
        }
        .points{
            position: absolute;
            left: 50%;
            bottom: 10px;
            transform: translate(-50%,200px);
            height: 14px;
        }
        .points li{
            display: inline-block;
            list-style: none;
            width: 14px;
            height: 14px;
            border: 1px solid #000;
            border-radius: 50%;
            background-color: white;
            margin: 0 5px;
        }
        .points .current{
            background-color: red;
        }

HTML code:

<div class="box">
        <div class="warpper">
            <img src="./Inspiration 3.jpg" alt="">
            <img src="./Inspirational2.jpg" alt="">
            <img src="./2f1d.jpg" alt="">
            <img src="./aimg.jpg" alt="">
            <img src="./peg.jpg" alt="">
        </div>
        <div class="btn" id="btn">
            <span class="left"> < </span>
            <span class="right"> > </span>
        </div>
        <ul class="points">
        </ul>
    </div>
<script src="swarp.js"></script>

JS code:

var imgs = document.querySelectorAll("img")
var btns = document.querySelectorAll("span")
var ul = document.querySelector(".points")
var lis = document.getElementsByTagName("li")
var timer
var current = 0 // Index of the currently playing picture var flag = true // Click anti-shake throttling var len = imgs.length // Picture length function loadFirst() {
    imgMove()
    bind()
    btnClick()
    getDot()
    showDot()
    autoPlay()
}
loadFirst()
function imgMove() {
    var mlen = Math.floor(len / 2)
    for (var i = 0; i < mlen; i++) {
        // Current playback image index value var rimg = current + 1 + i
        var limg = len + current - 1 - i
        if (rimg >= len) {
            rimg -= len
        }
        if (limg >= len) {
            limg -= len
        }
        imgs[limg].style.transform = `translateX(${150 * (i + 1)}px) translateZ(${200 - i * 100}px) rotateY(-30deg)`
        imgs[rimg].style.transform = `translateX(${-150 * (i + 1)}px) translateZ(${200 - i * 100}px) rotateY(30deg)`
    }
    imgs[current].style.transform = `translateZ(300px)`
}
// Automatic playback function function autoPlay() {
    timer = setInterval(function () {
        if (current >= len - 1) {
            current = 0
        } else {
            current++
        }
        imgMove()
        autoLi()
    }, 3000)
}
// Click on the picture to play function bind() {
    for (let i = 0; i < imgs.length; i++) {
        imgs[i].onclick = function () {
            current = i
            imgMove()
            autoLi()
        }
        imgs[i].onmouseover = function () {
            clearInterval(timer)
        }
        imgs[i].onmouseout = function () {
            autoPlay()
        }
    }
}
// Click the left and right buttons function btnClick() {
    for (let i = 0; i < btns.length; i++) {
        btns[i].onclick = function () {
            // Prevent crazy clicking if (!flag) {
                return
            }
            flag = false
            if (i == 0) {
                // Previous if (current <= 0) {
                    current = len - 1
                } else {
                    current--
                }
            } else {
                //Next if (current >= len - 1) {
                    current = 0
                } else {
                    current++
                }
            }
            setTimeout(function () {
                flag = true
            }, 1000)
 
            imgMove()
            autoLi()
 
        }
        btns[i].onmouseenter = function () {
            clearInterval(timer)
        }
        btns[i].onmouseout = function () {
            autoPlay()
        }
    }
}
// dot function getDot() {
    for (var i = 0; i < len; i++) {
        ul.innerHTML += `<li></li>`
    }
    lis[0].classList.add("current")
}
function showDot() {
    for (let i = 0; i < len; i++) {
        lis[i].onclick = function () {
            for (var j = 0; j < len; j++) {
                lis[j].classList.remove("current")
            }
            this.classList.add("current")
            current = i
            imgMove()
        }
    }
}
function autoLi() {
    for (var i = 0; i < len; i++) {
        if (i == current) {
            lis[i].classList.add("current")
        } else {
            lis[i].classList.remove("current")
        }
    }
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS implementation of carousel example
  • 3 simple ways to achieve carousel effects with JS
  • Pure js to achieve the effect of carousel
  • Native JS to implement breathing carousel
  • js to create a carousel effect
  • JavaScript imitates Xiaomi carousel effect
  • About the implementation of JavaScript carousel

<<:  Implementation of local migration of docker images

>>:  MySQL 8.0.23 free installation version configuration detailed tutorial

Recommend

MYSQL's 10 classic optimization cases and scenarios

Table of contents 1. General steps for SQL optimi...

A brief understanding of MySQL SELECT execution order

The complete syntax of the SELECT statement is: (...

How to import CSS styles into HTML external style sheets

The link-in style is to put all the styles in one...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

How to monitor array changes in Vue

Table of contents Preface Source code Where do I ...

Summary of ten Linux command aliases that can improve efficiency

Preface Engineers working in the Linux environmen...

CSS implementation code for drawing triangles (border method)

1. Implement a simple triangle Using the border i...

What you need to know about MySQL auto-increment ID

Introduction: When using MySQL to create a table,...

JS+Canvas realizes dynamic clock effect

A dynamic clock demo based on Canvas is provided ...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

CentOS installation mysql5.7 detailed tutorial

This article shares the detailed steps of install...

Basic operations on invisible columns in MySQL 8.0

Table of contents 01 Create invisible columns 02 ...

JavaScript canvas realizes dynamic point and line effect

This article shares the specific code for JavaScr...

Detailed explanation of Vue two-way binding

Table of contents 1. Two-way binding 2. Will the ...

Understanding JSON (JavaScript Object Notation) in one article

Table of contents JSON appears Json structure Jso...