<script type = "text / javascript"> // when the page loads $ (document) .ready (function () { // hang the handler on the link with the class "btn-slide" (top menu) $ (". btn-slide"). click (function () { // push / hide panel with id = panel1 $ ("# panel1"). slideToggle ("slow"); // change the class of the link itself $ (this) .toggleClass ("active"); // and do nothing further (so that there is no link transition) return false; }); // hang the handler on the link with the class "btn-slide2" (bottom menu) $ (". btn-slide2"). click (function () { // push / hide panel with id = panel2 $ ("# panel2"). slideToggle ("slow"); // change the class of the link itself $ (this) .toggleClass ("active2"); // and do nothing further (so that there is no link transition) return false; }); }); </ script>
<div class = "left"> <! - Directly the contents of the menu, we hide it -> <div class = "panel"> <ul> <li> <a href="#" title="Element 1"> Element 1 </a> </ li> <li> <a href="#" title="Element 2"> Element 2 </a> </ li> <li> <a href="#" title="Element 3"> Element 3 </a> </ li> </ ul> <ul> <li> <a href="#" title="Element 1"> Element 1 </a> </ li> <li> <a href="#" title="Element 2"> Element 2 </a> </ li> <li> <a href="#" title="Element 3"> Element 3 </a> </ li> </ ul> <ul> <li> <a href="#" title="Element 1"> Element 1 </a> </ li> <li> <a href="#" title="Element 2"> Element 2 </a> </ li> <li> <a href="#" title="Element 3"> Element 3 </a> </ li> </ ul> </ div> <! - Panel with button -> <p class = "slide"> <a href="#" class="btn-slide"> Menu </a> </ p> </ div>
// create an event handler for links with the class "btn-slide" $ (". btn-slide"). toggle (function () { // ... 1st click on the link // return false return false; }, function () { // ... 2nd click on the link // return false return false; });
// go along the DOM to level 2 up, inside the element (this is a div with the class left / right) we find the element we need and increment it 120 pixels wide $ (this) .parent (). parent (). find (". panel"). animate ({"width": "+ = 120px"}, "slow"); // replace the button class (for changing the arrow) $ (this) .toggleClass ("active");
$ (document) .ready (function () { $ (". btn-slide"). toggle (function () { $ (this) .parent (). parent (). find (". panel"). animate ({"width": "+ = 120px"}, "slow"); $ (this) .toggleClass ("active"); return false; }, function () { $ (this) .parent (). parent (). find (". panel"). animate ({"width": "- = 120px", opacity: "hide"}, "slow"); $ (this) .toggleClass ("active"); return false; }); });
<div class = "topmenu"> <ul> <li> <a href="#" title="Menu 1"> Menu 1 </a> <ul> <li> <a href="#" title="Element 1.1"> Element 1.1 </a> </ li> <li> <a href="#" title="Element 1.2"> Element 1.2 </a> </ li> <li> <a href="#" title="Element 1.3"> Element 1.3 </a> </ li> </ ul> </ li> <li> <a href="#" title="Menu 2"> Menu 2 </a> <ul> <li> <a href="#" title="Element 2.1"> Element 2.1 </a> </ li> <li> <a href="#" title="Element 2.2"> Element 2.2 </a> </ li> <li> <a href="#" title="Element 2.3"> Element 2.3 </a> </ li> </ ul> </ li> <li> <a href="#" title="Menu 3"> Menu 3 </a> <ul> <li> <a href="#" title="Element 3.1"> Element 3.1 </a> </ li> <li> <a href="#" title="Element 3.2"> Element 3.2 </a> </ li> <li> <a href="#" title="Element 3.3"> Element 3.3 </a> </ li> </ ul> </ li> </ ul> </ div>
$ ('. topmenu ul li'). hover ( function () { // ... }, function () { // ... } );
// find the ul element and call the animation slideDown $ (this) .find ('ul'). slideDown (); // change the background of the selected item by adding an active class $ (this) .addClass ("active");
$ (document) .ready (function () { $ ('. topmenu ul li'). hover ( function () { $ (this) .addClass ("active"); $ (this) .find ('ul'). slideDown (); }, function () { $ (this) .removeClass ("active"); $ (this) .find ('ul'). slideUp ('fast'); } ); });
<div class = "topmenu"> <ul> <li id = "menu1"> <a href="#" title="Menu 1"> Menu 1 </a> </ li> <li id = "menu2"> <a href="#" title="Menu 2"> Menu 2 </a> </ li> <li id = "menu3"> <a href="#" title="Menu 3"> Menu 3 </a> </ li> </ ul> </ div>
<ul> <li> <a href="#" title="Element 1"> Element 1 </a> </ li> <li> <a href="#" title="Element 2"> Element 2 </a> </ li> <li> <a href="#" title="Element 3"> Element 3 </a> </ li> </ ul>
$ (document) .ready (function () { $ ('. topmenu ul li'). hover ( function () { // ... here you need to make changes to the code $ (this) .addClass ("active"); }, function () { // leave here as is $ (this) .removeClass ("active"); $ (this) .find ('ul'). slideUp ('fast'); } ); });
// get the id of the active menu item var id = $ (this) .attr ('id'); // we push the active element into a local variable var li = $ (this); $ .ajax ({ // form the name of the page requested by AJAX url: 'ajax /' + id + '. html', beforeSend: function () { // before the "ask" change the class of the element - display the loading image li.addClass ('loading'); }, success: function (data) { // fill the submenu li.append (data); // show what happened li.find ('ul'). slideDown (); // remove loading image li.removeClass ('loading'); } });
$ (document) .ready (function () { $ ('. topmenu ul li'). hover ( function () { // add verification - haven't the elements been loaded before if ($ (this) .find ('ul'). length == 0) { var id = $ (this) .attr ('id'); var li = $ (this); $ .ajax ({ url: 'ajax /' + id + '. html', beforeSend: function () { li.addClass ('loading'); }, success: function (data) { li.append (data); li.find ('ul'). slideDown (); li.removeClass ('loading'); } }); } else { $ (this) .find ('ul'). slideDown (); } $ (this) .addClass ("active"); }, function () { $ (this) .find ('ul'). slideUp ('fast'); $ (this) .removeClass ("active"); } ); });
$ (document) .ready (function () { // add hover event handler $ ('. topmenu ul li'). hover ( function () { $ (this) .find ('ul: first'). slideDown (); }, function () { $ (this) .find ('ul: first'). slideUp ('fast'); } ); // add nesting to all menu items with nesting ” $ ('. topmenu li: has (ul)'). find ('a: first'). append ('"'); });
// get css information about the location of the top menu menu1 = parseInt ($ (". right"). css ("top"). substring (0, $ (". right"). css ("top"). indexOf ("px")); // location of the bottom menu is calculated based on the window size (96 taken by eye) menu2 = $ (window) .height () - 96;
$ (window) .scroll (function () { // here we will drag our menus });
$ (window) .scroll (function () { // define new position for our menus offset1 = menu1 + $ (document) .scrollTop () + "px"; offset2 = menu2 - $ ('. left .panel'). height () + $ (document) .scrollTop () + "px"; // drag items to a new place $ ('. right'). animate ({top: offset1}, {duration: 500, queue: false}); $ ('. left'). animate ({top: offset2}, {duration: 1000, queue: false}); });
// for all elements "a" which are in "li" with nested lists "ul" $ ('. panel ul li: has (ul) a'). click (function () { // go to the parent, find the "ul" and hide / hide it $ (this) .parent (). find ('ul'). slideToggle (); return false; }); // button "+" - hide all "ul" nested in "li" $ ('a.plus'). click (function () { // go to the parent, find the next element in the house, look for "ul li ul" in it, perform "slideUp" $ (this) .parent (). next (). find ('ul li ul'). slideUp ('fast'); return false; }); // button "-" - display all "ul" nested in "li" $ ('a.minus'). click (function () { // go to the parent, find the next element in the house, look for "ul li ul" in it, perform "slideDown" $ (this) .parent (). next (). find ('ul li ul'). slideDown ('slow'); return false; });
Source: https://habr.com/ru/post/39440/
All Articles