0

I am using a script called dropotron which creates a nice pulldown menu from making lists.

eg.

<ul class="menu">
<li>home</li>
<li>Categories
   <ul><li>foo</li><li>moo</li><li>too</li></ul>
</li>
</ul>

etc.

My problem is, that when I load a page, it takes a few seconds before the list turns into the menu how it should look and appears bad for the visitor.

My relevant code which includes the js,css etc. is:

<script type="text/javascript">
$(function() {
    $('#menu > ul').dropotron({
        mode: 'fade',
        globalOffsetY: 11,
        offsetY: -15
    });

});
</script>

Any suggestions on how I could speed it up, or make it not happen at all?

Thank you.

5
  • 1
    takes a few seconds 0_o. Potential can of worms. Who wants to start? Commented Oct 31, 2013 at 11:56
  • @IanBrindley is right. This is missing a lot of context. That, or you're running on 56k dial-up. Commented Oct 31, 2013 at 11:58
  • 1
    In your css file add #menu { display: none; } and in your script, after the dropotron() call, add $("#menu").show(); Commented Oct 31, 2013 at 11:59
  • This may help you, how to speed up but you may keep your ul hidden and on load, you may show and apply method. Commented Oct 31, 2013 at 12:00
  • 1
    Make a jsfiddle example to show us the problem in action and maybe we can help. Commented Oct 31, 2013 at 12:09

4 Answers 4

2

Simply avoid waiting until the DOM is ready to apply your plugin. Just put your plugin's initialization process right after your list's HTML in the body. That should speed it up.

<ul id="#my-menu" class="menu">
    <li>home</li>
    <li>Categories
    <ul><li>foo</li><li>moo</li><li>too</li></ul>
    </li>
</ul>

<script>
<!-- I changed the selector to target the element directly, but you can play with this. -->
$('#my-menu').dropotron({
    mode: 'fade',
    globalOffsetY: 11,
    offsetY: -15
});
</script>

You can also try to initially hide the list using inline styles and show it once the plugin has been correctly applied, however that will not necessary lead to a better user experience.

Note: Do not forget to minimize your JavaScript files and CSS files.

Sign up to request clarification or add additional context in comments.

6 Comments

$(document).ready() fires immediately once the DOM is parsed, it's not load and if you run any js code before DOM is parsed then it won't work.
@RCV, You do not have to wait until the DOM is completely parsed before manipulating it.
If, the ul is not in the DOM then $('#menu > ul') will throw error but you may put the script in the footer to make sure that, element is loaded but this is not the case here, IMO.
@RCV, I am not sure what you aren't understanding in the example I provided. <!-- YOUR LIST HTML HERE --> represents the list's HTML, then the script comes right after. The element will therefore exist in the DOM and can be manipulated.
Where did you mention that the script should be placed after the list ? Basically it stays in the header, are you telling to use <script> tags in the body or where ?
|
1

You seem to load the dropotron.js script after you load your html. To ensure the javascript is ready when the html is loaded you can put your <script src="../dropotron.js"></script> into the <head> part of your page. That way dropotron.js will be loaded before any html is parsed and rendered.

2 Comments

How do you come to that conclusion, or is it just a guess?
It's a guess based on the "takes a few seconds" statement.
0

Try with window.onload global event handler.

function load () {
 ......
}
window.onload = load;

Hope this works!

3 Comments

Can you add some explanation, why this answer works and what is OP doing wrong.
I just got the same issue when I tried to make parent div's height equal to its child image height with jquery. But there were tons of images with various of size. When I run my application locally everything worked great but on the host there was a bug, jquery couldn't change div's height corresponding to it's child's height, I thought that while browser tried to render images, jquery was loaded first. Then, I add $(document).ready() method. It also could not help because it was JQuery. Answer to your question is, because it is just JS and Other People doing wrong using jquery.
But inside load function you can write your code in jquery. It works! but I don't recommend
-2

try

<script type="text/javascript">
s(document).ready(function(){
$(function() {
    $('#menu > ul').dropotron({
        mode: 'fade',
        globalOffsetY: 11,
        offsetY: -15
    });

});
});

</script>

to ensure your document is fully loaded before running your function...

2 Comments

He already has a document ready handler in there. $(function() { }) is a document ready handler. You've just suggested wrapping it with another one.
$( document ).ready() and $(function() {}) is the same, see: learn.jquery.com/using-jquery-core/document-ready

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.