What is a dynamic menu. We create a dynamic and animated menu. Writing code to create a dynamic menu in jQuery

Liked:
26



Didn't like: 12

In this article I will show you how to write a simple jQuery plugin that will build a nice looking HTML markup from regular HTML markup. dynamic menu. The first thing we will start with is to decide what exactly the plugin will do and what our goal is. So:

  • Let's call the plugin dynamenu (from English Dynamic Menu). A short, good name in the spirit of jQuery. The dynamics of the menu will be represented by changes in the “transparency” of menu items and changes in the position of the menu element when hovering with the mouse - the menu item will “move” slightly to the right.
  • We will set the menu structure statically in the form of HTML markup. This will be a sequential set of DIV elements. Inside each div there is a hyperlink with the name of the menu item. For each div we will set an attribute class="dynamenu" so that you can then access a set of divs and turn them into a menu.
  • To animate the menu we will use standard method jQuery - animate(). It is quite enough to implement the task of “dynamism”. With this method we will change the properties opacity And left at the moment you hover the mouse over an element and remove the mouse from the menu element.
  • Now let's determine which files need to be created to test the operation of the example in question. Here they are:

    2. jquery.dynamenu.js - plugin code placed in a separate file.

    3. dynamenu.css - the style of our menu. We put all the CSS here

    These files with a working menu can be found in the archive attached to the article. At the end of the article there is a live demonstration of how our menu works. Please note that in the demo, the plugin call is placed in the same place as the plugin code itself. This is only due to the fact that the live demo mechanism we use requires placing the JavaScript code in a separate frame.

    Let's start writing a plugin. Go!

    1. Cooking HTML markup pages

    Let's set the most simple markup for our menu. Let these be several menu items for our future website:

    Site news Photoshop tutorials Photoshop brushes Beautiful fonts

    Everything is simple here - 4 div elements, each with a dynamenu class, so that we can then pass it as a selector to our future plugin. Inside each div there is a hyperlink with the name of the menu item.

    2. Set CSS styles

    To style our menu, we use only two CSS styles. With their help, we will make the background of each menu item gray, the menu width 300 pixels with external and indentation 5 pixels each, and all menu links are black:

    a.mlink ( color:#000; text-decoration:none; ) .dynamenu ( background-color: #aaaaaa; width:300px; margin:5px; padding:5px; font-family: "Tahoma"; font-size: 9pt)

    3. We write the “framework” of the plugin

    First of all, let's define the framework of our plugin. This framework is partially taken from the official jQuery website and contains the so-called "Best Practices" ( best practics and techniques - from the developers of the jQuery language). IN general case a similar frame is applicable to almost everyone jQuery plugins. If you remember and understand how this framework functions, then you will be able to write plugins in one go. It looks like this:

    (function($) ( // list of methods of our plugin var methods = ( init: function(options) ( // plugin initialization can be placed here. in our case // for simplicity there will be nothing here ), sliding: function () ( // the sliding method of our plugin will directly build the menu ) ); // “register” our plugin in the jQuery namespace $.fn.dynamenu = function(method) ( // here we call the desired method inside the plugin ) ) (jQuery) ;

    As you can see, there is nothing complicated in the frame. But he carries within himself useful information. First important point, which needs to be understood in the framework - we collect all the methods added to our plugin (init, sliding) into one object - methods. This allows you to avoid cluttering the $.fn namespace unnecessary functions. Correctly specifying the namespace of our plugin is very an important part the plugin development process itself. Using a namespace ensures that our plugin is less likely to be overwritten by other plugins or code located on the same HTML page. Namespaces also make life easier because... helps you keep better track of methods, events, and data.

    Always use method hiding inside the plugin, for example in a methods object. Besides the fact that it is good style programming, this technique will help you avoid conflicts with other third-party libraries and plugins, and will also allow you not to pollute the jQuery namespace

    The next point worth paying attention to is the designer of our plugin. This is the line $.fn.dynamenu = function (method) (...). As you can see, the constructor takes one parameter - method. As a value for the parameter, we will pass a string containing the name of the method inside the object methods, which we are going to call. Let's populate the constructor with the following code:

    $.fn.dynamenu = function(method) ( if (methods) ( methods.init.apply(this, arguments); return methods[ method].apply(this, Array.prototype.slice.call(arguments, 1)) ; ) else ( $.error("Method" + method + " does not exist!"); ) )

    Let's figure out what we did. First we check if a method with the name passed in the constructor parameter exists in the object methods our plugin. If there is a method, then we first call the init method to initialize our plugin, and then we call the method whose name was passed in the constructor parameter, and we pass all the remaining arguments to it. If there is no method with the same name, then we will issue an error and the plugin will stop working.

    On at this stage a lot has already been done! Now we can access our plugin and call its methods, although no useful work They haven't done it yet. I recommend putting our plugin code in a separate file and calling it jquery.dynamenu.js. Placing the plugin code in separate file is logical - after all, the plugin should be universal in its essence and allow us and other developers to connect it ready-made to their site.

    Now that our framework is filled with code that can call the plugin's internal methods, it's time to build out the "meat" - that is, write the code that will directly turn our div blocks into a beautiful dynamic menu. Let's get started...

    4. We write the code that creates a dynamic menu in jQuery

    All useful code of our plugin will be placed in the function sliding, which is internal method plugin and is located in the object methods. Let's briefly describe the sequence of steps to turn static markup with our div blocks into a dynamic menu in jQuery:

  • First, let's change the transparency of all div blocks, setting it to 0.4 points. This will make each menu item grayish. Then, when you hover the mouse over it, we will change the opacity to 1, thus creating the effect of highlighting the menu item
  • Next, we will create handlers for “entering” the mouse into the menu item area and “exiting” it. When you hover over a menu item, we will change the font style to bold and change the background color to a darker one. Also, using the standard jQuery animate method, we will create the effect of “pushing” the menu a little to the right. When the mouse leaves the menu item area, we will simply return all parameters to their initial state.
  • That, in fact, is the whole logic. Now let's see how to implement this:

    return this.each(function() ( $(this).css(("opacity":"0.4")); $(this).hover(function() ( $("a.mlink", this).css (("font-weight":"bold")); $(this).animate(( opacity:1, "margin-left":"+=5" ), 100, "linear"); ) ( $("a.mlink", this).css(("font-weight":"normal")); $(this).animate(( opacity:0.4, "margin-left":"-=5 " ), 100, "linear"); )); ));

    Just in case, I’ll explain what’s happening. So, in the very first line we see the operator. It does the following: it runs through what is passed to the function sliding set of elements (i.e. all our div blocks) and executes the code placed inside. After such a run through the elements, we return ( return statement) the result of performing operations for each element of the set, again, in the form of a set. Thus, our sliding function returns a set of div blocks passed to the “input” of the function, only processed and already turned into a dynamic menu. This implements an important jQuery concept - the ability to use our plugin in a call chain. To better understand what a call chain is, here's an example:

    $(".myelm").dynamenu("sliding").css(("border" : "1px solid red"));

    In the above piece of code we see what a chain of calls is: first we select all the elements on the page with the class myelm, then we use our dynamenu plugin and then again we use the standard method along the chain jQuery css() to change the style of elements. If we did not return the return this.each(function() ( ... )) construct from the method, then use css method() in the “chain” they would no longer be able to.

    Use the this.each() return operator to support chaining in your plugins and make them more versatile.

    Let's move on, inside the each() operator, where we run through all our div blocks, it goes exactly initial installation properties "transparency" (opacity) of the element in 0.4. point. The maximum opacity value is 1 (100%), so we set the "opacity" to 40%. After this, we set two handlers for “hovering” the mouse and “moving” the mouse out of the div block area. In the first handler we set the menu item title to bold and use animate method() to achieve “full opacity” of the menu item, and also shift it to the right by 5 pixels. In the mouse “leaving” handler, we simply return the element to its initial state - change the font again to normal and shift it to the left again by 5 pixels.

    That's all! Our plugin is ready to use. Although it is simple, it can be very useful for creating a dynamic menu on the site. Below is a demonstration of how our plugin works. To see the result of the plugin, go to the Result tab.

    Good luck with your writing good plugins! Comments, questions and feedback are always welcome;)

    You've probably seen dynamic and animated menus on some websites that change as you scroll down. By minimizing the main navigation menu, you leave more space for content. In this tutorial, we'll explain how you can create a menu yourself using HTML5, CSS3, and a little jQuery.

    If you want a special focus on the content of the site, as well as allow you to create a larger and more impressive navigation when a user first visits the website, then this type of menu is perfect for you. You can perfectly showcase your brand or logo, and after an initial look at the site, reduce some elements, allowing the user to focus on your content.

    There are several ways to do this. In this tutorial, we'll explain how to create a fixed, full-width menu that resizes in height along with your logo, creating a miniaturized version of the original. If desired, you can also replace the logo with another option, such as initials or an icon, but keep in mind that consistency is very important here so that the user understands how the element has changed and what its the main objective is still the site navigation.

    Creation basic structure in HTML5

    We'll start by creating the basic HTML code we'll need. To start, we'll stick to a very simple HTML5 structure.

    Now that our initial HTML code is written, we'll add the code for the menu, as well as some other details for the header of our HTML file.

    How to create an animated menu | WebDesignMagazine

    • home
    • Articles
    • This is a very cool site!

    Scroll down and see how the menu changes

    All! We've arrived!

    In our : We added a meta tag for author to indicate the creator of the document, after which we included Eric Meyer's famous “CSS reset”, which will reset almost every element in the HTML file, giving you a cleaner and easier document to work with. And since we'll be using JQuery later, on the last line of our main element we'll import it via the JQuery CDN.

    In our tag, we used the default HTML5 element. Ours will be the full width of the page and will be responsible for changes between large and small versions of the menu. We're giving ours a class called “large” so that we can change some specific properties in the CSS to turn our menu into a smaller version.

    This is our container menu that contains an image of our website logo and a simple unordered menu list with three links.

    Since we don't have any content here, this will be used to stretch the page and force scrolling to take effect.

    And that's all for the HTML part. Now we need to style the elements using CSS and make the menu dynamic.

    Styling menus and pages

    /* Import the Amaranth font */ @import url(//fonts.googleapis.com/css?family=Amaranth); /* Main style */ body( background-color: #ebebeb; ) ul( float: right; ) li( display: inline; float: left;) img.logo(float: left;) /* Menu size and centering * / nav( width: 960px; margin: 0 auto;)

    A little bit of this CSS will make our menu 960px wide in the center, while organizing our menu on the right and logo on the left. We'll also import the Amaranth font from Google Web Fonts to use for our text on the page.

    Section.stretch( float: left; height: 1500px; width: 100%; ) section.stretch p( font-family: "Amaranth", sans-serif; font-size: 30px; color: #969696; text-align: center; position: relative; margin-top: 250px; ) section.stretch p.bottom( top: 100%; )

    Header( background: #C7C7C7; border-bottom: 1px solid #aaaaaa; float: left; width: 100%; position: fixed; z-index: 10; ) header a( color: #969696; text-decoration: none; font-family: "Amaranth", sans-serif; text-transform: uppercase; font-size: 1em; ) header a.active, header a:hover( color: #3d3d3d; ) header li( margin-right: 30px; ) /* Dimensions for a larger menu */ header.large( height: 120px; ) header.large img( width: 489px; height: 113px; ) header.large li( margin-top: 45px; )

    This is where we finish the basic styling of our header.

    will serve as our menu container. It will contain our element and will serve as the element where we define the background color, menu height, link menu style and more. It will adapt to the screen width with width properties: 100% and will remain fixed across other elements on the website. It's important to remember to set the z-index so that the element overlaps the rest of the page, as well as position:fixed​ to make the div anchored at the top so that it stays in the same position while the user scrolls through the website. As you can see, in addition to setting styles for the headers, we also set some specific styles for the “large” class using header.large. The initial state of our menu will be large, and so we're defining only the necessary styles here to make it look the way we want it when the user initially enters the page.

    Dynamic menu resizing

  • Our menu is done and styled, but we still want to work on keeping it minimal. To create this "state", we will create a new CSS class called "small" which will be responsible for changing the properties we need to modify. We've already defined a large menu, so now we just have to make our menu shorter, our image smaller proportionally, and the (margin top) we use in our elements

    , which should also be reduced so that they remain vertically centered with the new menu height:

    So as you can see, these styles are almost identical to those in the larger menu, we just changed the class “large” to “small” and changed the values ​​we used to smaller ones. We use negative margin tops on the image to centralize it in the container since the image has a thin shadow and is taller than the writing to accommodate it. Now we have all the necessary styles to customize the resize menu, and if you try to change it in your , you will see in the browser that the menu will become smaller. But we need it to be dynamic.

    Changing a Menu Class Using jQuery

    With all our styling in place, we just need to add some JavaScript to switch between the "large" and "small" classes. Since we want to change this based on user scrolling, we will use the .ScrollTop() function in jQuery. This function will allow us to get or set the scroll position in pixels. The scroll position is the number of pixels that have already been scrolled in the browser window. In this case, we just need to know how many pixels the user scrolled so we can call our code and switch between classes:

    $(document).on("scroll",function())( if($(document).scrollTop()>100)( $("header").removeClass("large").addClass("small"); ) else( $("header").removeClass("small").addClass("large"); ) ));

    If the user has scrolled more than 100 pixels, then the "large" class we created will be removed and our new "small" class will be added. This way the menu will resize as we previously defined in the CSS. Give it a try, it should be working by now, but you may have noticed that the transitions between classes seem very abrupt.

    CSS transitions for menu animation

    In order to smoothly switch between classes in our menu, we will use CSS transitions. Just use this piece of code next to the rest of your CSS.

    Header,nav, a, img, li( transition: all 1s; -moz-transition: all 1s; /* Firefox 4 */ -webkit-transition: all 1s; /* Safari and Chrome */ -o-transition: all 1s; /* Opera */ )

    Here we have defined transitions for all CSS properties for. And

  • elements, basically all the elements that we change. This code will animate the changes between both classes with CSS transitions within 1 second. Check now, the result should be very smooth.

    Have you ever heard the statement that "you can't make a dynamic dropdown menu using just CSS in IE"? I'm sure so. So, do you actually believe this? That's right, better not believe it.

    The goal we want to achieve in this article

    The goal of this article is to make a dropdown menu for IE made entirely in CSS. Starting with this setting, I expanded the task to make such a menu work in other most famous browsers (from the comments it turns out that these browsers are Opera 7.x and the latest versions of Firefox).

    The goal that we want to achieve in this article is, in principle, more or less general educational, i.e. give a general idea of ​​some of the "hidden" and rarely used features of browsers.
    Also, those who are especially curious can find in this article some tricks with which you can achieve certain non-standard results. Well, for developers, this article can become a reason for thought or a source of new ideas.

    How do we imagine the level of the reader?

    I was actually thinking about labeling this article as "advanced". But I am sure that even not the most experienced developers will understand well what is written in the article. In short, the reader simply must know the basics of CSS and
    HTML.

    How is this menu different from all others?

    I spent a long time looking online for menus that were made in CSS, but I didn’t find a single solution that would work without glitches in IE. However, I found many interesting ideas that led me to the result that will be described here. Yes, my code isn't perfect either, but I simply don't have the time to fix all the errors. The most interesting alternative solution I've seen (that uses CSS) is based on using the hover pseudo-class for LI elements. But I never thought that this was possible, just as I never thought that it was even possible to make a drop-down menu for IE without scripts...

    The main difference between mine and other menus is that mine runs in IE. All the solutions I've seen use the LI element as the main element for the :hover pseudo-class, however Microsoft has decided that this pseudo-class can only be used for
    element A. Most sites make a reservation that their menus only work in Opera 7.x or Mozilla browsers. But these browsers are used by only five percent of users! Yes, such menus are good in these browsers, but unfortunately they cannot be visible in most of the most common browsers. Now we will correct this misunderstanding.

    What is a menu made with just CSS?

    This is a dynamic menu that is created using only CSS and no scripts (for example, written in JavaScript).

    What, you can’t believe it?

    Let's look at the code:

    < STYLE type = text / css id = "default" title = "default" name = "default" >
    *::- moz - any - link br ,*:- moz - any - link br (
    /*a workarround for mozilla*/
    display: none;
    }

    div #menu * (
    cursor: pointer; /*because IE displays the text cursor
    if the link is inactive*/
    }

    Disabled (
    color : red ! important ;
    background: none! important ;
    }

    Div #menu (
    background : #F5F5DC;

    height: 15px;
    white - space : nowrap ;
    width: 100%;
    }

    Div #menu .a (
    background : #F5F5DC;
    border : 1px solid #F5F5DC;
    color : #000000;
    text - decoration : none ;
    }

    Div #menu .a table (
    display: block;
    font: 10px Verdana, sans-serif;
    white - space : nowrap ;
    }

    Div #menu table, div#menu table a (
    display: none;
    }

    Div #menu .a:hover, div#menu div.menuitem:hover (
    background : #7DA6EE;
    border : 1px solid #000080;
    color : #0000FF;
    margin - right :- 1px ; /*resolves a problem with Opera
    not displaying the right border*/
    }

    Div #menu .a:hover table, div#menu div.menuitem:hover table(
    background : #FFFFFF;
    border : 1px solid #708090;
    display: block;
    position: absolute;
    white - space : nowrap ;
    }

    Div #menu .a:hover table a, div#menu div.menuitem:hover table a (
    border - left : 10px solid #708090;
    border - right : 1px solid white ; /*resolves a jump problem*/
    color : #000000;
    display: block;
    padding: 1px 12px;
    text - decoration : none ;
    white - space : nowrap ;
    z-index: 1000;
    }

    Div #menu .a:hover table a:hover, div#menu div.menuitem:hover table a:hover (
    background : #7DA6EE;
    border : 1px solid #000000;
    border - left : 10px solid #000000;
    color : #000000;
    display: block;
    padding: 0px 12px;
    text - decoration : none ;
    z-index: 1000;
    }

    Td(
    border - width : 0px ;
    padding: 0px 0px 0px 0px;
    }

    menuitem (
    float: left;
    margin : 1px 1px 1px 1px ;
    padding: 1px 1px 1px 1px;
    }

    Menuitem * (
    padding: 0px 0px 0px 0px;
    }

    #other (

    }

    #moz(

    }

    #moz::-moz-cell-content(
    height: auto; visibility: visible;
    }

    #other::-moz-cell-content(
    height: 1px; visibility: hidden;
    }

    #holder (
    width: 100%;
    }

    < TABLE id = holder >
    < TR >
    < TD id = "other" >
    < DIV id = "menu" >
    < DIV class= "menuitem" >
    < a class= "a" href = "#" >File< BR >
    < TABLE >
    < TR >
    < TD >< a href = #2>click me

    < TR >
    < TD >< a href = #3>Save

    < TR >
    < TD >< a href = #4>Close



    < DIV class= "menuitem" >
    < A class= "a" href = "#11" >Help< BR >
    < TABLE >
    < TR >
    < TD >< a class= "disabled" >..

    < TR >
    < TD >< a href = #13>Index

    < TR >
    < TD >< a href = "#14" >About






    < TR >
    < TD id = "moz" >Mozilla specific menu!
    < DIV id = "menu" >
    < DIV class= "menuitem" >
    < a class= "a" href = "#" >Filezilla
    < TABLE >
    < TR >
    < TD >< a href = #2>Open

    < TR >
    < TD >< a href = #3>Save

    < TR >
    < TD >< a href = #4>Close




    < DIV class= "menuitem" >
    < A class= "a" href = "#11" >Helpzilla
    < TABLE >
    < TR >
    < TD >< a class= "disabled" >..

    < TR >
    < TD >< a href = #13>Index

    < TR >
    < TD >< a href = "#14" >About







    < BR >

    What's going on, why is everything working?

    Let me make a reservation right away that in this article I will not teach you how to use CSS. Therefore, we immediately move on to consider the principle of operation of the menu - to the pseudo-class ":hover". Yes, this is exactly the class. Those. a selector can inherit another selector that includes ":hover". In our case, "A:hover TABLE" selects "

    V
    element , which the mouse pointer is hovering over. The following is a trick with a table whose "display" property is set to "none" (that is, it is invisible). The table is located between the anchor tags ( ,). According to Microsoft, this may cause IE to react inappropriately, but I haven't noticed anything like that.

    Why do we use a table? But because it very well separates the nested anchors that we want to use from the main anchor. This solution does not work in Mozilla 0.7 and even with JavaScript I have not yet found a way to implement this. Direct nesting of anchors is not allowed by Microsoft, so the table element is a kind of hack for IE. And, as far as I know, only tables allow you to “run” IE in this way.

    So what do we have here? 2 tables with anchors inside anchors.

    < A class= "a" href = "#11" >Help< BR >
    < TABLE cellpadding = "0" cellspacing = "0" border = "0" >
    < TR >
    < TD >< a href = "#12" >Howto

    < TR >
    < TD >< a href = "#13" >Index

    < TR >
    < TD >< a href = "#14" >About

    Which are hidden.

    div #menu .a table (
    display: none;
    z - index :- 1 ;
    }

    The browser shows the contents of the anchor on mouseover and applies the appropriate styling when it does so:

    div #menu .a:hover (
    background : #7DA6EE;
    border : 1px solid black ;
    color: black; z-index: 0;
    }

    For the dropdown table that we use for the submenu: this is the key table, which is the dropdown list.

    div #menu .a:hover table(
    background:White;
    display: block;
    position: absolute;
    width: 125px; z-index: 0;
    border : 1px solid #708090;
    }

    For links inside submenus:

    div #menu .a:hover table a (
    display: block;
    color: Black;
    text - decoration : none ;

    }

    If we hover over one of the links in the submenu, the browser applies the following style:

    For links inside submenus:

    div #menu .a:hover table a:hover (
    display: block;
    background : #7DA6EE;
    color: black;
    text - decoration : none ;
    padding: 0px 11px;
    border : 1px solid black ; z-index: 1000;
    visibility: visible;
    }

    Dropdown menu link style:

    div #menu .a:hover table a (
    display: block;
    color: Black;
    text - decoration : none ;
    padding: 1px 12px; z-index: 1000;
    }

    You may have noticed that I used multiple "z-index" properties on some elements. They are hacks for some problems I found when testing the menu.

    Improvements

    To add sublevels to a dropdown menu, you simply insert another ".menuitem" div element (along with its content and similar structure) instead of a link in the parent table.
    Now that you have sub-levels in the menu, you will need to remove the tags
    to give the menu "normal check out". In addition to this, you will need to make multiple copies of the .menuitem and .a classes with the same properties, but different names for each submenu.
    Yes, it looks like a lot of work, BUT you can simply add their selectors to the appropriate section of the style sheet.

    Full description I'll do it next time on how to do it.
    In the meantime, I’ll say that you can change this menu the way you want, adapt it to suit you. And there are endless possibilities for this - only your imagination can limit them...

    Switching styles (Skins)

    If you want to add skins for your menu with the ability to change them by the user, you will need to add additional tables styles and give them names with id="some_name" (for IE) and with name="some_name" (for other browsers). To prevent both styles from being applied, you need to disable all but the default styles by adding a "disabled" option to the tag's style (whether you link it or use linear syntax). Mozilla and Opera allow you to switch named styles from the browser. Typically, these browsers do not apply all styles that are defined by name="..." and ignore id="...". They also know how to use name="default" as a default style sheet and name="alternate" as an alternative style sheet. You can define a style name that the user will see as the title="..." property. For example, the demo menu on this page includes the following definitions:

    < STYLE type = text / css id = "alternate" title = "Blue" name = "alternate" disabled >
    ...< STYLE >
    < STYLE type = text / css id = "default" title = "Default" name = "default" >
    ...< STYLE >

    Pay attention to the naming order, I strongly recommend that you strictly follow it.

    IE doesn't have built-in toggle CSS styles, so we’ll have to do it ourselves (not without using JavaScript):

    Select one of the styles by clicking on the appropriate one and go back up to see
    changes:

    onclick = "document.styleSheets("default").disabled=false;document.styleSheets("alternate").disabled=true;" > Style
    default

    Onclick = "document.styleSheets("alternate").disabled=false;document.styleSheets("default").disabled=true;" > Blue

    Onclick = "document.styleSheets("alternate").disabled=true;document.styleSheets("default").disabled=true;" > Without
    styles

    This is done like this:

    < ul >
    < li onclick = "document.styleSheets("default").disabled=false;
    document.styleSheets("alternate").disabled=true;" >
    < a >Default

    < li onclick = "document.styleSheets("alternate").disabled=false;
    >
    < a >Blue

    < li onclick = "document.styleSheets("alternate").disabled=true;
    document.styleSheets("default").disabled=true;" >
    < a >No Stylesheet

    Warning! This is just a small example!
    Reloading the page will reset the stylesheets to default. Therefore, for real purposes it is necessary to use cookies or server scripts in order to remember the user's choices, which again is not the subject of this article.
    I will only add that the above code will only work in
    I.E.

    Conclusion

    I advise everyone to use the menu on CSS based on your sites (and web applications), because this way you can avoid many of the problems that appear when using menus on JavaScript based. Such problems typically arise when events are handled incorrectly in IE. Moreover, some browsers have the ability to disable scripts, and even more so, many browsers do not support Microsoft JS.

    If the browser does not support CSS, then it will at least display all links.

    Known Bugs

    By default, links in submenus do not work in Mozilla. But I found a more or less acceptable solution to this error. It is based on insertion special menu, again without using scripts. Look carefully at the places in the code where Mozilla (or "moz") is mentioned. You will see that the HTML sections do not have nested anchors (the last tag is placed where it should be). In the first part of the CSS I use undocumented selectors - these are special selectors for Mozilla, and adding a selector:hover for those div elements, which are supported by Mozilla. And still, after this, the behavior remains not entirely correct.

    There is a note in the comments (from Nick Young) that the menu does not work in Netscape. I'm sure the problem there is the same as in Mozilla. Have to search Additional information about it. The solution may require some changes because alternative code should work fine in Netscape as well.

    Notes:

    The page was tested in IE versions 5, 5.5, 6, Opera 7.23 and Mozilla 0.71. Most likely, the menu will work in more earlier versions specified browsers.

    Numerous requests from my persistent readers and simply curious users have had their effect. I finally created a lesson in which we will learn how to make a dynamic VKontakte menu with the effect of pressed buttons! By analogy with regular Internet sites, when a visited link is marked in a special way(pressed button, underlining, etc.) - we will create the same design on VKontakte, using the created pages and graphic design. To begin with, we will make graphic templates in Photoshop - we will create a menu header and two types of buttons. Then we will create several VKontakte pages, according to the items in our menu. And finally we'll do trick, which, in fact, will create for us the illusion of following the link. The lesson is quite complex and is suitable for those who feel confident in the functionality of Vkontakte. I will perform all manipulations using an example your VKontakte group, where this effect is realized live. So, let's get to work!

    Step 1. Create a menu header in Photoshop
    Create a document in Photoshop with a width of 600 pixels. The height can be different, at your discretion. You can place any specific photograph, collage, information banner and other graphic pictures in the header. In this case I used this one advertising banner size 600x172 pixels.

    Step 2. Create a navigation bar in Photoshop
    Now we need to create a navigation bar. IN in this example I used only text as buttons. But at your discretion, you can create colored buttons and write text on them. We do this - create a 600x56 pixel rectangle in Photoshop and in this case fill it with white. Then we write menu items on the line - about 5-6 items, no more. Large quantity items will look squeezed.

    Step 3. Create a pressed navigation bar in Photoshop
    Now we need to create active links, as if they were clicked on. I used a regular underline, but you can use a different text or background color to mark the visited link.

    Step 4. Cut ready-made pictures
    At this stage, we need to cut the pictures from Step 2 and Step 3. We should have two sets of five buttons each - one button without an underline, the other button with an underline. The buttons for each individual item (with and without underlining) must be same size. The picture below shows all of our graphic design— ten buttons and one menu header.

    Step 5. Create a VKontakte Menu page
    Now let's go to VKontakte. Our task is to create separate page called "Menu". To do this we will use the following code
    http://vk.com/pages?oid=-XXX&p=Page_Name
    where instead of XXX we will substitute the id of our group, and instead of the text “Page Name” we will write Menu. Now we need to find out the group id. How to do it? Let's go to home page groups and look at our posts on the wall - right under the “Add post” block it will say “All posts” – click on this link.

    Step 6. Determine the group id and edit the code
    We go to the page and see a URL like this https://vk.com/wall-78320145?own=1, where the numbers 78320145 in this example are the group id. We substitute our data into source and we get a record like this:
    http://vk.com/pages?oid=-78320145&p=Menu(with your numbers!). Paste this line into address bar browser and press Enter. This is how we created new page VKontakte and initially it looks like this.

    Step 7. Create the remaining VKontakte navigation pages
    In a similar way, we create four more navigation pages: Prices, How to order, Technical specifications and Questions. That is, we copy the corresponding code four more times into the address bar of the browser (with your id numbers in the example below, my numbers):

    http://vk.com/pages?oid=-78320145&p=Prices

    http://vk.com/pages?oid=-78320145&p=How_to order

    http://vk.com/pages?oid=-78320145&p=Technical specifications

    http://vk.com/pages?oid=-78320145&p=Questions
    Please note that in the two-word page title (How to order), the space between the words is replaced by an underscore How_to order. Now we have five ready-made pages for each menu item. We did not create a Portfolio page because it is located on the Menu page

    Step 8. Upload photos to the first page of the menu
    On the created one, not yet blank page(see Step 6) Menu click on the Edit link or the Fill with content link. After this we see the editing panel. Here we need to click on the camera icon with the Upload photo function. Important! Please make sure that you are in wiki markup mode. Switching modes is regulated by the icon at the right edge of the page.

    Step 9. Result after loading images
    We load our pictures that we created in Step 1 and Step 2. After loading, we see the code as in the picture below, and the menu itself looks like this. After each code change, don’t forget to click Save Page, and then click Preview to view the result.

    Step 10. Editing the image code
    Now our task is to replace all noborder properties with the nopadding property. And put the first picture actual sizes, since VKontakte compressed the image to 400 pixels when uploading. After all the changes we should get the following code and menu.

    Step 11. Add links to pictures
    Now we need to put links for each picture. The link must be inserted after nopadding| instead of a space before the closing parentheses. For the first image (menu header from Step 1), you can give a link to the main page of the group, or you can use the nolink property (put through; after nopadding without spaces). For the second card, insert the address of the format page page-78320145_49821289. That is, the full URL of the image https://vk.com/page-78320145_49821289, the first part with the domain can be omitted. But for links to external sites, the URL of the link must be specified in full.

    Step 12. Copy the code to the remaining navigation pages
    That's enough simple step, we copy latest code from the previous step and paste it onto the remaining created pages - Prices, How to order, Terms of reference and Questions. We are on the page, click Edit or Fill with content (we are in wiki markup mode), paste the code and click Save. And then also on next page. That is, now we have five pages, on each of which the menu looks exactly the same. But you can already navigate through the menu - when you click on a link, for example Prices, we will be moved to the Prices page, etc.

    Step 13. Making a pressed button effect
    Now we have to change one picture on each of the five pages (replace the button without an underline with a button with an underline). For example, on the first page of the Menu we load a new image and then replace the address in the code old picture to a new one (underlined in red). Then go to the Prices page, upload an image with underlined Prices and change it to the address in the code new picture. Then we go to the pages How to order, Terms of reference and Questions and do the same operation in the same way.

    The final.
    As a result, we got a navigation effect when you click on a menu link and it becomes active. But since the graphic design on all pages is almost the same, with the exception of active link, the illusion of navigation is created, although in fact it is moving to another page.

    The menu designed in this way is not adapted to mobile devices. When the screen size decreases, the pictures begin to slide one under the other. In order to do adaptive design, you need to use tables for the hard version. But this is another story and a more advanced technique. In the meantime, look at various options graphic design of the menu.