JavaScript to achieve Taobao product image switching effect

JavaScript to achieve Taobao product image switching effect

JavaScript clothing album switching effect (similar to Taobao product image switching), for your reference, the specific content is as follows

Without further ado, let's get straight to the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #bigImg{
            width: 200px;
        }
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            list-style: none;
            /*overflow: hidden;*/
        }
        ul li{
            float: left;
            width: 46px;
            height: 46px;
            margin-left: 10px;
            margin-top: 20px;
            border: 2px solid #ffffff;
        }
        ul .active{
            border-color: red;
        }
    </style>
</head>
<body>
    <img src="img/cloth_01.jpg" id="bigImg">
    <ul>
        <li class="active">
            <a href="">
                <img src="img/cloth_01.jpg" width=46 class="smallImg">
            </a>
        </li>
        <li>
            <a href="">
                <img src="img/cloth_02.jpg" width=46 class="smallImg">
            </a>
        </li>
        <li>
            <a href="">
                <img src="img/cloth_03.jpg" width=46 class="smallImg">
            </a>
        </li>
        <li>
            <a href="">
                <img src="img/cloth_04.jpg" width=46 class="smallImg">
            </a>
        </li>
        <li>
            <a href="">
                <img src="img/cloth_05.jpg" width=46 class="smallImg">
            </a>
        </li>
    </ul>
    
    <!--JS part-->
    <script type="text/javascript">
        // 1. Get the event source var bigImg = document.getElementById("bigImg");
        var smallImgs = document.getElementsByClassName("smallImg");

        for (var i=0;i<smallImgs.length;i++){
            // 2. Traverse the collection and add an event to each img tag smallImgs[i].onmouseover = function (){

                // 3. Event handler // 3.1 Before hovering to each li tag, set the class names of all li tags to empty values ​​for (var j=0;j<smallImgs.length;j++){
                    smallImgs[j].parentNode.parentNode.setAttribute("class","");
                }

                // 3.2 Modify the src attribute value of the large image var smallImgSrc = this.getAttribute("src");
                bigImg.setAttribute("src",smallImgSrc);
                // 3.3 Add a class to the parent tag of the img tag that the mouse is hovering over this.parentNode.parentNode.setAttribute("class","active");

            }
        }
    </script>
</body>
</html>

Implementation effect diagram:

The first picture is selected by default (the large picture is the first one by default). When the mouse hovers over the corresponding picture, the large picture switches to that 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:
  • Videojs+swiper realizes Taobao product details carousel
  • javascript to realize product image magnifying glass
  • JavaScript implements product details of e-commerce platform

<<:  How to solve the error when connecting to MySQL in Linux: Access denied for user 'root'@'localhost'(using password: YES)

>>:  How to use Docker Compose to implement nginx load balancing

Recommend

How to implement communication between Docker containers

Scenario: A laradock development environment (php...

Solution to forgetting MySQL root password in MACOS

MySQL is a relational database management system ...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

A brief analysis of the differences between px, rem, em, vh, and vw in CSS

Absolute length px px is the pixel value, which i...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

The difference between html block-level tags and inline tags

1. Block-level element: refers to the ability to e...

How to disable foreign key constraint checking in MySQL child tables

Prepare: Define a teacher table and a student tab...

Discussion on Web Imitation and Plagiarism

A few months after entering the industry in 2005, ...

docker-maven-plugin packages the image and uploads it to a private warehouse

Table of contents 1. Introduction to docker-maven...

Detailed explanation of MySQL foreign key constraints

Official documentation: https://dev.mysql.com/doc...

Detailed explanation of the practical use of HTML table layout

When is the table used? Nowadays, tables are gene...