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...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

How to replace plain URLs with links

Here we will explain how to replace Urls with links from string Using PHP $string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.  CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg   CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be'; $string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string); Using Javascript <script> function linkify(inputText) {     var replacedText, replacePattern1, replacePattern2, replacePattern3;     //URLs starting with http://, https://, or ftp://     replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;     replacedText = inputT...