简体   繁体   English

手风琴代码无法正常工作,如何解决?

[英]accordion code not working properly, how to fix it?

I am trying to make my own accordion for my portfolio page template, for my website. 我正在尝试为我的网站的投资组合页面模板制作自己的手风琴。 xtractweb.com as once any one click the accodion respective work-snippet will be shown etc ... for now i am trying with this demo but i stuck in middle. xtractweb.com,因为任何人单击accodion都会显示相应的工作片段,等等...现在,我正在尝试使用此演示,但我陷入了中间。 The code i am currently using is given below: Html code snippet: 我当前使用的代码如下:HTML代码段:

<div class="accordion-content">
   <ul>
     <li class="current">
       <h3>Donec at neque eget lacus lobortis molestie</h3>
       <div class="content current">
           <p>Nulla risus orci, viverra nec lacinia at, aliquam vel justo. Phasellus felis purus, placerat vel augue vitae, aliquam tincidunt dolor. Sed hendrerit diam in mattis mollis. Donec ut tincidunt magna. </p>
       </div>
     </li>
     <li class="">
       <h3>Phasellus felis purus, placerat vel augue vitae</h3>
       <div class="content" style="display: none;">
          <p>Nulla risus orci, viverra nec lacinia at, aliquam vel justo. Phasellus felis purus, placerat vel augue vitae, aliquam tincidunt dolor. Sed hendrerit diam in mattis mollis. Donec ut tincidunt magna. </p>
       </div>
     </li>
 </ul>
</div>

and here is the jquery code: 这是jQuery代码:

$('.accordion-content ul li h3').click(function(){
    var parent = $(this).parent('li');
    if(parent.hasClass('current')){
        $(this).next('div').slideUp();
        parent.removeClass('current');
        //$(this).parent('li').children('.content').slideUp();
    }
    else
    {
        parent.siblings().children('div').stop(true,true).slideUp();
        parent.addClass('current');
        parent.siblings().removeClass('current');
        $(this).next('div').slideDown();
        $(this).parent('li').addClass('current');
        //$(this).parent('li').children('.content').slideDown();
    }
});

while all my css showing the work good ... the problem is that once i click the h3 it shows the div content, but once i click another h3 quickly than old div shown, it destroys the whole process and results not shows as i want... any one suggest me what to do now or any easier method than that ... ? 虽然我所有的css都显示工作良好...问题是,一旦我单击h3,它就会显示div内容,但是一旦我单击比显示的旧div更快的另一个h3,它就会破坏整个过程,并且结果不如我所愿...有人建议我现在该怎么办,或者比这更简单的方法...? here is the css code snippet: 这是css代码段:

.accordion-content {
    li {
      margin-bottom: 10px;
      border: 1px solid #dedede;
      background: #ececec url("../images/plus-minus.png") no-repeat;
      background-position: 96% 45%;
      background-width: 8px;
      background-height: 10px ;
      &.current {
        background: #ececec url("../images/minus.png") no-repeat;
        background-position: 96% 11%;
      }
    }
    h3 {
      font-size: 16px;
      font-family: 'open sans', sans-serif;
      text-align: left;
      color: #2c2725;
      padding: 10px 0 10px 20px;
      &:hover {
        cursor: pointer;
      }
    }
    .content {
      width: 100%;
      background: #ffffff;
      padding: 10px 0 10px 0;
      text-align: center;
      display: none;
      text-align: left;
      padding: 15px;
      padding-bottom: 55px;
      p {
        font-size: 14px;
        line-height: 24px;
        color: #7f8281;
      }
    }
    .current {
      display: block;
    }
  }

Regards 问候

Here is the might help you. 这可能会帮助您。

jQuery Code jQuery代码

$('.accordion-content').find('h3').click(function(){
    $('.content').slideUp(); // Hide all content.
    $(this).next().slideDown(); // show current clicked heading detail
    $(this).parent().addClass('current').siblings('li').removeClass('current');  // add current class to current clicked heading and remove from another heading.
});

Fiddle Demo 小提琴演示

the problem is not there as i fell, just look at the code snippet in jsfidle http://jsfiddle.net/gkQ3Y/1/ your results looking cool. 问题不存在,因为我跌倒了,只是看看jsfidle http://jsfiddle.net/gkQ3Y/1/中的代码片段,您的结果看起来很酷。 what else you think you want ? 您还想要什么? the only change which i feel is that, you should add 我感到的唯一变化是,您应该添加

.stop(true,true)

to make your accordion load properly, as you just clicks early than it loads and results looks different than you expect. 使您的手风琴正确加载,因为您单击的时间比加载的时间早,结果看起来与您预期的有所不同。 so, your new code snippet will look like shown below: 因此,您的新代码段将如下所示:

 $('.accordion-wrapper .accordion-content ul li h3').click(function(){

    var parent = $(this).parent('li');
    if(parent.hasClass('current')){
        $(this).next('div').stop(true,true).slideUp();
        parent.removeClass('current');
        //$(this).parent('li').children('.content').slideUp();
    }
    else
    {
        parent.siblings().children('div').stop(true,true).slideUp();
        parent.addClass('current');
        parent.siblings().removeClass('current');
        $(this).next('div').stop(true,true).slideDown();
        $(this).parent('li').addClass('current');
        //$(this).parent('li').children('.content').slideDown();
    }
});

i hope it will be making every thing fine right now, and your whole code is enough to do the work, while just these two lines will do the rest of the work now. 我希望它可以使现在的一切变得很好,并且您的整个代码足以完成工作,而仅这两行现在就可以完成其余工作。 Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM