Create accordion menu with jQuery [I want to start with any one open]

Asked 1 years ago, Updated 1 years ago, 104 views

When creating an accordion menu using jQuery,
How do I code to start any one of them open from scratch?

The following coding is used:

[html]

<divid="side">
  <divid="recent-posts-3" class="widget widget_recent_entries">
    <div class="side_d">
      <div class="side_box">
        <h3>FY2017</h3>
        <ul>
          <li>Menu</li>
          <li>Menu</li>
          <li>Menu</li>
          <li>Menu</li>
          <li>Menu</li>
          <li>Menu</li>
        </ul>
      </div>
    </div>
  </div>
  <divid="category-4" class="widgetwidget_category">
    <div class="side_d">
      <div class="side_box">
        <h3>Categories</h3>
        <ul>
          <li>Category 1</li>
          <li>Category 2</li>
          <li>Category 3</li>
          <li>Category 4</li>
        </ul>
      </div>
    </div>
  </div>
  <divid="meta-2" class="widgetwidget_meta">
    <div class="side_d">
      <div class="side_box">
        <h3>Meta Information </h3>
        <ul>
          <li>Login</li>
          <li>RSS</li>
          <li>WordPress.org</li>
          <li>---------------/li>
        </ul>
      </div>
    </div>
  </div>
</div>

[css]

#side{
  width: 100%;
  padding —5px5px5px10px;
  margin —0px0000;
  background-color:#F0F0F0;
}

#side.widgetdiv.side_box{
  margin —5px05px0;
}

#side.widgetdiv.side_boxh3{
  width: 99%;
  margin —1px1px0px1px;
  padding —10px0 10px20px;
  line-height —25px;
  color:#FFFFFF;
  font-size: 16px;
  background-color:#88888;
  box-sizing: border-box;
}

#side.widget div.side_box h3.wp-according{
  background-color:#999;
}

/* arrow*/
# side.widget div.side_box h3.arrow {
  background-color:#BBBBBB;
  cursor —pointer;
}

# side.widget div.side_box h3.opend {
  cursor:default;
}

/* active*/
# side.widget div.side_box h3.active {
  background-color:#FF8856;
  color:#fff;
}

/* hovered*/
# side.widget div.side_box h3.hovered {
  background-color:#DAA520;
}

#side.widgetdiv.side_boxul{
  display:block;
  list-style: none;
  width —auto;
  height —100%;
  margin —0 10px0 1.8px;
  padding —00px010px;
  background-color:#FFFFFF;
}

# side.widget div.side_box ulli {
  list-style: none;
  width —96%;
  height —16px;
  margin-left —5px;
  padding —10px0 10px35px;
  border-bottom:1px#EEEEEEE solid;
  line-height —16px;
  font-size: 15px;
  color:#55555;
  position:relative;
}

# side.widget div.side_box ulli:before{
  counter-increment:list;
  content: ";
  display:block;
  position:absolute;
  left —2px;
  height —15px;
  width —15px;
  border-radius: 50%;
  background: #F6A38B;
  top: 50%;
  -moz-transform:translateY (-50%);
  - webkit-transform:translateY (-50%);
  -o-transform:translateY (-50%);
  -ms-transform:translateY (-50%);
  transform —translateY (-50%);
}

# side.widget div.side_box ulli: after {
  content: ";
  display:block;
  position:absolute;
  left —8px;
  height:0;
  width:0;
  border-top —4px solid transparent;
  border-bottom —4px solid transparent;
  border-left —5px solid#fff;
  top: 50%;
  -moz-transform:translateY (-50%);
  - webkit-transform:translateY (-50%);
  -o-transform:translateY (-50%);
  -ms-transform:translateY (-50%);
  transform —translateY (-50%);
}

#side.widget div.side_boxulia {
  text-decoration: none;
  color:#666;
}

[jQuery]

(function($){
  $(function(){
    // Speed
    var slide_speed = 400;
    varkeep_openItem;

    // Add MENU block designated class name
    varaccord=$('#recent-posts-3, #category-4, #meta-2').find('h3').addClass('wp-according');

    varaccord_itme=$('.wp-accordion');
    record_itme.css({'cursor':'pointer'});
    record_itme.next().hide();

    // click-action
    $('.wp-accordion').click(function(){
      // hit decision class
      $(this).toggleClass("active");
      var slideItem=$(this).next();

      // hit branching
      if($(this).hasClass('active')){
        // Close first if open
        if(keep_openItem){
          keep_openItem.slideUp(slide_speed, function(){
            // active class switching
            keep_openItem.prev().removeClass("active");
          });
        }

        // Open
        slideItem.slideToggle(slide_speed, function(){
          // Save openItem
          keep_openItem = slideItem;
        });
      }

      if(!$(this).hasClass('active'))){
        // Close
        slideItem.slideToggle(slide_speed);
        // Clear openItem
        keep_openItem=null;
      }
    });
  });
}) (jQuery)

jquery

2022-09-30 21:27

1 Answers

accord_itme.next().hide(); is private, so
·Re-release
·Give active class to wp-accordion
·Replace the variable that holds the currently open item
It's possible if you do three things.
In the following example, we have made the first element public, but
Change to any selector.I think it's okay to specify an id or something.

(function($){
$(function(){

    // Speed
    var slide_speed = 400;
    varkeep_openItem;

    // Add MENU block designated class name
    varaccord=$('#recent-posts-3, #category-4, #meta-2').find('h3').addClass('wp-according');

    varaccord_itme=$('.wp-accordion');
    record_itme.css({'cursor':'pointer'});
    record_itme.next().hide();


    // Set the selector you want to specify.This time, I tried the first element as an example.
    var slide_opendefalt=$('.wp-according:first')
    slide_opendefalt.next().show(); // Re-publish
    grant active class to slide_opendefalt.addClass("active"); //wp-according
    keep_openItem=slide_opendefalt.next(); // Replace with the variable that holds the currently open item

    // click-action
    $('.wp-accordion').click(function(){

hereinafter abbreviated

It's a bit redundant because I re-publish what I made private.You can do it if you want to be smarter, but this time try not to change the original source as much as possible


2022-09-30 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.