简体   繁体   English

CSS JS 固定位置侧边栏

[英]CSS JS Fixed position sidebar

I have 2 blocks - one with fixed position and the other next to it.我有 2 个街区 - 一个位置固定,另一个在它旁边。 I would like the fixed block to increase in length after clicking the button, and the second block to decrease in size and to click everything again to return it to its original state.我希望固定块在单击按钮后长度增加,第二个块减小大小并再次单击所有内容以将其恢复到原始状态。 Fixed block will be sidebar menu, permanently glued.固定块将是侧边栏菜单,永久粘合。

Everything should happen at 100% width.一切都应该以 100% 的宽度发生。

How to do it, but the important thing is that the width of the fixed block and the latter should be specified in pixels, not in percent .怎么做,但重要的是固定块和后者的宽度应该以像素为单位指定,而不是百分比

Is this to be done?这是要做的吗?

 jQuery(document).ready(function($) { jQuery(".btn").click(function() { var div1 = jQuery(".left"); var div2 = jQuery(".right"); if (div1.width() < 200) { div1.animate({ width: "25%" }, { duration: 200, specialEasing: { width: "linear" } }); div2.animate({ width: "75%" }, { duration: 200, specialEasing: { width: "linear" } }); } else { div1.animate({ width: "5%" }, { duration: 200, specialEasing: { width: "linear" } }); div2.animate({ width: "95%" }, { duration: 200, specialEasing: { width: "linear" } }); } }); });
 .container { width: 100%; margin: 0 auto; } .left { float: left; position: fixed; width: 5%; height: 100%; background: pink; z-index: 5; } .right { float: right; position: relative; width: 95%; height: 300px; background: green; } .content { float: left; position: relative; width: 100%; height: 100px; background: silver; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="left"> <input type="button" value="click" class="btn"> </div> <div class="right"> <div class="content"> abc </div> </div> </div>

Everything should happen at 100% width.一切都应该以 100% 的宽度发生。

... in pixels, not in percent. ...以像素为单位,而不是百分比。

Are you sure that it's not the opposite?你确定这不是相反的吗? It's way more better to try to make yor css based on % rather that px or rem (unless it's required to do it in px )尝试基于%而不是pxrem制作 yor css 更好(除非需要在px

In any case, I fixed your code, but in % , from now on you can play with px if that's the requirement:无论如何,我修复了您的代码,但是在% ,从现在开始,如果需要,您可以使用 px:

 jQuery(document).ready(function($) { jQuery(".btn").click(function() { var leftDiv = jQuery(".left"); var rightDiv = jQuery(".right"); console.log("leftDiv width: " + leftDiv.width() + " - " + "rightDiv width: " + rightDiv.width()) var minWidthForRight = $('.container').width() - 300; var maxWidthForRight = $('.container').width() - 100; if (leftDiv.width() < 200) { leftDiv.animate({ width: "300px", }, { duration: 200, specialEasing: { width: "linear" } }); rightDiv.animate({ width: minWidthForRight }, { duration: 200, specialEasing: { width: "linear" } }); } else { leftDiv.animate({ width: "100px" }, { duration: 200, specialEasing: { width: "linear" } }); rightDiv.animate({ width: maxWidthForRight }, { duration: 200, specialEasing: { width: "linear" } }); } }); });
 .container { width: 100%; margin: 0 auto; } .left { float: left; position: fixed; width: 150px; height: 300px; background: pink; z-index: 5; } .right { float: right; position: relative; width: 95%; height: 300px; background: green; resize: horizontal; } .content { float: left; position: relative; width: 100%; height: 100px; background: silver; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="left"> <input type="button" value="click" class="btn"> </div> <div class="right"> <div class="content"> abc </div> </div> </div>

EDIT编辑

Yes, it is possible to keep the left side in px and the left side in %, not % itself but calculated in px based on screen resolution.是的,可以将左侧保留为 px,将左侧保留为 %,而不是 % 本身,而是根据屏幕分辨率以 px 计算。

Basically you take the container width and "eliminate" the width that you specified in animation:基本上,您采用容器宽度并“消除”您在动画中指定的宽度:

var minWidthForRight = $('.container').width() - 300;

The right side will be: 300 + (100% - 300 ) and same for left: 100 + (100% - 100 )右侧将是: 300 + (100% - 300 )左侧相同: 100 + (100% - 100 )

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

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