如何使用纯javascript显示和隐藏模态对话框
How to show and hide modal dialog using pure javascript?
我有一个对话框,我想用纯javascript显示/隐藏它。我使用了modal.classList.add('hide');它隐藏了它,但随后整个屏幕都冻结了,我无法点击任何东西。隐藏和显示动画对于我正在构建的对话框来说是最重要的,这就是我采取这种方法的原因。有我的模式对话框
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content" >
<div class="modal-header">
<a class="close button">
<paper-button raied type="button" class="close" id="close" aria-hidden="true" on-click="close">Close</paper-button>
</a>
<h2 class="modal-title">Title</h2>
</div>
<div class="modal-body next">
<img class='modal-img' />
</div>
<div class="modal-footer">
<paper-button raied id="previous" type="button" class="pull-left prev" on-click="prev">Previous</paper-button>
<paper-button raied id="next" type="button" class="next" on-click="next">Next</paper-button>
</div>
</div>
</div>
</div>
这是关闭对话框和CSS的函数:
close: function() {
var modal = Polymer.dom(this.root).querySelector(".modal");
//modal.style.display = "none";
modal.classList.add('hide');
}
这是我的样式表:
<style>
@keyframes show {
0% {
display: none;
opacity: 0;
}
1% {
display: block;
}
100% {
display: block;
opacity: 1;
}
}
@keyframes hide {
0% {
display: block;
opacity: 1;
}
99% {
display: block;
}
100% {
display: none;
opacity: 0;
}
}
.element, .element-css {
animation: show 500ms linear;
animation-fill-mode: forwards;
}
.hide{
animation: hide 500ms linear;
animation-fill-mode: forwards;
}
</style>
你可以试试这个
Polymer({
is: 'my-dialog',
behaviors: [
Polymer.NeonAnimationRunnerBehavior
],
properties: {
opened: {
type: Boolean
},
animationConfig: {
value: function() {
return {
'entry': {
// provided by neon-animation/animations/scale-up-animation.html
name: 'scale-up-animation',
node: this
},
'exit': {
// provided by neon-animation-animations/fade-out-animation.html
name: 'fade-out-animation',
node: this
}
}
}
}
},
listeners: {
'neon-animation-finish': '_onNeonAnimationFinish'
},
show: function() {
this.opened = true;
this.style.display = 'inline-block';
// run scale-up-animation
this.playAnimation('entry');
},
hide: function() {
this.opened = false;
// run fade-out-animation
this.playAnimation('exit');
},
_onNeonAnimationFinish: function() {
if (!this.opened) {
this.style.display = 'none';
}
}
});
Html
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content" >
<div class="modal-header">
<a class="close button">
<paper-button raied type="button" class="close" id="close" aria-hidden="true" on-click="close">Close</paper-button>
</a>
<h2 class="modal-title">Title</h2>
</div>
<div class="modal-body next">
<img class='modal-img' />
</div>
<div class="modal-footer">
<paper-button raied id="previous" type="button" class="pull-left prev" on-click="prev">Previous</paper-button>
<paper-button raied id="next" type="button" class="next" on-click="next">Next</paper-button>
</div>
</div>
</div>
</div>
您还可以查看jsfiddle链接
在隐藏关键帧中以100%添加visibility: hidden
,以0%添加visibility: visible
,因此显示关键帧。
希望对有所帮助
相关文章:
- 将隐藏输入类型转移到我的隐藏模态/表单
- 使用模态上的文件输入字段更新主视图上的隐藏字段
- 角度JS.超时模态隐藏在已打开的模态后面
- 如何在没有背景的情况下隐藏模态弹出窗口
- 分解javascript/jquery代码,以便在单击外部时隐藏模态
- NgAnimate - 由于 NGAnimate,我的模态显示隐藏不起作用
- jQueryUI 自动完成隐藏在模态后面
- Angular JS:隐藏/关闭模态
- 滚动以隐藏Javascript模态
- 隐藏Angular Bootstrap模态而不是关闭
- Bootstrap如何隐藏模态
- 如何使用纯javascript显示和隐藏模态对话框
- 如果条件为假,隐藏第二个模态
- Onsen UI模态在angularjs中是不隐藏的
- 如何在模态渐变中隐藏元素
- 隐藏模态后,模态中文本框的值没有被清除
- 禁用隐藏模态窗口时,点击退出模态jquery
- 我如何在渲染模板和隐藏模态后重置表单
- 通过背景点击来消除模态和隐藏模态
- 如何在每次隐藏模态时重置模态内容(removeData() 不起作用)