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

Solution to MySQL being unable to start due to excessive memory configuration

Problem Description MySQL reports an error when s...

Implementation of CSS text shadow gradually blurring effect

text-shadow Add a shadow to the text. You can add...

W3C Tutorial (2): W3C Programs

The W3C standardization process is divided into 7...

Install .NET 6.0 in CentOS system using cloud server

.NET SDK Download Link https://dotnet.microsoft.c...

Implementation of Docker private warehouse registry deployment

As more and more Docker images are used, there ne...

Summary of 10 advanced tips for Vue Router

Preface Vue Router is the official routing manage...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

MySQL 5.7.21 installation and configuration tutorial under Window10

This article records the installation and configu...

The difference between Div and table in HTML (discussed in detail in all aspects)

1: Differences in speed and loading methods The di...

How to get the contents of .txt file through FileReader in JS

Table of contents JS obtains the .txt file conten...

CSS positioning layout (position, positioning layout skills)

1. What is positioning? The position attribute in...