views:

191

answers:

2

So I'm trying to adapt this Dropdown menu on Joomla the styles work great as expected so I'll post the javascript includes on the head of my website:

            <script type='text/javascript' src='js/jquery.js'></script>
                <script type='text/javascript' src='js/dropdown.js'></script>

                <script type='text/javascript'>
                  $(function() {
                    $('.menu').droppy();
                  });
                </script>

                <script type='text/javascript'>
                  $(function() {
                    $('.menu').droppy({speed: 100});
                  });
                </script>

ok I don't know why its is not working I'll post the dropdown.js should I post the jQuery too? it's really big!

                $.fn.droppy = function(options) {

                  options = $.extend({speed: 250}, options || {});

                  this.each(function() {

                    var root = this, zIndex = 1000;

                    function getSubnav(ele) {
                      if (ele.nodeName.toLowerCase() == 'li') {
                        var subnav = $('> ul', ele);
                        return subnav.length ? subnav[0] : null;
                      } else {
                        return ele;
                      }
                    }

                    function getActuator(ele) {
                      if (ele.nodeName.toLowerCase() == 'ul') {
                        return $(ele).parents('li')[0];
                      } else {
                        return ele;
                      }
                    }

                    function hide() {
                      var subnav = getSubnav(this);
                      if (!subnav) return;
                      $.data(subnav, 'cancelHide', false);
                      setTimeout(function() {
                        if (!$.data(subnav, 'cancelHide')) {
                          $(subnav).slideUp(options.speed);
                        }
                      }, 500);
                    }

                    function show() {
                      var subnav = getSubnav(this);
                      if (!subnav) return;
                      $.data(subnav, 'cancelHide', true);
                      $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
                      if (this.nodeName.toLowerCase() == 'ul') {
                        var li = getActuator(this);
                        $(li).addClass('hover');
                        $('> a', li).addClass('hover');
                      }
                    }

                    $('ul, li', this).hover(show, hide);
                    $('li', this).hover(
                      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
                      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
                    );

                  });

                };

