[英]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.