简体   繁体   中英

Meteor Live Accordion Menu

I'm creating a Social-Media style site and using Meteor & MongoDB I create posts, with {{title}} and {{content}} Inside of an accordion menu with title being on the title of the menu, and the content being in the toggling Accordion Menu.

My code looks like this:

HTML:

<template name="postsList">

{{#each posts}}



<div class="accordion">
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-1">{{title}}</a>

    <div id="accordion-1" class="accordion-section-content">
        <p>{{content}}</p>
    </div><!--end .accordion-section-content-->
</div><!--end .accordion-section-->
</div><!--end .accordion-->


{{/each}}

</template>

JS:

    function close_accordion_section() {
    $('.accordion .accordion-section-title').removeClass('active');
    $('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}

$('.accordion-section-title').click(function(e) {
    // Grab current anchor value
    var currentAttrValue = $(this).attr('href');

    if($(e.target).is('.active')) {
        close_accordion_section();
    }else {
        close_accordion_section();

        // Add active class to section title
        $(this).addClass('active');
        // Open up the hidden content panel
        $('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
    }

    e.preventDefault();
});

CSS:

/*----- Accordion -----*/
.accordion, .accordion * {
-webkit-box-sizing:border-box; 
-moz-box-sizing:border-box; 
box-sizing:border-box;
}

.accordion {
overflow:hidden;
box-shadow:0px 1px 3px rgba(0,0,0,0.25);
border-radius:3px;
background:#f7f7f7;
}

/*----- Section Titles -----*/
.accordion-section-title {
width:100%;
padding:15px;
display:inline-block;
border-bottom:1px solid #1a1a1a;
background:#333;
transition:all linear 0.15s;
/* Type */
font-size:1.200em;
text-shadow:0px 1px 0px #1a1a1a;
color:#fff;
}

.accordion-section-title.active, .accordion-section-title:hover {
background:#4c4c4c;
/* Type */
text-decoration:none;
}

.accordion-section:last-child .accordion-section-title {
border-bottom:none;
}

/*----- Section Content -----*/
.accordion-section-content {
padding:15px;
display:none;
}

But While the {{title}} is displayed correctly, when you click it (should open to the {{content}} ) It doesn't, despite the content being correct in the MongoDB database, why is this happening?

Any Help would be appreciated!

Semantic UI Code:

$(document).ready(function(){   
$('.ui.accordion')
.accordion();
});

 <template name="postsList">

 {{#each posts}}

<div class="ui accordion">
  <div class="active title">
 <i class="dropdown icon"></i>
 {{title}}
 </div>
 <div class="content">
  {{content}}
  </div>
  </div>
{{/each}}

  </template>

The javascript should be in events block

Template.postlist.events({
      "click .accordion-section-title" : function(){
          // your code here

      } 


 })

See this example Meteor Todo Tutorial

Alternatively, use twbs:bootstrap or semantic:ui packages and use their inbuilt functions for accordion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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