My question here is: Why is it not working! I know that this is really complex (I don't anything about JavaScript) but if you help me I'll post a tutorial and edited files that will help a lot of people!

By the way I've download jQuery from the original site so I don't think that this can be the problem!

Thanks in advance!

Here is the HTML generated from 2 levels til the UL:

            <div id="topmenu">  
            <div class="moduletabledropdown">
            <ul class="menu">
            <li id="current" class="first level0 home active"><a href="http://www.mundosoftware.com.br/templatex/" title="Home"><span>Home</span></a></li>
            <li class="level0 parent faq"><a href="/templatex/index.php?option=com_content&amp;view=section&amp;id=3&amp;Itemid=41" title="FAQ"><span>FAQ</span></a><ul class="level1">
            <li class="first last level1 item-01"><a href="http://www.google.com" title="Item 01"><span>Item 01</span></a></li></ul></li>
            <li class="level0 parent the-news"><a href="/templatex/index.php?option=com_content&amp;view=category&amp;layout=blog&amp;id=1&amp;Itemid=50" title="The News"><span>The News</span></a><ul class="level1"><li class="first last level1 item-02"><a href="http://www.google.com" title="Item 02"><span>Item 02</span></a></li></ul></li>
            <li class="level0 web-links"><a href="/templatex/index.php?option=com_weblinks&amp;view=categories&amp;Itemid=48" title="Web Links"><span>Web Links</span></a></li><li class="last level0 parent news-feeds"><a href="/templatex/index.php?option=com_newsfeeds&amp;view=categories&amp;Itemid=49" title="News Feeds"><span>News Feeds</span></a><ul class="level1"><li class="first last level1 item-03"><a href="http://www.google.com" title="Item 03"><span>Item 03</span></a></li></ul></li></ul></div>

Here is the HTML for a page that does work pure HTML CSS:

                <html>
                  <head>
                    <title>droppy - Nested drop down menus</title>
                    <meta name="description" content="" />
                    <meta name="keywords" content="" />
                      <script type='text/javascript' src='assets/jquery.js'></script>
                    <link rel="stylesheet" href="assets/project-page.css" type="text/css" />

                    <!-- per Project stuff -->
                      <script type='text/javascript' src='javascripts/jquery.droppy.js'></script>
                      <link rel="stylesheet" href="stylesheets/droppy.css" type="text/css" />
                    <!-- END per project stuff -->

                  </head>
                  <body>
                    <div id='container'>
                      <h1>
                        droppy
                        <span class='subtitle'> - Nested drop down menus</span>
                      </h1>

                      <div id='sidebar'>
                        <ul id='project-nav'>
                          <li><a href='#overview'>Overview</a></li>
                          <li><a href='#example'>Example</a></li>
                          <li><a href='#usage'>Usage</a></li>
                          <li><a href='#download'>Download</a></li>
                          <li><a href='#known-issues'>Known Issues</a></li>
                        </ul>
                        <ul id='ohoa-nav'>
                          <li><a href='http://onehackoranother.com/projects/'&gt;Back to projects &raquo;</a></li>
                          <li><a href='http://onehackoranother.com/'&gt;Back to onehackoranother.com &raquo;</a></li>
                        </ul>
                        <a href='http://thepixeltrap.com' id='pixel-trap' title='The Pixel Trap: New Directory for Web Professionals'>
                              <img src='http://onehackoranother.com/images/pixel-16.png' alt='' /> The Pixel Trap - A New Directory for Web Professionals
                            </a>
                      </div>

                      <div id='main'>

                <h2 class='first' id='overview'>Overview</h2>

                <p>Quick and dirty nested drop-down menu in the jQuery styleee. I needed a nav like
                  this for a recent project and a quick Googling turned up nothing that really suited,
                  so droppy was born. It hasn't been designed with flexibility in mind - if you like
                  what you see, great, integration should be a breeze - otherwise I'd look for something
                  more configurable elsewhere.</p>

                <h2 id='example'>Example</h2>

                <ul id='nav'>
                  <li><a href='#'>Top level 1</a></li>
                  <li><a href='#'>Top level 2</a>
                    <ul>
                      <li><a href='#'>Sub 2 - 1</a></li>
                      <li>
                        <a href='#'>Sub 2 - 2</a>
                        <ul>
                          <li>
                            <a href='#'>Sub 2 - 2 - 1</a>
                            <ul>
                              <li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
                              <li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
                              <li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
                              <li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
                            </ul>
                          </li>
                          <li><a href='#'>Sub 2 - 2 - 2</a></li>
                          <li>
                            <a href='#'>Sub 2 - 2 - 3</a>
                            <ul>
                              <li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
                              <li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
                              <li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
                              <li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
                            </ul>
                          </li>
                        </ul>
                      </li>
                      <li><a href='#'>Sub 2 - 3</a></li>
                    </ul>
                  </li>
                </ul>

                <script type='text/javascript'>
                  $(function() {
                    $('#nav').droppy();
                  });
                </script>

                <h2 id='usage'>Usage</h2>

                <p>No mandatory configuration options or nothin' here, just use include the Javascript/CSS
                  resources and insert the following code in your document head, or any other
                  suitable place:</p>

                <div class='caption'>Javascript:</div>
                <pre>&lt;script type='text/javascript'&gt;
                  $(function() {
                    $('#nav').droppy();
                  });
                &lt;/script&gt;</pre>

Don't mind unclosed divs that is not the full code!

+1  A: 

Since your <script> block is in the <head> tag, it executes before the page body is loaded, when there is no .menu element

You need to move your <script> block to the end of the <body> tag, or wrap it in $(function() { ... });.

SLaks
That was quick I'll try that and get back to you! thank you!!!
Evilalan
I put all the <script></script> right before the <body> it give the sabe result I'm really sorry about asking but how would I wrap it with a $(function() { ... }); ? in other words how would it look like!I really need to learn Javascript
Evilalan
You need to put it at the _end_ of the `<body>` element, write before `</body>`.
SLaks
You would have to put it right before the closing body tag, not the opening body tag. But, instead of that, I would probably create an initialization method and add an "onload" method like:$(document).ready(function(){ // Your code here...});and initialize your drop-down list.
El Guapo
use of the $(function(){}) will hold execution until the window is loaded.
Tracker1
Why was this downvoted?
SLaks
You need to put it at the end of the <body> element, write before </body>, that is what I did! I can't do $(document).ready(function(){ // Your code here... }); because it's a CMS (joomla), if I load inside there it will load a div before the ul li ul il
Evilalan
voted up so you don't negative points...
Evilalan
+1  A: 

Can you post some of your drop down menu HTML?

Right now you are applying the droppy plugin to the selector '.menu', which looks for an element on your page that has the CSS class 'menu', is that what you intended? If you want to apply the dropdown to an element with id="menu" you would use $('#menu').

Update

I would recommend using superfish instead, the mechanics of drop down menus are really hard to get right to make the menus easy to use. Superfish does an excellent job of being very forgiving for the site visitors.

I can't find anything particularly wrong with the code, seems to work ok. The only change I had to do to get it to work was to initially set the submenus to hidden using CSS. Try this out, you'll need to add your CSS back into the HTML. I would suggest using Firefox, Firebug and Firequery. This will give you insight into what exactly is breaking if there are any JavaScript errors.

<html>
<head>
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript" src="javascripts/jquery.droppy.js"></script>
    <style>
        ul#nav li ul {
            display: none;
        }
    </style>
</head>
<body>
    <h2 id='example'>
        Example</h2>
    <ul id='nav'>
        <li><a href='#'>Top level 1</a></li>
        <li><a href='#'>Top level 2</a>
            <ul>
                <li><a href='#'>Sub 2 - 1</a></li>
                <li><a href='#'>Sub 2 - 2</a>
                    <ul>
                        <li><a href='#'>Sub 2 - 2 - 1</a>
                            <ul>
                                <li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
                                <li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
                                <li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
                                <li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
                            </ul>
                        </li>
                        <li><a href='#'>Sub 2 - 2 - 2</a></li>
                        <li><a href='#'>Sub 2 - 2 - 3</a>
                            <ul>
                                <li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
                                <li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
                                <li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
                                <li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li><a href='#'>Sub 2 - 3</a></li>
            </ul>
        </li>
    </ul>

    <script>
        $(function() {
            $('#nav').droppy();
        });
    </script>

</body>
</html>
DavGarcia
I just updated the "post" with the generated HTML. Thank you very much for helping me out!
Evilalan
I using superfish now but a little bit of PHP was missing... Something like that:'<?php echo $this->baseurl ;?>/templates/<?php echo $this->template ;?>' thanks
Evilalan
It's 100% valid and working on IE6 by the time I'm writing this down! Really neat piece of jQuery! since I've loaded jQuery I can't resist to use and style a few more 1kb plugins this is awesome! I can implement JS solutions without knowing JS! I'll learn it tho!
Evilalan
So that is why YooTheme use jQuery so often! haha I'll create a post explaining how to create a easy to use DropDown for joomla in a few weeks!
Evilalan