表单/表格

仿github表情符号自动完成jQuery插件

阿里云


At.js 是一款仿 github 表情符号和 at 自动完成 jquery 插件。通过 At.js 插件,可以在表单,后某个可编辑的 div 元素,或某些所见即所得的文本编辑器中,通过快捷方式来自动补全表情符号,或 at 某人的操作。
At.js 插件的特点还有:

支持 IE7+的 textarea 元素

也想出现在这里?联系我们
创客主机

支持 HTML5 contentEditable 元素(不包括 IE8)

可以监听任何字符,不单单只是@字符

使用模板来返回格式化数据

支持键盘控制

支持 AMD

使用方法

该插件依赖于 jquery.caret.js。在使用是要引入 jquery,jquery.caret.js,jquery.atwho.css 和 jquery.atwho.js 文件。

  1. <link href="css/jquery.atwho.css" rel="stylesheet">
  2. <script src="http://code.jquery.com/jquery.js"></script>
  3. <script src="js/jquery.caret.js"></script>
  4. <script src="js/jquery.atwho.js"></script>

初始化插件

At.js 插件最基本的用法如下:

  1. $('.atwho-inputor').atwho({
  2.   at: "@",
  3.   data: ["one", "two", "three"],
  4. }).atwho({
  5.   at: ":",
  6.   data: ["+1", "-1", "smile"]
  7. });

配置参数

At.js 插件的可用配置参数如下:

  1. $('.atwho-inputor').atwho({
  2.     // key char for observing such as `@`
  3.     at: void 0,
  4.     /*
  5.         alias name of `at`
  6.         it would be an id attribute of the popup view.
  7.     */
  8.     alias: void 0,
  9.     /*
  10.          should be a plain object *Array* or a *URL*
  11.          would save *Array* directly.
  12.          would load and save remote JSON data by *URL*
  13.      */
  14.     data: null,
  15.     /*
  16.          Would render the passed value at the top of the selector popup.
  17.     */
  18.     headerTpl: "<h3>Select a user</h3>",
  19.     /*
  20.          would eval it and assign value of the key contained in `${}`
  21.          key-value ( {'name': "one"} ) is an item in `data` Array.
  22.          Alternatively, this can be a function accepting one data item as a parameter.
  23.     */
  24.     displayTpl: "<li>${name}</li>",
  25.     /*
  26.          It will be evaluated and inserted in the inputor.
  27.          `atwho-at` is the `at` for runtime by default.
  28.          You change it into anything you want.
  29.     */
  30.     insertTpl: "${atwho-at}${name}",
  31.     /*
  32.         There are several data processors that can be overriden here such as `filter`.
  33.         we will cover it later.
  34.     */
  35.     callbacks: DEFAULT_CALLBACKS,
  36.     /*
  37.         would matching item by test against the value of this `search_key` with query string.
  38.     */
  39.     searchKey: "name",
  40.     /*
  41.         limit number of items to show in popup list.
  42.     */
  43.     limit: 5,
  44.     /*
  45.         setting the max length of the string after `at` that would be matched
  46.         It will stop matching if the query string is longer than `max_len`.
  47.     */
  48.     maxLen: 20,
  49.     /*
  50.         if `yes`, At.js will match the query with a spaaace before the `at`.
  51.     */
  52.     startWithSpace: true,
  53.     // time in ms to persist the popup menu for after blurring from the invoking text field
  54.     displayTimeout: 300,
  55.     // highlight_first suggestion in popup menu
  56.     highlightFirst: true,
  57.     // delay time trigger At.js while typing. For example: delay: 400
  58.     delay: null,
  59.     // suffix for inserting string.
  60.     suffix: undefined,
  61.     // don't show dropdown view without `suffix`
  62.     hideWithoutSuffix: false,
  63.     // Add attribute to the inserted element for contenteditable mode
  64.     // check out the issue: https://github.com/ichord/At.js/issues/253#issuecomment-75694945
  65.     editableAtwhoQueryAttrs: {}
  66. });

应用示例

你可以自定义要在自动完成列表中显示的内容,如显示一个头像,或一个表情符号。一个有效的模板必须是一个<li>元素,例如:

  1. <li>这里可以放任何东西</li>

你可以在<li>元素中放任何 HTML 代码,如一张图片。

  1. var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];
  2. var emojisList = $.map(emojis, function(value, i) {
  3.   return {'id':i, 'name':value};
  4. });
  5. //http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png
  6. $(".container textarea").atwho({
  7.   at: ':',
  8.   displayTpl: "<li><img src='icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
  9.   insertTpl: ":${name}:",
  10.   data: emojisList
  11. });

注册多个 at 关键字

At.js 可以监听多个 at 关键字,并且每个关键字都有自己的配置。

  1. var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"];
  2.  
  3. var names = ["Jacob", "Isabella", "Ethan", "Emma", "Michael", "Olivia", "Alexander", "Sophia", "William", "Ava", "Joshua", "Emily", "Daniel", "Madison", "Jayden", "Abigail", "Noah", "Chloe", "你好", "你你你"];
  4.  
  5. var emojisList = $.map(emojis, function(value, i) {
  6.   return {'id':i, 'name':value};
  7. });
  8.  
  9. var issues = [
  10.   { name: "1", content: "stay foolish"},
  11.   { name: "2", content: "stay hungry"},
  12.   { name: "3", content: "stay heathly"},
  13.   { name: "4", content: "this happiess"},
  14. ];
  15.  
  16. $(".container textarea")
  17.   .atwho({
  18.     at: "@",
  19.     data: names
  20.   })
  21.   .atwho({
  22.     at: "#", 
  23.     displayTpl: '<li>${name} <small>${content}</small></li>',
  24.     data: issues
  25.   })
  26.   .atwho({
  27.     at: ":", 
  28.     displayTpl: "<li><img src='icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>",
  29.     insertTpl: ':${name}:',
  30.     data: emojisList
  31.   });

远程加载更多数据

当你从某个 URL 获取数据时,At.js 将执行 AJAX 请求异步加载更多 json 数据。

  1. $('textarea').atwho({
  2.         at: "@", 
  3.         data: "http://www.atjs.com/users.json",
  4.         limit: 7
  5.     });

定义查询条件

如果你想支持 unicode 编码,你可以自定义查询条件。

  1. $('#inputor').atwho({
  2.         at: "@", 
  3.         callbacks: {
  4.             matcher: function(flag, subtext) {
  5.                 var match, matched, regexp;
  6.                 regexp = new XRegExp('(\\s+|^)' + flag + '(\\p{L}+)$', 'gi');
  7.                 match = regexp.exec(subtext);
  8.                 // ... get matched result
  9.                 return matched;
  10.             }
  11.             //, ... others callbacks
  12.         }
  13.     });

Github 地址为:https://github.com/ichord/At.js

仿 github 表情符号自动完成 jQuery 插件

已有 406 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!