纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.8 KiB

3 years ago
  1. /*
  2. desc:jQuery模拟盖章
  3. author:hyf
  4. date:2012-11-08
  5. */
  6. ;$.fn.zSign = function (options) {
  7. var _s = $.extend({
  8. img: '',
  9. width: 150,
  10. height: 150,
  11. offset: 30, //边界值
  12. callBack: null
  13. }, options || {});
  14. var _parent = $(this).addClass('zsign');
  15. var range = {
  16. minX: _s.offset,
  17. minY: _s.offset,
  18. maxX: $(window).width(), //扣去2个padding=8px以及2个边框1px
  19. maxY: $(window).height()
  20. };
  21. var _btnPanel = $("<div class='panels'><button class='btn btn-default add ' >盖 章</button><button class='btn btn-default cancel'>关 闭</button></div>").appendTo(_parent);
  22. var _html = "<div class='sign' style='height:" + _s.height + "px;width:" + _s.width + "px;top:" + _s.offset + "px;left:" + _s.offset + "px'><img src='" + _s.img + "' draggable='false'/><button class='btn btn-default ok' >确定</button><button class='btn btn-default del' >删除</button></div>";
  23. var _add = $('.add', _btnPanel).click(function (e) {
  24. _add.attr('disabled', 'disabled');
  25. var sign = $(_html).appendTo(_parent);
  26. $('.ok', sign).click(function () {
  27. //确定盖章
  28. sign.addClass('ok').off('mousedown').find('.btn').remove();
  29. _add.removeAttr('disabled');
  30. if (_s.callBack) {
  31. _s.callBack.call(this, { img: _s.img, top: parseInt(sign.css('top')), left: parseInt(sign.css('left')) });
  32. }
  33. });
  34. $('.del', sign).click(function () {
  35. //取消盖章
  36. sign.remove();
  37. _add.removeAttr('disabled');
  38. });
  39. //绑定移动事件
  40. sign.on('mousedown', function (e) {
  41. sign.data('x', e.clientX);
  42. sign.data('y', e.clientY);
  43. var position = sign.position();
  44. $(document).on('mousemove', function (e1) {
  45. var x = e1.clientX - sign.data('x') + position.left;
  46. var y = e1.clientY - sign.data('y') + position.top;
  47. x = x < range.minX ? range.minX : x;
  48. x = x > range.maxX ? range.maxX : x;
  49. y = y < range.minY ? range.minY : y;
  50. y = y > range.maxY ? range.maxY : y;
  51. sign.css({ left: x, top: y });
  52. }).on('mouseup', function () {
  53. $(this).off('mousemove').off('mouseup');
  54. });
  55. });
  56. });
  57. $('.cancel', _btnPanel).click(function () {
  58. var r = true;
  59. if (_add.attr('disabled') == 'disabled') {
  60. if (!confirm("未确定的盖章将被取消,确定要关闭吗?")) {
  61. r = false;
  62. }
  63. }
  64. if (r) {
  65. //删除未确定位置的盖章
  66. $('.sign:not(.ok)', _parent).remove();
  67. _btnPanel.remove();
  68. }
  69. });
  70. };