• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kylefox/jquery-modal: The simplest possible modal for jQuery

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

kylefox/jquery-modal

开源软件地址:

https://github.com/kylefox/jquery-modal

开源编程语言:

JavaScript 70.4%

开源软件介绍:

A simple & lightweight method of displaying modal windows with jQuery.

For quick examples and demos, head to jquerymodal.com.

Why another modal plugin?

Most plugins I've found try to do too much, and have specialized ways of handling photo galleries, iframes and video. The resulting HTML & CSS is often bloated and difficult to customize.

By contrast, this plugin handles the two most common scenarios I run into

  • displaying an existing DOM element
  • loading a page with AJAX

and does so with as little HTML & CSS as possible.

Installation

You can install jquery-modal with npm:

npm install jquery-modal

or with Bower:

bower install jquery-modal

or use the hosted version from cdnjs:

<!-- Remember to include jQuery :) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

<!-- jQuery Modal -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />

Using Rails? Check out the jquery-modal-rails plugin!

jQuery Requirements: As of version 0.3.0, jQuery 1.7 is required. If you're using an earlier version of jQuery you can use the v.0.2.5 tag.

Naming conflict with Bootstrap: Bootstrap's modal uses the same $.modal namespace. If you want to use jquery-modal with Bootstrap, the simplest solution is to manually modify the name of this plugin.

Opening

Method 1: Automatically attaching to links

The simplest approach is to add rel="modal:open" to your links and use the href attribute to specify what to open in the modal.

Open an existing DOM element by ID:

<form id="login-form" class="modal">
  ...
</form>

<a href="#login-form" rel="modal:open">Login</a>

Load a remote URL with AJAX:

<a href="login.html" rel="modal:open">Login</a>

Method 2: Manually

You can manually open a modal by calling the .modal() method on the element:

<form id="login-form" class="modal">
  ...
</form>
$('#login-form').modal();

You can also invoke .modal() directly on links:

<a href="#ex5" data-modal>Open a DOM element</a>
<a href="ajax.html" data-modal>Open an AJAX modal</a>
$('a[data-modal]').click(function(event) {
  $(this).modal();
  return false;
});

Compatibility Fallback

You can provide a clean fallback for users who have JavaScript disabled by manually attaching the modal via the data-modal attribute. This allows you to write your links pointing to the href as normal (fallback) while enabling modals where JavaScript is enabled.

<!-- By default link takes user to /login.html -->
<a href="/login.html" data-modal="#login-modal">Login</a>

<!-- Login modal embedded in page -->
<div id="login-modal" class="modal">
  ...
</div>

<!-- For browsers with JavaScript, open the modal. -->
<script>
  $(function() {
    $('a[data-modal]').on('click', function() {
      $($(this).data('modal')).modal();
      return false;
    });
  });
</script>

Fade Transitions

By default the overlay & window appear instantaneously, but you can enable a fade effect by specifying the fadeDuration option.

$('a.open-modal').click(function(event) {
  $(this).modal({
    fadeDuration: 250
  });
  return false;
});

This will fade in the overlay and modal over 250 milliseconds simultaneously. If you want the effect of the overlay appearing before the window, you can specify the fadeDelay option. This indicates at what point during the overlay transition the window transition should begin.

So if you wanted the window to fade in when the overlay's was 80% finished:

$(elm).modal({
  fadeDuration: 250,
  fadeDelay: 0.80
});

Or, if you wanted the window to fade in a few moments after the overlay transition has completely finished:

$(elm).modal({
  fadeDuration: 250,
  fadeDelay: 1.5
});

The fadeDelay option only applies when opening the modal. When closing the modal, both the modal and the overlay fade out simultaneously according to the fadeDuration setting.

Fading is the only supported transition.

Closing

Because there can be only one modal active at a single time, there's no need to select which modal to close:

$.modal.close();

Similar to how links can be automatically bound to open modals, they can be bound to close modals using rel="modal:close":

<a href="#close" rel="modal:close">Close window</a>

(Note that modals loaded with AJAX are removed from the DOM when closed).

Checking current state

  • Use $.modal.isActive() to check if a modal is currently being displayed.
  • Use $.modal.getCurrent() to retrieve a reference to the currently active modal instance, if any.

Options

These are the supported options and their default values:

$.modal.defaults = {
  closeExisting: true,    // Close existing modals. Set this to false if you need to stack multiple modal instances.
  escapeClose: true,      // Allows the user to close the modal by pressing `ESC`
  clickClose: true,       // Allows the user to close the modal by clicking the overlay
  closeText: 'Close',     // Text content for the close <a> tag.
  closeClass: '',         // Add additional class(es) to the close <a> tag.
  showClose: true,        // Shows a (X) icon/link in the top-right corner
  modalClass: "modal",    // CSS class added to the element being displayed in the modal.
  blockerClass: "modal",  // CSS class added to the overlay (blocker).

  // HTML appended to the default spinner during AJAX requests.
  spinnerHtml: '<div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div>',

  showSpinner: true,      // Enable/disable the default spinner during AJAX requests.
  fadeDuration: null,     // Number of milliseconds the fade transition takes (null means no transition)
  fadeDelay: 1.0          // Point during the overlay's fade-in that the modal begins to fade in (.5 = 50%, 1.5 = 150%, etc.)
};

Events

The following events are triggered on the modal element at various points in the open/close cycle (see below for AJAX events).

$.modal.BEFORE_BLOCK = 'modal:before-block';    // Fires just before the overlay (blocker) appears.
$.modal.BLOCK = 'modal:block';                  // Fires after the overlay (block) is visible.
$.modal.BEFORE_OPEN = 'modal:before-open';      // Fires just before the modal opens.
$.modal.OPEN = 'modal:open';                    // Fires after the modal has finished opening.
$.modal.BEFORE_CLOSE = 'modal:before-close';    // Fires when the modal has been requested to close.
$.modal.CLOSE = 'modal:close';                  // Fires when the modal begins closing (including animations).
$.modal.AFTER_CLOSE = 'modal:after-close';      // Fires after the modal has fully closed (including animations).

The first and only argument passed to these event handlers is the modal object, which has four properties:

modal.$elm;       // Original jQuery object upon which modal() was invoked.
modal.options;    // Options passed to the modal.
modal.$blocker;   // The overlay element.
modal.$anchor;    // Anchor element originating the event.

So, you could do something like this:

$('#purchase-form').on($.modal.BEFORE_CLOSE, function(event, modal) {
  clear_shopping_cart();
});

AJAX

Basic support

jQuery Modal uses $.get for basic AJAX support. A simple spinner will be displayed by default (if you've included modal.css) and will have the class modal-spinner. If you've set the modalClass option, the spinner will be prefixed with that class name instead.

You can add text or additional HTML to the spinner with the spinnerHtml option, or disable the spinner entirely by setting showSpinner: false.

The default spinner is from the excellent SpinKit by Tobias Ahlin


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap