JavaScript to achieve accordion effect

JavaScript to achieve accordion effect

This article shares the specific code for JavaScript to achieve the accordion effect for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">


<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
  
  ul {
   width: 100%;
   text-align: center;
  }
  
  ul li {
   list-style: none;
   overflow: hidden;
   display: inline-block;
   transition: all 1s;
  }
 </style>
</head>

<body>
 <ul>

 </ul>
 <script>
  var ul = document.querySelector("ul");
  var imgs = ["img/img4.jpg", "img/img5.jpg", "img/img6.jpg", "img/img7.jpg", "img/img8.jpg", "img/img9.jpg", ];
  for (var i = 0; i < imgs.length; i++) {
   var li = document.createElement("li");
   var img = document.createElement("img");
   img.src = imgs[i];
   li.appendChild(img);
   ul.appendChild(li);
  }
  var lis = document.querySelectorAll("li");
  window.onload = window.onresize = function() {
   for (var i = 0; i < lis.length; i++) {
    lis[i].style.width = (ul.offsetWidth / imgs.length) - 10 + "px";
   }
  }
  var imgM = document.querySelectorAll("img");
  for (let i = 0; i < imgM.length; i++) {
   imgM[i].onmouseenter = function() {
    for (var j = 0; j < imgM.length; j++) {
     lis[j].style.width = ((ul.offsetWidth - imgM[i].offsetWidth) / (imgs.length - 1)) - 10 + "px";
    }
    lis[i].style.width = imgM[i].offsetWidth + "px";
   }

   imgM[i].onmouseleave = function() {
    for (var i = 0; i < lis.length; i++) {
     lis[i].style.width = (ul.offsetWidth / imgs.length) - 10 + "px";
    }
   }
  }
 </script>
</body>

</html>

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:
  • Pure js to achieve accordion effect code
  • Accordion effect navigation menu made by native js
  • Use ReactJS to implement tab page switching, menu bar switching, accordion switching and progress bar effects
  • Vue.js accordion menu component development example
  • js to achieve a simple accordion effect
  • js realizes the effect of foldable and expandable accordion menu
  • Pure js to achieve accordion effect
  • Native JS to achieve vertical accordion effect
  • Cool js accordion effect
  • JS realizes the picture accordion effect

<<:  Nginx server https configuration method example

>>:  How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

Recommend

How to get the size of a Linux system directory using the du command

Anyone who has used the Linux system should know ...

Solution to "No input file specified" in nginx+php

Today, the error "No input file specified&qu...

Vue implements button switching picture

This article example shares the specific code of ...

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Chinese and English font name comparison table (including Founder and Arphic)

In CSS files, we often see some font names become...

Using keras to judge SQL injection attacks (example explanation)

This article uses the deep learning framework ker...

Detailed explanation of the problem of configuring servlet url-pattern in tomcat

When configuring web.xml for tomcat, servlet is a...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

MySQL table auto-increment id overflow fault review solution

Problem: The overflow of the auto-increment ID in...

Detailed explanation of where the image pulled by docker is stored

20200804Addendum: The article may be incorrect. Y...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

W3C Tutorial (2): W3C Programs

The W3C standardization process is divided into 7...

Analysis of the implementation principle of Vue instructions

Table of contents 1. Basic Use 2. Working Princip...

Detailed explanation of moment.js time and date processing

Monday to Sunday time format conversion (Y --- ye...