Remodal 轻量级 JavaScript 模态窗口插件 - 文章教程

Remodal 轻量级 JavaScript 模态窗口插件

发布于 2020-08-23 字数 7691 浏览 1022 评论 0

Remodal 是一个扁平化,响应式,轻量,快速,容易定制的模态窗口插件。使用声明式状态符号和哈希(Hash)跟踪。所有现代的浏览器都支持。您可以轻松地定义为模态窗口定义背景容器(如模糊效果)。支持所有现代的浏览器。

Remodal 轻量级 JavaScript 模态窗口插件

兼容性

  • 支持所有现代浏览器。
  • 对于 IE8+ 浏览器。要启用 IE8 样式,请像 modernizr 那样将 lt-ie9 类添加到 html 元素中。
  • jQuery、jQuery2、Zepto 支持。
  • Browserify 支持。

使用方法

在头部分包含dist文件夹中的CSS文件:

<link rel="stylesheet" href="dist/remodal.css">
<link rel="stylesheet" href="dist/remodal-default-theme.css">

在</ body>之前的dist文件夹中包含JS文件:

<script src ="dist/remodal.min.js"> </script>

您可以为模态定义背景容器(如模糊效果)。它可以是任何简单的内容包装器:

<div class =“remodal-bg”>
...页面内容...
</ DIV>

现在创建模态对话框:

<div class="remodal" data-remodal-id="modal">
  <button data-remodal-action="close" class="remodal-close"></button>
  <h1>Remodal</h1>
  <p>
    Responsive, lightweight, fast, synchronized with CSS animations,
    fully customizable modal window plugin with declarative
    configuration and hash tracking.
  </p>
  <br>
  <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
  <button data-remodal-action="confirm" class="remodal-confirm">OK</button>
</div>

不要使用 ID 属性,如果你想避免锚跳,用 data-remodal-id.

所以,现在你可以用哈希函数调用它:

<a href="#modal">Call the modal with data-remodal-id="modal"</a>

或这这样使用:

<a data-remodal-target="modal">Call the modal with data-remodal-id="modal"</a>

选项

您可以使用data-remodal-options属性传递其他选项。

<div class="remodal" data-remodal-id="modal"
  data-remodal-options="hashTracking: false, closeOnOutsideClick: false">
  <button data-remodal-action="close" class="remodal-close"></button>
  <h1>Remodal</h1>
  <p>
    Responsive, lightweight, fast, synchronized with CSS animations,
    fully customizable modal window plugin with declarative
    configuration and hash tracking.
  </p>
  <br>
  <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
  <button data-remodal-action="confirm" class="remodal-confirm">OK</button>
</div>

hashTracking

Default: true

To open the modal without the hash, use the data-remodal-target attribute.

<a data-remodal-target="modal" href="#">Call the modal with data-remodal-id="modal"</a>

closeOnConfirm

Default: true

If true, closes the modal window after clicking the confirm button.

closeOnCancel

Default: true

If true, closes the modal window after clicking the cancel button.

closeOnEscape

Default: true

If true, closes the modal window after pressing the ESC key.

closeOnOutsideClick

Default: true

If true, closes the modal window by clicking anywhere on the page.

modifier

Default: ''

Modifier CSS classes for the modal that is added to the overlay, modal, background and wrapper (see CSS).

appendTo

Default: document.body

全局配置

<script>
window.REMODAL_GLOBALS = {
  NAMESPACE: 'modal',
  DEFAULTS: {
    hashTracking: false
  }
};
</script>
<script src="../dist/remodal.js"></script>

NAMESPACE

Base HTML class for your modals. CSS theme should be updated to reflect this.

DEFAULTS

Extends the default settings.

通过 JavaScript 调用

Do not set the ‘remodal’ class, if you prefer a JS initialization.

<div data-remodal-id="modal">
  <button data-remodal-action="close" class="remodal-close"></button>
  <h1>Remodal</h1>
  <p>
    Responsive, lightweight, fast, synchronized with CSS animations,
    fully customizable modal window plugin with declarative
    configuration and hash tracking.
  </p>
</div>
<script>
    var options = {...};
    $('[data-remodal-id=modal]').remodal(options);
</script>

Methods

Get the instance of the modal and call a method:

var inst = $('[data-remodal-id=modal]').remodal();

/**
 * Opens the modal window
 */
inst.open();

/**
 * Closes the modal window
 */
inst.close();

/**
 * Returns a current state of the modal
 * @returns {'closed'|'closing'|'opened'|'opening'}
 */
inst.getState();

/**
 * Destroys the modal window
 */
inst.destroy();

Events

$(document).on('opening', '.remodal', function () {
  console.log('Modal is opening');
});

$(document).on('opened', '.remodal', function () {
  console.log('Modal is opened');
});

$(document).on('closing', '.remodal', function (e) {

  // Reason: 'confirmation', 'cancellation'
  console.log('Modal is closing' + (e.reason ? ', reason: ' + e.reason : ''));
});

$(document).on('closed', '.remodal', function (e) {

  // Reason: 'confirmation', 'cancellation'
  console.log('Modal is closed' + (e.reason ? ', reason: ' + e.reason : ''));
});

$(document).on('confirmation', '.remodal', function () {
  console.log('Confirmation button is clicked');
});

$(document).on('cancellation', '.remodal', function () {
  console.log('Cancel button is clicked');
});

CSS

Classes

  • .remodal – the default class of modal dialogs.
  • .remodal-wrapper – the additional wrapper for the .remodal, it is not the overlay and used for the alignment.
  • .remodal-overlay – the overlay of modal dialogs, it is under the wrapper.
  • .remodal-bg – the background of modal dialogs, it is under the overlay and usually it is the wrapper of your content. You should add it on your own.

The remodal prefix can be changed in the global settings. See the NAMESPACE option.

States

States are added to the .remodal, .remodal-overlay, .remodal-bg, .remodal-wrapper classes.

List:

.remodal-is-opening
.remodal-is-opened
.remodal-is-closing
.remodal-is-closed

Modifier

A modifier that is specified in the options is added to the .remodal, .remodal-overlay, .remodal-bg, .remodal-wrapper classes.

相关链接

如果你对这篇文章有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!

关于作者

JSmiles

生命进入颠沛而奔忙的本质状态,并将以不断告别和相遇的陈旧方式继续下去。

1919 文章
23 评论
35201 人气
更多

推荐作者

随心

文章 0 评论 0

A燕芳

文章 0 评论 0

浮生未歇

文章 0 评论 2577

qq_WQViPi

文章 0 评论 0

J.Smile

文章 1 评论 0