Skip to main content

jQuery Slidetoggle examples

1.  Simplest Method
<script>
$(document).ready(function(){
  $("button.demo").click(function(){
    $(this).parent().find('p').slideToggle("slow");
  });
});
</script>
HTML
<div class="main">
<button class="demo">Toggle slideUp() and slideDown()</button>
<p>This is a paragraph.</p>
</div>
<div class="main">
<button class="demo">Toggle slideUp() and slideDown()</button>
<p>This is a paragraph.</p>
</div>
<div class="main">
<button class="demo">Toggle slideUp() and slideDown()</button>
<p>This is a paragraph.</p>
</div>


2. Second example
<script type="text/javascript">
jQuery(document).ready(function($) {
    // Find the toggles and hide their content
    $('.toggle').each(function(){
        $(this).find('.toggle-content').hide();
    });
    // When a toggle is clicked (activated) show their content
    $('.toggle a.toggle-trigger').click(function(){
        var el = $(this), parent = el.closest('.toggle');
        if( el.hasClass('active') )
        {
            parent.find('.toggle-content').slideToggle();
            el.removeClass('active');
        }
        else
        {
            parent.find('.toggle-content').slideToggle();
            el.addClass('active');
        }
        return false;
    });
});  //End

<!-- Toggle CSS --> 
<style type="text/css">   
.toggle {    
font-size: 13px;
    line-height:20px;    
margin-bottom: 10px;  
border: 1px solid #e5e5e5;  
-webkit-border-radius: 5px;    
-moz-border-radius: 5px;          
border-radius: 5px;  
}
.toggle a.toggle-trigger {  
display:block;  
padding: 10px 20px 15px 20px;  
position:relative;  
text-decoration: none;  
color: #666;
}
.toggle a.toggle-trigger:hover {  
opacity: .8;  
text-decoration: none;
}
.toggle a.active {  
text-decoration: none;  
border-bottom: 1px solid #e5e5e5;  
-webkit-box-shadow: 0 8px 6px -6px #ccc;    
-moz-box-shadow: 0 8px 6px -6px #ccc;          
box-shadow: 0 8px 6px -6px #ccc;  
color: #000;
}
.toggle a.toggle-trigger:before {  
content: "-";  
margin-right: 10px;  
font-size: 1.3em;  
}
.toggle a.active.toggle-trigger:before {  
content: "+";
}
.toggle .toggle-content {  
padding: 10px 20px 15px 20px;  
color:#666;
}
</style>

Html part
<div class="toggle">
    <a href="#" title="Title of Toggle" class="toggle-trigger">This is testing</a>
    <div class="toggle-content">
        <p>This is testing done by sudhir pandey.You can also implement it.</p>
    </div>
</div>
<div class="toggle">
    <a href="#" title="Title of Toggle" class="toggle-trigger">This is my second testing</a>
    <div class="toggle-content">
        <p>This is my second testing text.</p>
    </div>
</div>

Example 3: SlideUp and SlideDown 
// Open Current active div only and hide other

HTML
<ul id="toggle-view">
<li>
<div class="panelCollapse">
<h3>Panel 1</h3>
<span>+</span>
</div>
<div class="panel">
<div class="panel-body">
This is panel 1 body.
</div>
</div>
</li>
<li>
<div class="panelCollapse">
<h3>Panel 2</h3>
<span>+</span>
</div>
<div class="panel">
<div class="panel-body">
This is panel 2 body.
</div>
</div>
</li>
<li>
<div class="panelCollapse">
<h3>Panel 3</h3>
<span>+</span>
</div>
<div class="panel">
<div class="panel-body">
This is panel 3 body.
</div>
</div>
</li>
</ul>

CSS
#toggle-view {
list-style:none;
margin:0;
padding:0;
width:100%;
}
#toggle-view li {
margin:5px 0px;
border-bottom:1px solid #ccc;
position:relative;
cursor:pointer;
}
#toggle-view h3 {
margin:0;
font-size:1.3em;
background:#f3f3f3;
color:#333;
text-transform:uppercase;
padding:14px 12px;
}
.panelCollapse{
border-left:1px solid #ccc;
border-top:1px solid #ccc;
border-right:1px solid #ccc;
}
.panelCollapse span {
background-color: #0099da;
    color: #fff;
    content: "−";
    float: right;
    font-family: "FontAwesome";
    height: auto;
    margin-right: -15px;
    margin-top: -5px;
    padding:14px 0;
    text-align: center;
    width: 33px;
position:absolute;
top:6px;
right:15px;
font-size:24px;
}
#toggle-view .panel {
margin:0px;
display:none;
border:1px solid #cecccc;
}

javascript
<script>
$(document).ready(function(){
$('#toggle-view .panelCollapse').click(function () {
var text = $(this).parent().children('div.panel');
if (text.is(':hidden')) {
text.slideDown('200');
$(this).children('span').html('-');
} else {
text.slideUp('200');
$(this).children('span').html('+');
}
$(this).parent().siblings().children('div.panel').slideUp('200');
$(this).parent().siblings().children('div.panelCollapse').find('span').html('+');
});
});
</script>

Comments

Popular posts from this blog

Generate XML file in Cakephp

Steps to Generate XML file using CakePHP: Step-1 Enable to parse xml extension in config route.php file.     Router::parseExtensions('xml'); Step-2 Add Request Handler Component to the Controller    var $components = array(‘RequestHandler’); Step-3 Add controller Action For XML Generation in Post Controller     function generateXMLFile()     {         if ($this->RequestHandler->isXml()) { // check request type             $this->layout = 'empty'; // create an empty layout in app/views/layouts/empty.ctp              }        }  Add header code in empty layout <?php header('Content-type: text/xml');?> <?php echo $this->Xml->header(); ?> <?php echo $content_for_layout; ?> Step-4 Set up View To generate XML Create xml folder inside Posts vi...

Simple JavaScript Object and Class Examples

In JavaScript, most things are objects. An object is a collection of related data and/or functionality Namespace: Everything you create in JavaScript is by default global. In JavaScript namespace is Global object . How to create a namespace in JavaScript? Create one global object, and all variables, methods, and functions become properties of that object. Example:  // global namespace var MYAPP = MYAPP || {}; Explaination: Here we first checked whether MYAPP is already defined.If yes, then use the existing MYAPP global object, otherwise create an empty object called MYAPP How to create sub namespaces? // sub namespace MYAPP.event = {}; Class JavaScript is a prototype-based language and contains no class statement.JavaScript classes create simple objects and deal with inheritance. Example: var Person = function () {}; Explaination: Here we define a new class called Person with an empty constructor. Use the class keyword class Calculation {}; A class expr...

How to Add Next Previous links to The Event Calendar

Add Next/Previous links to The Event Calendar Wordpress Plugin Add code to your child theme’s functions.php file /**  * Allows visitors to page forward/backwards in any direction within month view */ if ( class_exists( 'Tribe__Events__Main' ) ) { class ContinualMonthViewPagination {     public function __construct() {         add_filter( 'tribe_events_the_next_month_link', array( $this, 'next_month' ) );         add_filter( 'tribe_events_the_previous_month_link', array( $this, 'previous_month' ) );     }     public function next_month() {         $url = tribe_get_next_month_link();         $text = tribe_get_next_month_text();         $date = Tribe__Events__Main::instance()->nextMonth( tribe_get_month_view_date() );         return '<a data-month="' . $date . '" href="' . $url . '" rel="next"...