twitter-bootstrap 更改将引导程序“modal-open”类分配给而不是主体的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26335861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change where bootstrap "modal-open" class is assigned to instead of the body
提问by Third_Hyperion
Currently
目前
modal-open
is assigned to the body when the modal is opened. How do i change where the class is added to instead of the body.
打开模态时分配给主体。我如何更改类添加到而不是正文的位置。
Thanks for any help.
谢谢你的帮助。
采纳答案by MrPk
You have to change js that adds modal-open. It's a bit strange as behaviour, anyway you can do it softly or brutally.
您必须更改添加 modal-open 的 js。作为行为有点奇怪,无论如何你可以轻柔地或粗暴地这样做。
Softly: intercept modal click for show and remove class to body adding to another element:
轻轻地:拦截模态点击以显示并删除类到正文添加到另一个元素:
// Modal is first added to the body element and then removed and added to another element
// Modal先被添加到body元素,然后被移除并添加到另一个元素
$('#myModal').on('shown.bs.modal', function (e) {
$('body').removeClass('modal-open');
$('#myCustomElement').addClass('modal-open');
})
and then intercept also the close of the modal and add an execution of a second remove:
然后拦截模态的关闭并添加第二个删除的执行:
$('#myModal').on('hidden.bs.modal', function (e) {
$('#myCustomElement').removeClass('modal-open');
})
Brutally method, to avoid that modal-open it's unnecessary added to body element: go in TwitterBootstrap directory > js Open modal.js Search for modal-open , you will find (on version 3.2.0):
残酷的方法,为了避免 modal-open 没有必要添加到 body 元素:进入 TwitterBootstrap 目录 > js 打开 modal.js 搜索 modal-open ,你会发现(在 3.2.0 版本上):
in
在
Modal.prototype.show
you'll find
你会找到
this.$body.addClass('modal-open')
in Modal.prototype.hideyou'll find
在Modal.prototype.hide你会发现
this.$body.removeClass('modal-open')
you have to change the target of addClassand removeClassand replace those instructions, if you have a fixed element it's enough simple (Ex: $('#myFixedElement').addClass('modal-open'), if it's a dynamic element it's complex and you'll have again to intercept Show function to add a global varin js to let modal.jsknow and read your target element. However you can also change the signature of those functions but i don't really suggest you to do that, basically you'll make your code too hard to maintain.
你要改的目标addClass和removeClass并更换这些说明,如果你有一个固定的元素是很简单(例如:$('#myFixedElement').addClass('modal-open')如果它是一个动态的元素,它是复杂的,你将不得不再次拦截显示功能添加global var在JS让modal.js了解并阅读您的目标元素。但是您也可以更改这些函数的签名,但我并不真正建议您这样做,基本上您会使您的代码难以维护。

