简体   繁体   English

jQuery 在隐藏溢出时停止内容跳转

[英]jQuery Stop content from jumping when overflow hidden

The default size of a scrollbar is 17px in width, however its better to let the browser calculate the exact width and then use it.滚动条的默认宽度为 17px,但是最好让浏览器计算出准确的宽度然后使用它。

// Calculating the exact width of the browser scrollbar
    let scrollbarWidth=(window.innerWidth-$(window).width());
    
// then when needed to perform a click action just add this below it:
        $("body").css("overflow","hidden");
        $("body").css("padding-right", scrollbarWidth +"px");

Try using box-sizing: border-box;尝试使用box-sizing: border-box; in your body.在你的身体里。

Here in this demo I set the body width to (100vw + 1px) so that it will engage the horizontal scrollbar.在此演示中,我将主体宽度设置为(100vw + 1px) ,以便它与水平滚动条接合。 When you click the button, it will add the style as you showed (hiding the scrollbar and adding margin to the body).当您单击该按钮时,它将添加您显示的样式(隐藏滚动条并向主体添加边距)。

I added borders to the button to show it correctly aligns to the right edge as intended.我为按钮添加了边框,以显示它按预期正确对齐到右边缘。

 $(document).ready(function() { console.log($(document).width(), window.innerWidth); var scrollbarWidth = ($(document).width() - window.innerWidth) + "px"; $(document).on('click', '.is-open', function() { console.log(scrollbarWidth); if ($(this).hasClass("is-open")) { $("body").css("overflow", "hidden"); $("body").css("padding-right", scrollbarWidth); } }); });
 body{ width: calc(100vw + 2px); box-shadow: inset 0 0 2px red; margin: 0; box-sizing: border-box; }.is-open{ cursor: pointer; border: solid green; padding: 2px 10px; font-size: 2rem; text-align: right; }.buttoncontainer{ display: block; text-align: right; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <p>Clicking the button will hide the scrollbar and add a padding-right to body</p> <div class="buttoncontainer"> <button class="is-open">Click here</button> </div> </body>

暂无
暂无

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

相关问题 加载内容时停止无限滚动从跳转到页面末尾 - Stop infinite scroll from jumping to end of page when loading content 阻止浏览器跳转到jQuery中的id元素 - Stop browser from jumping to an id element in jQuery 当内容从溢出滚动和溢出隐藏时,元素的 100% 宽度相同 - The same 100% width of element when content shifts from overflow scroll and overflow hidden 如何使用jQuery阻止内容跳到固定导航后面 - How to stop content jumping up behind fixed nav using jQuery 当使用jQuery从溢出区域外部动画子元素时,元素不遵守隐藏的溢出 - Element does not obey overflow hidden when a child element is animated in from outside the overflow area with jQuery 当用户单击图像时,如何使用jQuery / JS显示“溢出:隐藏内容”? - How to show the overflow:hidden content using jQuery / JS when a user clicks on an image? 如何检查是否溢出:隐藏的html类已经使用jquery或javascript从视图内容中删除了? - how to check if overflow:hidden html class has cut out from view content using jquery or javascript? 导航栏固定在滚动后如何阻止内容跳转? - How to stop content from jumping after navbar becomes fixed on scroll? 当溢出被“隐藏”时,停止Firefox重置水平滚动位置? - Stop Firefox resetting horizontal scroll position when overflow is “hidden”? 如何在向右溢出时隐藏div内容 - How to make a div content hidden when it's overflow to the right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM