简体   繁体   English

如何使用jQuery创建幻灯片菜单(从右至左/从左至右)

[英]how to create a slide menu ( right to left / left to right ) with jQuery

i was trying to create a slide menu with jQuery. 我试图用jQuery创建幻灯片菜单。 By default the menu is open when the page loads. 默认情况下,页面加载时菜单是打开的。 and when i clicks on it, Menu slides from right to left. 当我单击它时,菜单从右向左滑动。 but i don't know how we can push it to its default position. 但我不知道如何将其推到默认位置。 or how we can push it back and forth. 或我们如何来回推动它。

JS Fiddle: http://jsfiddle.net/pPf7N/307/ JS小提琴: http : //jsfiddle.net/pPf7N/307/

HTML 的HTML

<aside class="asideMenu">

 i'm the menu click me

</aside>

jQuery jQuery的

$('.asideMenu').on('click', function(){
  $(this).css({
   'left':'-200px'
  });
  $(this).html('<span class="openMenu"> open menu -> </span>');
  $('.openMenu').on('click', function(){
   alert('want to push the menu to its default position.');
  });
});

This is a working example, and you can elaborate it to apply according to your needs. 这是一个有效的示例,您可以详细说明它以根据需要进行应用。 Hope it helps. 希望能帮助到你。

 $('.asideMenu').on('click', function() { if ($(this).hasClass('menuClosed')) { $(this).removeClass('menuClosed'); } else { $(this).addClass('menuClosed'); } }); 
 aside { height: 900px; width: 300px; position: absolute; background-color: #ddd; margin: 0px; padding: 0px; left: 0px; cursor: pointer; transition: all 0.5s ease; -webkit-transition: all 0.5s ease; } .menuClosed { left: -200px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <aside class="asideMenu"> i'm the menu click me </aside> 

try change your JS with this one it may be helpful 尝试用此一种更改您的JS可能会有所帮助

 $('.asideMenu').click(function() { var clicks = $(this).data('clicks'); if (clicks) { $(this).css({ 'left':'-10px' }); $(this).html('<span class="openMenu"> open menu -> </span>'); } else { $('.asideMenu').css({ 'left':'-200px' }); } $(this).data("clicks", !clicks); }); 
 aside{ height:900px; width:300px; position:absolute; background-color:#ddd; margin:0px; padding:0px; cursor:pointer; transition:all 0.5s ease; -webkit-transition:all 0.5s ease; } .openMenu{ color:#000; float:right; cursor:pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <aside class="asideMenu"> i'm the menu click me </aside> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM