简体   繁体   English

使用Angular-bootstrap模式和窗口位置时,如何防止页面滚动到顶部:已修复ipad的解决方法?

[英]How to prevent page scroll to top when using Angular-bootstrap modal and window position:fixed workaround for ipad?

You may know, that there is a bug in bootstrap modals on some devices, that the page behind the modals scrolls instead of the modal ( http://getbootstrap.com/getting-started/#support-fixed-position-keyboards ) 您可能知道,某些设备上的引导程序模式存在错误,模式列表后面的页面滚动而不是模式页面( http://getbootstrap.com/getting-started/#support-fixed-position-keyboards

This bug can be easily fixed by adding a css-rule .modal-open { position: fixed; } 可以通过添加css-rule .modal-open { position: fixed; } .modal-open { position: fixed; } . .modal-open { position: fixed; }

But this fix produces another bug - when you open a modal the page scrolls to the top. 但是此修复程序会产生另一个错误-当您打开模式窗口时,页面会滚动到顶部。 It can be resolved via JS, for example this way: https://stackoverflow.com/a/34754029/2244262 可以通过JS来解决,例如通过以下方式: https : //stackoverflow.com/a/34754029/2244262

BUT I use angular-bootstrap package, which replaces bootstrap jquery events with promises . 但是我使用angular-bootstrap包,它将packagestrap jquery事件替换为promises Because of this I don't know how to define global handlers for all modals on show/hide events. 因此,我不知道如何为显示/隐藏事件的所有模态定义全局处理程序。 I have too many modals in my code and I don't want to define these handlers in each modal call, that's too dirty. 我的代码中有太多模态,并且我不想在每个模态调用中定义这些处理程序,这太脏了。

Any ideas on how to fix scroll to the top problem in my situation? 关于如何解决我所遇到的最主要问题的任何想法?

Maybe you can do it that way. 也许您可以那样做。 The close function works when modal is closed. 当模式关闭时,关闭功能起作用。 This prevents the page from scrolling up when the modal closes. 这可以防止模式关闭时页面向上滚动。

ngOnInit(): void {
 document.body.style.position = 'initial';
 document.body.style.top = `-${window.scrollY}px`;
 ...
}

close(result?: any): void {
 this.activeModal.close(result || {});

 const scrollY = document.body.style.top;
 document.body.style.position = '';
 document.body.style.top = '';
 window.scrollTo(0, parseInt(scrollY || '0') * -1);
}

暂无
暂无

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

相关问题 如何以安全最小化的方式声明角度引导模态 - How to declare an angular-bootstrap modal in a way that is safe for minimization 如何从angular-bootstrap模态中删除类 - How to remove the class from angular-bootstrap modal 单元测试angular-bootstrap $ modal - Unit testing angular-bootstrap $modal 嵌套视图上的角度引导模态 - angular-bootstrap modal on nested view 当引导程序模态打开后应如何回到正常位置,如何防止背景滚动 - How to prevent background scroll when bootstrap modal is open after that it should come to normal position Bootstrap 3模态弹出框可防止通过聚焦在顶部滚动 - Bootstrap 3 Modal Popup prevent scroll by focus on top 使用 angular-bootstrap 时 $injector 错误 ($modalInstanceProvider &lt;- $modalInstance) - $injector error when using angular-bootstrap ($modalInstanceProvider <- $modalInstance) 在固定位置div上使用jquery淡入/淡出功能时,防止页面自动向上滚动 - prevent automatically page scroll up when using jquery fadein/out founction on fixed position div 来自函数调用的变量更改未显示在angular-bootstrap模态窗口中 - Variable changes from function call not displaying in angular-bootstrap modal window 在ipad浏览器中打开bootstrap模式时,是否可以将模态弹出窗口的位置固定在中心? 我正在使用Bootstrap v3.0.0 - Is it possible to fix the position of modal-popup window in center when bootstrap modal is open in ipad browsers? I am using Bootstrap v3.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM