@4lolo/js-year-calendar 中文文档教程

发布于 4年前 浏览 47 项目主页 更新于 3年前

js-year-calendar

一个完全可定制的年历小部件

CircleCICodeCovNPM

这个库也可用于:

React.jsVue.js

Requirements

这个插件使用纯javascript。 不需要图书馆。

Installation

您可以使用以下方法获取小部件:

  • From the GitHub repository
  • From the Node package manager, using the following command: npm install js-year-calendar
  • From Yarn, using the following command: yarn add js-year-calendar
  • From the CDN, by adding the following script directly in your HTML page:

AND

Initialization

如果您使用的是 javascript 模块,请不要忘记导入库:

import Calendar from 'js-year-calendar';
import 'js-year-calendar/dist/js-year-calendar.css';

Usage

您可以使用以下 javascript 代码创建日历:

new Calendar('.calendar')

new Calendar(document.querySelector('.calendar'));

Where .calendar 是应包含日历的 DIV 元素的选择器。

如果您不想使用 javascript 初始化日历,您也可以使用以下 HTML

<div data-provide="calendar"></div>

页面加载完成时将自动创建日历

Using options

您可以指定选项来自定义日历:

new Calendar('.calendar', {
    style: 'background',
    minDate: new Date()
})

您可以在中找到详尽的选项列表文档

Language

如果你想使用不同语言的日历,你应该导入你想使用的语言对应的语言环境文件,然后设置日历的 language 属性:

import Calendar from 'js-year-calendar';
import 'js-year-calendar/locales/js-year-calendar.fr';

或者

<script src="https://unpkg.com/js-year-calendar@latest/dist/js-year-calendar.umd.min.js"></script>
<script src="https://unpkg.com/js-year-calendar@latest/locales/js-year-calendar.fr.js"></script>

Then

new Calendar('.calendar', {
    language: 'fr'
})

The list of available语言在此处可用,

Updating calendar

您可以在实例化后更新日历:

const calendar = new Calendar('.calendar');

calendar.setStyle('background');
calendar.setMaxDate(new Date());

您可以找到文档中的详尽方法列表。

Events

将事件绑定到日历

const calendar = new Calendar('.calendar', {
    clickDay: function(e) {
        alert('Click on day ' + e.date.toString());
    }
});

您可以在初始化时或之后

new Calendar('.calendar');
document.querySelector('.calendar').addEventListener('clickDay', function(e) {
    alert('Click on day ' + e.date.toString());
});

您可以在文档。

Migrating from bootstrap-year-calendar

此小部件基于 bootstrap-year-calendar 小部件。 如果您使用的是这个小部件,这些是应用到成功迁移项目的修改:

Initialization

该项目不再使用 jQuery,因此日历的初始化将使用纯 Javascript。

旧代码:

$('.calendar').calendar({ /* Options */ })

将被替换为:

new Calendar('.calendar', { /* Options */ });

或者

new Calendar($('.calendar').get(0), { /* Options */ });
// Use ".get(0)" to get the DOM element from the jQuery element

Get the calendar from the DOM element

鉴于小部件不依赖于 jQuery,将无法再从 DOM 元素获取日历实例:

$('.calendar').data('calendar').set...();

您必须自己存储日历实例:

const calendar = new Calendar('.calendar');

...

calendar.set...();

What next

查看文档examples 页面来发现所有的功能。

js-year-calendar

A fully customizable year calendar widget

CircleCICodeCovNPM

This library is also available for:

React.jsVue.js

Requirements

This plugin uses pure javascript. No library is required.

Installation

You can get the widget using the following methods:

  • From the GitHub repository
  • From the Node package manager, using the following command: npm install js-year-calendar
  • From Yarn, using the following command: yarn add js-year-calendar
  • From the CDN, by adding the following script directly in your HTML page:

<script src="https://unpkg.com/js-year-calendar@latest/dist/js-year-calendar.min.js"></script>

AND

<link rel="stylesheet" type="text/css" href="https://unpkg.com/js-year-calendar@latest/dist/js-year-calendar.min.css" />

Initialization

If you're using javascript modules, don't forget to import the library:

import Calendar from 'js-year-calendar';
import 'js-year-calendar/dist/js-year-calendar.css';

Usage

You can create a calendar using the following javascript code :

new Calendar('.calendar')

Or

new Calendar(document.querySelector('.calendar'));

Where .calendar is the selector of a DIV element that should contain your calendar.

You can also use the following HTML if you don't want to use javascript to initialize the calendar

<div data-provide="calendar"></div>

The calendar will be automatically created when the page will finish loading

Using options

You can specify options to customize the calendar:

new Calendar('.calendar', {
    style: 'background',
    minDate: new Date()
})

You can find the exhaustive list of options in the documentation.

Language

If you want to use the calendar in a different language, you should import the locale file corresponding to the language you want to use, and then set the language prop of the calendar:

import Calendar from 'js-year-calendar';
import 'js-year-calendar/locales/js-year-calendar.fr';

OR

<script src="https://unpkg.com/js-year-calendar@latest/dist/js-year-calendar.umd.min.js"></script>
<script src="https://unpkg.com/js-year-calendar@latest/locales/js-year-calendar.fr.js"></script>

Then

new Calendar('.calendar', {
    language: 'fr'
})

The list of available languages is available here

Updating calendar

You can update the calendar after being instantiated:

const calendar = new Calendar('.calendar');

calendar.setStyle('background');
calendar.setMaxDate(new Date());

You can find the exhaustive list of methods in the documentation.

Events

You can bind events to the calendar at initialization

const calendar = new Calendar('.calendar', {
    clickDay: function(e) {
        alert('Click on day ' + e.date.toString());
    }
});

or later

new Calendar('.calendar');
document.querySelector('.calendar').addEventListener('clickDay', function(e) {
    alert('Click on day ' + e.date.toString());
});

You can find the exhaustive list of events in the documentation.

Migrating from bootstrap-year-calendar

This widget is based on the bootstrap-year-calendar widget. If you were using this widget, these are the modifications to apply to successfully migrate your project:

Initialization

The project doesn't use jQuery anymore, so the initialization of the calendar will be using pure Javascript.

The old code:

$('.calendar').calendar({ /* Options */ })

Will be replaced by:

new Calendar('.calendar', { /* Options */ });

Or

new Calendar($('.calendar').get(0), { /* Options */ });
// Use ".get(0)" to get the DOM element from the jQuery element

Get the calendar from the DOM element

Given that the widget doesn't rely on jQuery, it won't be possible to get the calendar instance from the DOM element anymore:

$('.calendar').data('calendar').set...();

You will have to store the instance of the calendar by yourself:

const calendar = new Calendar('.calendar');

...

calendar.set...();

What next

Check the documentation and examples pages to discover all the functionalities.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文