dropdown.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @Name: layui.dropdown 基于layui的下拉菜单
  3. * @Author: 李祥
  4. * @License:MIT
  5. * 最近修改时间: 2018/11/16
  6. */
  7. layui.define(["jquery"], function (exports) {
  8. var $ = layui.jquery;
  9. // 记录dropdown的上一次点击的按钮dom
  10. var dropdownTarget = "";
  11. var dropdown = function (elem, data, callback) {
  12. var $elem=null;
  13. if(typeof elem ==="string"){
  14. $elem=$(elem).next(".layui-border-box").find(".urp-dropdown-table .urp-dropdown-btn");
  15. }else{
  16. callback=data;
  17. data=elem;
  18. $elem=$(".urp-dropdown-table .urp-dropdown-btn");
  19. }
  20. $elem.off("click").on("click", function (event) {
  21. var index = $(this).parents("tr").attr("data-index");
  22. var d = data[index];
  23. var event = event || window.event;
  24. var options = callback(d);
  25. if (Object.prototype.toString.call(options) !== "[object Array]") {
  26. layui.hint().error('dropdown回调参数错误(格式应为数组)');
  27. return;
  28. }
  29. layui.stope(event);
  30. // 点击的按钮dom
  31. var target = event.target || event.srcElement;
  32. target = $(target).parents(".urp-dropdown-table").children(".urp-dropdown-btn").get(0);
  33. if ($(".urp-dropdown-menu-table").length > 0) {
  34. $(".urp-dropdown-menu-table").remove();
  35. // 判断两次点击的是否是同一个按钮
  36. if ((dropdownTarget.get(0)).isEqualNode($(target).parents("tr").get(0))) {
  37. return;
  38. }
  39. }
  40. // 下拉菜单拼接
  41. var str = '<ul class="urp-dropdown-menu-table ">';
  42. options.forEach(function (val, index) {
  43. var icon = val.icon || "";
  44. if (icon.indexOf("fa-") > -1) {
  45. icon = icon.indexOf("fa ") === -1 ? ("fa " + icon) : icon;
  46. } else {
  47. icon = icon.indexOf("layui-icon ") === -1 ? ("layui-icon " + icon) : icon;
  48. }
  49. var title = val.title || "按钮" + (index + 1);
  50. var url = val.url || "javascript:;";
  51. str += '<li><a href="' + url + '"><i class="' + icon + '"></i> ' + title + '</a></li>'
  52. })
  53. str += '</ul>';
  54. $(document.body).append(str);
  55. // 位置与事件绑定
  56. $(".urp-dropdown-menu-table").css({
  57. right: $(document.body).width() - $(target).offset().left - $(target).width() - 11,
  58. top: $(target).offset().top + $(target).height() + 10 - $(document).scrollTop()
  59. }).find("li").each(function (index, item) {
  60. options[index]["event"] && $(item).off().on("click", options[index]["event"])
  61. })
  62. // 判断是否需要朝上
  63. if ($(document.body).height() < $(target).offset().top + 50 + $(".urp-dropdown-menu-table").height()) {
  64. $(".urp-dropdown-menu-table").addClass("urp-dropdown-menu-table-top").css({
  65. top: $(target).offset().top - $(".urp-dropdown-menu-table").height() - 10 - $(document).scrollTop()
  66. })
  67. }
  68. // 更新上一次按钮dom
  69. dropdownTarget = $(target).parents("tr");
  70. })
  71. }
  72. var documentEvent=function() {
  73. $(document.body).on("click", '.urp-dropdown:not(.urp-dropdown-table)', function (event) {
  74. event.stopPropagation();
  75. var onOff = true;
  76. // 判断当前是否打开,是的话,直接关闭,否则先关闭所有的,再打开当前的
  77. if ($(this).hasClass("open")) {
  78. onOff = false;
  79. }
  80. $(".urp-dropdown").removeClass("open");
  81. if (onOff) {
  82. $(this).addClass("open");
  83. }
  84. })
  85. $(document.body).on("click", function () {
  86. $(".urp-dropdown").removeClass("open");
  87. $(".urp-dropdown-menu-table").remove();
  88. });
  89. // 滚动移除
  90. window.addEventListener("scroll",function() {
  91. $(".urp-dropdown-menu-table").remove();
  92. },true);
  93. }
  94. documentEvent();
  95. exports('dropdown', dropdown);
  96. })