Hide and Slide Drupal content with jQuery

The Drupal core and many modules use the jQuery package to create nice interactive effects.
But it's also a handy and easy to use tool for designers and authors!

Here is a quick example to show/hide a div with a nice slide effect, triggered from an adjoining element. If you know your CSS you can do all sort of tricks with this.

  1. <p class="toggler">Be snoopy...</p>
  2. <div>
  3. Hey, this is secret! Please click again to help me hide...
  4. </div>

The paragraph "p" is the trigger, this is marked with a class of "toggler".
The immediately following div is hidden on page load and display when you click the "toggler". Here is the jQuery code:

  1. $(".toggler").click(function(){
  2. $(this).next().slideToggle("slow");
  3. return false;
  4. }).next().hide();

I.e. each "toggler" gets a click function and the next element is hidden.
The function itself slides that next element up and down on click.

Stick this in the page load function and you are ready to go!

Quick example: Create a new node, switch the Input Format to Full HTML and put this in the body section:

  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $(".toggler").click(function(){
  4. $(this).next().slideToggle("slow");
  5. return false;
  6. }).next().hide();
  7. });
  8. </script>
  9.  
  10. <p class="toggler">Be snoopy...</p>
  11. <div>
  12. Hey, this is secret! Please click again to help me hide...
  13. </div>

Please note that you need jQuery enabled. This will likely be the case but it could be disabled in some themes.

Comments

Added quick example

Added a quick example to get you started. See the last paragraph.

Could you please make it more

Could you please make it more specific, I don't quiet understand what the steps should be:)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can use BBCode tags in the text.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options