iOS风格通知徽章插件 iOSBadge

  • 源码大小:294.46KB
  • 所需积分:1积分
  • 源码编号:19JP-3613
  • 浏览次数:576次
  • 最后更新:2023年07月02日
  • 所属栏目:其他
本站默认解压密码:19jp.com 或 19jp_com

简介

iOSBadge是一个简单的jQuery和Vanilla JavaScript插件,它为元素添加了iOS风格的、完全可定制的通知徽章。

之所以创建此插件,是因为在许多情况下,一个网站会有许多未读项目,无论是电子邮件还是论坛帖子。在iPhone和iPad等iOS设备上,有一个通知徽章,显示用户有多少物品。这个插件的想法是在网页中复制类似的东西。

如何使用它:

1.加载iOSBadge插件的文件。

  1. <link rel="stylesheet" href="/path/to/dist/iosbadge.min.css" />
  2. <script src="/path/to/dist/iosbadge.min.js"></script>
  3. <!-- jQuery Is OPTIONAL -->
  4. <script src="/path/to/cdn/jquery.slim.min.js"></script>

2.将默认通知徽章添加到您指定的元素中。请注意,目标元素必须相对定位。

  1. <div id="demo" class="app">
  2. ...
  3. </div>
  1. .app {
  2. position: relative;
  3. /* your own styles here */
  4. }
  1. // Vanilla JS
  2. var icon = document.getElementById('demo');
  3. var badge = new IOSBadge(icon, {
  4. // options here
  5. });
  6.  
  7. // OR jQuery
  8. $("#app").iosbadge({
  9. // options here
  10. });

3.自定义通知徽章:

  1. {
  2.  
  3. // number of notifications
  4. content: 1,
  5.  
  6. // 20, 22, 24, 26, 28, 30, 32, 34, and 36
  7. size: 20,
  8.  
  9. // 'red', 'blue', 'green', 'grey',and 'ios'
  10. theme: 'red',
  11.  
  12. // 'top-left', 'top-right', 'bottom-left' or 'bottom-right'
  13. position: 'top-right',
  14.  
  15. // namespace
  16. namespace: 'iosb',
  17.  
  18. }

4.可用的API方法允许您以编程方式控制通知标记。

  1. // set the number of notifications
  2. badge.setContent(number);
  3.  
  4. // get the number of notifications
  5. badge.getContent();
  6.  
  7. // set the position of the badge
  8. badge.setPosition(position);
  9.  
  10. // set the theme of the badge
  11. badge.setTheme(theme);
  12.  
  13. // set the size of the badge
  14. badge.setSize(size);
  15.  
  16. // increase & decrease
  17. badge.decreaseBy(number);
  18. badge.increaseBy(number);
  19.  
  20. // show & hide the bage
  21. badge.show(number);
  22. badge.hide(number);

5.请注意,这些方法中的大多数都是可链接的。

  1. badge
  2. .setContent(5)
  3. .increaseBy(3)
  4. .show();

预览截图