以下是我在“Houdini”脚本(http://cferdinandi.github.io/houdini/)中实现这一点的方法:
JavaScript:
(function($) {
$(function () {
$('.collapse-toggle').click(function(e) { // When a link or button with the .collapse-toggle class is clicked
e.preventDefault(); // Prevent the default action from occurring
var toggle = $(this);
var dataID = toggle.attr('data-target'); // Get the ID of the target element
toggle.toggleClass('active'); // Add or remove the '.active' class from the toggle element
$(dataID).toggleClass('active'); // Add or remove the '.active' class from the target element
});
});
})(jQuery);
CSS:
/* Force pointer on button */
.collapse-toggle {
cursor: pointer;
}
/* When expanded, hide "show" text.
* When collapsed, hide "hide" text. */
.active .collapse-text-show,
.collapse-text-hide {
display: none;
visibility: hidden;
}
/* When expanded, show "hide" text. */
.active .collapse-text-hide {
display: inline;
visibility: visible;
}
/* Hide the collapsed element */
.collapse {
max-height: 0;
overflow: hidden;
opacity: 0;
/* Add animation when content expands and collapses */
-webkit-transition: opacity 0.35s ease;
-moz-transition: opacity 0.35s ease;
-ms-transition: opacity 0.35s ease;
-o-transition: opacity 0.35s ease;
transition: opacity 0.35s ease;
}
/* When collapsed element has the .active class, show it
* Uses max-height instead of display: none to allow for
* CSS3 animations, which don't work on display values. */
.collapse.active {
max-height: 9999em;
opacity: 1;
}
HTML(基于您上面的代码):
<button class="collapse-toggle" data-target="#menu_jquery">
<span class="collapse-text-show">Show Menu</span>
<span class="collapse-text-hide">Hide Menu</span>
</button>
<div class="collapse" id="menu_jquery">
<?php #wp_page_menu( 'sort_column=menu_order' ); ?>
</div>