[英]jquery div slide effect with another div adjusting width automatically
我正在嘗試通過菜單滑動菜單項來使giva滑入/滑出效果,以便在用戶需要時為主要內容提供更多空間-更像Google日歷漢堡菜單的點擊行為。
我正在嘗試並排放置兩個div。 第一個是菜單,第二個是其他內容。 我正在嘗試使用jQuery UI切換幻燈片第一分區。
兩個問題-
只有將固定寬度指定為2格時,才能將它們並排放置。
如何使第二個div占用其余空間?
僅在滑出第二個div完成后才調整其寬度。
如何使寬度平滑增加?
這是代碼https://codepen.io/coder92/pen/Yjomwd
$(document).ready(function(){ //adds click function to the hamburger menu $( "#menu" ).click(function() { $(".menuDiv").toggle('slide'); }); });
.menuDiv{ background-color: green; width:200px; float:left; } .contentDiv{ //width:100%; width:500px; color:white; background-color:blue; float:left; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <span id="menu"> toggle </span> <div> <div class="menuDiv"> Menu </div> <div class="contentDiv"> <table> <tr> <td> data </td> </tr> </table> </div> </div>
提前致謝!
為了順利滑動,您可以使用
$(".menuDiv").animate({width: 'toggle'}, 350);
並使數據占據頁面的其余部分
float: left;
來自.contentDiv
(浮動使元素的大小不完整) margin: 0 auto;
(這使數據占據了整個頁面的其余部分) overflow: hidden
(阻止藍色從綠色下方移動) Google日歷執行此操作的方式是利用flex
顯示, hidden
溢出和負margin
來達到效果。 您可以參考以下示例:
$(document).ready(function(){ //adds click function to the hamburger menu $("#menu").click(function () { var $menu = $(".menuDiv"); if ($menu.is(':visible')) { $menu.animate({'margin-left':-200}, function() { $menu.hide(); }); } else { $menu.show().animate({'margin-left':0}); } }); });
.containerDiv { display: flex; overflow-x: hidden; } .menuDiv{ background-color: green; width:200px; } .contentDiv{ color:white; background-color:blue; float:left; flex:1 1 auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <span id="menu"> toggle </span> <div class="containerDiv"> <div class="menuDiv"> Menu </div> <div class="contentDiv"> <table> <tr> <td> data </td> </tr> </table> </div> </div>
請注意,我使用jQuery animate
方法,而Google Calendar使用CSS transition
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.