search
HomeWeb Front-endJS TutorialPure js+html and pure css+html create accordion effect_javascript skills

This article shares the two effects of making an accordion with pure js+html and making an accordion with pure css+html for your reference. The specific content is as follows

1. Pure css+html accordion effect

This kind of accordion written in CSS is relatively simple. It is mainly applied to the transition attribute in CSS.

The code is as follows:

<!DOCTYPE HTML>
<html>
<head>
<style>
body{background: url('bg.gif') repeat;}
ul,li,p{margin: 0px;padding: 0px;list-style: none;}
 #div{width: 1180px;height: 405px;border:5px solid #ccc;padding: 0px;margin: 0px auto;overflow: hidden;} 
 .list{width: 3200px;}
 .list li{float: left;width: 170px;height: 500px;;position: relative;
   -moz-transition:width 2s;
   transition: width 2s;
   -moz-transition: width 2s; /* Firefox 4 */
   -webkit-transition: width 2s; /* Safari 和 Chrome */
   -o-transition: width 2s; /* Opera */
 }
.list:hover li{width: 107px;}
.list li:hover{width: 538px;}
.list li p{width: 100%;height: 100%;opacity: 0.5;position: absolute;top: 0px;left: 0px;background: black; }
.list li:hover p{opacity:0}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
</script>
</head>
<body >
<div id="div">
   <ul class="list"> <!--如果设置父级与子级之间没有空隙的话,将margin和padding设为0即可-->
     <li><img  src="/static/imghwm/default1.png"  data-src="image/1.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/2.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/3.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/4.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/5.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/6.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
     <li><img  src="/static/imghwm/default1.png"  data-src="image/7.jpg"  class="lazy"   style="max-width:90%" alt="Pure js+html and pure css+html create accordion effect_javascript skills" ><p></p></li>
   </ul>
</div>
</body>
</html>

2. Making accordion with pure js+html

A problem with this accordion is that when each li is moved individually, there is no problem, but when the movement is very fast, there is a gap in the rightmost li. I think it's a timer problem, that is, before each li has returned to its position, the next li starts moving. But my timer has been turned off.

Can anyone please leave me a message and help me figure out how to change it!

The code is as follows:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>手风琴效果</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="perfectMove2.js"></script>
<script type="text/javascript">
window.onload=function()
{
  var oDiv=document.getElementById('show1');
  var iMinWidth=9999999;
  var aLi=oDiv.getElementsByTagName('li');
  var aSpan=oDiv.getElementsByTagName('span');
  var i=0;
  var bool=false;
  for(i=0;i<aLi.length;i++)
  {
    aSpan[i].index=i;
    aSpan[i].onmouseover=function ()
    {
       for(i=0;i<aLi.length;i++)
       {
         startMove(aLi[i],{width:this.offsetWidth});//调用运动函数
         bool=true;
       }
       if(bool)
       {
        startMove(aLi[this.index],{width:552});
       }
    }  
  }

};
</script>
</head>
<body>
<div id="show1">
  <ul>
    <li class="active">
      <span class="bg0">这是第一个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/1.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
    <li >
      <span class="bg1">这是第二个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/2.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
    <li >
      <span class="bg2">这是第三个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/3.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
    <li>
      <span class="bg3">这是第四个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/4.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
    <li>
      <span class="bg4">这是第五个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/5.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
    <li>
      <span class="bg5">这是第六个</span>
      <img  src="/static/imghwm/default1.png"  data-src="images/6.jpg"  class="lazy" / alt="Pure js+html and pure css+html create accordion effect_javascript skills" >
    </li>
  </ul>
</div>
</body>
</html>

perfectMove2.js code is as follows:

function getStyle(obj,attr)//用此种方法获取样式中的属性
{
   if(obj.currentStyle)
   {
    return obj.currentStyle[attr];
   }
   else
   {
    return getComputedStyle(obj,false)[attr];
   }
}
function startMove(obj,json,fn)
{
    clearInterval(obj.timer);//清除定时器
    obj.timer=setInterval(function ()
    {
    var stop=true;
    for(var attr in json)
    {
           var iCur=0;
           if(attr=='opacity')
           {
             iCur=parseInt(parseFloat(getStyle(obj, attr))*100);//这里加parseInt是避免div的数值不稳定,在波动
           }
           else
          {
             iCur=parseInt(getStyle(obj, attr));
          }
           var iSpeed=(json[attr]-iCur)/8;
           iSpeed=iSpeed>0&#63;Math.ceil(iSpeed):Math.floor(iSpeed);
          if(iCur!=json[attr])
          {
              stop=false;
          }
          if(attr=='opacity')
             {
              obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
              obj.style.opacity=(iCur+iSpeed)/100;
            }
          else
            {
               obj.style[attr]=iCur+iSpeed+'px';
            }
         
    }
    if(stop)
    {
     clearInterval(obj.timer);
     if(fn){fn();}
    }
   }, 30)
    
}

The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

jQuery Check if Date is ValidjQuery Check if Date is ValidMar 01, 2025 am 08:51 AM

Simple JavaScript functions are used to check if a date is valid. function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //test var

jQuery get element padding/marginjQuery get element padding/marginMar 01, 2025 am 08:53 AM

This article discusses how to use jQuery to obtain and set the inner margin and margin values ​​of DOM elements, especially the specific locations of the outer margin and inner margins of the element. While it is possible to set the inner and outer margins of an element using CSS, getting accurate values ​​can be tricky. // set up $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); You might think this code is

10 jQuery Accordions Tabs10 jQuery Accordions TabsMar 01, 2025 am 01:34 AM

This article explores ten exceptional jQuery tabs and accordions. The key difference between tabs and accordions lies in how their content panels are displayed and hidden. Let's delve into these ten examples. Related articles: 10 jQuery Tab Plugins

10 Worth Checking Out jQuery Plugins10 Worth Checking Out jQuery PluginsMar 01, 2025 am 01:29 AM

Discover ten exceptional jQuery plugins to elevate your website's dynamism and visual appeal! This curated collection offers diverse functionalities, from image animation to interactive galleries. Let's explore these powerful tools: Related Posts: 1

HTTP Debugging with Node and http-consoleHTTP Debugging with Node and http-consoleMar 01, 2025 am 01:37 AM

http-console is a Node module that gives you a command-line interface for executing HTTP commands. It’s great for debugging and seeing exactly what is going on with your HTTP requests, regardless of whether they’re made against a web server, web serv

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

jquery add scrollbar to divjquery add scrollbar to divMar 01, 2025 am 01:30 AM

The following jQuery code snippet can be used to add scrollbars when the div content exceeds the container element area. (No demonstration, please copy it directly to Firebug) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor