js to achieve a simple carousel effect

js to achieve a simple carousel effect

This article shares the specific code of js to achieve a simple carousel effect for your reference. The specific content is as follows

Image switching using transform = translateX()

<style>
        .box {
            position: relative;
            overflow: hidden;
            margin: 200px auto;
            width: 600px;
            height: 400px;
        }
        
        .img {
            width: 3000px;
            height: 400px;
        }
        
        img {
            float: left;
            width: 600px;
            height: 400px;
        }
        
        .btn {
            position: absolute;
            top: 350px;
            left: 245px;
            width: 110px;
            height: 20px;
        }
        
        .dian {
            float: left;
            margin: 5px;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.3);
            cursor: pointer;
        }
        
        .left,
        .right {
            display: none;
            box-sizing: border-box;
            position: absolute;
            top: 150px;
            width: 50px;
            height: 100px;
            background-color: rgba(0, 0, 0, 0.521);
            font-size: 40px;
            line-height: 100px;
            color: #fff;
        }
        
        .box:hover .left,
        .box:hover .right {
            display: block;
        }
        
        .left {
            padding-left: 10px;
            left: 0;
        }
        
        .right {
            padding-right: 10px;
            text-align: right;
            right: 0;
        }
</style>
<div class="box">
        <div class="img">
            <img src="./img/1.jpeg" alt="">
            <img src="./img/2.jpeg" alt="">
            <img src="./img/3.jpeg" alt="">
            <img src="./img/4.jpeg" alt="">
            <img src="./img/5.jpeg" alt="">
        </div>

        <div class="btn">
            <span class="dian"></span>
            <span class="dian"></span>
            <span class="dian"></span>
            <span class="dian"></span>
            <span class="dian"></span>
        </div>

        <div class="left">&lt; </div>
        <div class="right">&gt;</div>
</div>
<script>
        var btn = document.getElementsByClassName('btn')[0];
        var imgBox = document.getElementsByClassName('img')[0];
        var imgs = imgBox.getElementsByTagName('img');
        var btnChild = document.getElementsByClassName('dian');

        var k = 0;
        // Add custom properties to the button for (var i = 0; i < btnChild.length; i++) {
            btnChild[i].dataset.num = i * 600;
        }
        // The default is the first picture, and the first button is white btnChild[0].style.background = 'rgba(255, 255, 255, 0.8)';

        // Click the button to switch the image and change the button color btn.onclick = function(e) {

            if (e.target.nodeName === 'SPAN') {
                // First set all buttons to default color for (var i = 0; i < btnChild.length; i++) {
                    btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)';
                }
                // The button clicked changes color k = +e.target.dataset.num;
                console.log(k);
                btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)';
                // Switch image imgBox.style.transform = `translateX(-${e.target.dataset.num}px)`;
            }
        }

        // Get the image length // var imgWidth = +getComputedStyle(imgs[0]).width.slice(0, -2);
        var imgWidth = imgs[0].offsetWidth;


        // Timer moves the picture var interval1 = setInterval(move, 1000);
        var interval;



        // Function moves the image and changes the button function move() {
            k += imgWidth;
            if (k === imgWidth * imgs.length) {
                k = 0;
            }
            // Move the image imgBox.style.transform = `translateX(-${k}px)`;
            // Change button color for (var i = 0; i < btnChild.length; i++) {
                btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)';
            }

            btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)';


        }


        // Move the mouse to the image to delete the timer var box = document.getElementsByClassName('box')[0];
        box.onmouseover = function() {
            clearInterval(interval1);
            clearInterval(interval);
        }

        // Move the mouse out to start the timer box.onmouseout = function() {
            interval = setInterval(move, 1000);
        }

        var leftBtn = document.getElementsByClassName('left')[0];
        var rightBtn = document.getElementsByClassName('right')[0];

        rightBtn.onclick = function() {

            k += imgWidth;
            if (k === imgWidth * imgs.length) {
                k = 0;
            }
            // Move the image imgBox.style.transform = `translateX(-${k}px)`;
            // Change button color for (var i = 0; i < btnChild.length; i++) {
                btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)';
            }

            btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)';


        }
        leftBtn.onclick = function() {
            k -= imgWidth;
            if (k < 0) {
                k = imgWidth * (imgs.length - 1);
            }
            imgBox.style.transform = `translateX(-${k}px)`;
            // Change button color for (var i = 0; i < btnChild.length; i++) {
                btnChild[i].style.background = 'rgba(255, 255, 255, 0.4)';
            }

            btnChild[k / 600].style.background = 'rgba(255, 255, 255, 0.8)';


        }
</script>

Effect picture:

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 implements multiple tab switching carousel
  • Native JavaScript to achieve the effect of carousel
  • JavaScript to implement the most complete code analysis of simple carousel (ES6 object-oriented)
  • JavaScript to implement simple carousel chart most complete code analysis (ES5)
  • js to implement the complete code of the carousel
  • Sample code for implementing carousel with native js
  • js to achieve the effect of supporting mobile phone sliding switch carousel picture example
  • JS carousel diagram implementation simple code
  • js to achieve click left and right buttons to play pictures
  • JavaScript implementation of carousel example

<<:  What you need to know about MySQL auto-increment ID

>>:  Tomcat reports an error when starting the springboot project war package: Error when starting the child

Recommend

7 ways to vertically center elements with CSS

【1】Know the width and height of the centered elem...

Detailed explanation of the usage of MySQL memory tables and temporary tables

Usage of MySQL memory tables and temporary tables...

Detailed explanation of data types in JavaScript basics

Table of contents 1. Data Type 1.1 Why do we need...

Split and merge tables in HTML (colspan, rowspan)

The code demonstrates horizontal merging: <!DO...

How to filter out certain libraries during mysql full backup

Use the --all-database parameter when performing ...

Detailed tutorial on installing and configuring MySQL 5.7.20 under Centos7

1. Download the MySQL 5.7 installation package fr...

Vue implements a small countdown function

Countdown function needs to be implemented in man...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

An example of the calculation function calc in CSS in website layout

calc is a function in CSS that is used to calcula...

The difference between distinct and group by in MySQL

Simply put, distinct is used to remove duplicates...

Detailed explanation of the usage of MySQL data type DECIMAL

MySQL DECIMAL data type is used to store exact nu...

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...

Detailed explanation of pure SQL statement method based on JPQL

JPQL stands for Java Persistence Query Language. ...