javascript – 从fullcalendar中删除元素(通过拖动到垃圾桶)

前端之家收集整理的这篇文章主要介绍了javascript – 从fullcalendar中删除元素(通过拖动到垃圾桶)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个fullcalendar,外部元素被拖到它上面.我比较新的 jquery.我不知道如何获取被拖动到“垃圾桶”图标的对象的ID.我只想将项目从日历中拖出到图像中,当我放开鼠标时,该项目被删除.

这是我的代码…..

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html>
  3. <head>
  4. <link rel='stylesheet' type='text/css' href='../fullcalendar.css' />
  5. <script type='text/javascript' src='../jquery/jquery.js'></script>
  6. <script type='text/javascript' src='../jquery/jquery-ui-custom.js'></script>
  7. <script type='text/javascript' src='../fullcalendar.min.js'></script>
  8. <script type='text/javascript'>
  9.  
  10. $(document).ready(function() {
  11.  
  12.  
  13. /* initialize the external events
  14. -----------------------------------------------------------------*/
  15.  
  16. $('#external-events div.external-event').each(function() {
  17.  
  18. // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
  19. // it doesn't need to have a start or end
  20. var eventObject = {
  21. title: $.trim($(this).text()) // use the element's text as the event title
  22. };
  23.  
  24. // store the Event Object in the DOM element so we can get to it later
  25. $(this).data('eventObject',eventObject);
  26.  
  27.  
  28.  
  29. // make the event draggable using jQuery UI
  30. $(this).draggable({
  31. zIndex: 999,revert: true,// will cause the event to go back to its
  32. revertDuration: 0 // original position after the drag
  33. });
  34.  
  35. });
  36.  
  37.  
  38.  
  39.  
  40. /* initialize the calendar
  41. -----------------------------------------------------------------*/
  42.  
  43. $('#calendar').fullCalendar({
  44. header: {
  45. left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'
  46. },editable: true,droppable: true,// this allows things to be dropped onto the calendar !!!
  47. drop: function(date,allDay) { // this function is called when something is dropped
  48.  
  49. // retrieve the dropped element's stored Event Object
  50. var originalEventObject = $(this).data('eventObject');
  51.  
  52. // we need to copy it,so that multiple events don't have a reference to the same object
  53. var copiedEventObject = $.extend({},originalEventObject);
  54.  
  55. // assign it the date that was reported
  56. copiedEventObject.start = date;
  57. copiedEventObject.allDay = allDay;
  58.  
  59.  
  60.  
  61. // render the event on the calendar
  62. // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
  63. $('#calendar').fullCalendar('renderEvent',copiedEventObject,true);
  64.  
  65. // is the "remove after drop" checkBox checked?
  66. if ($('#drop-remove').is(':checked')) {
  67. // if so,remove the element from the "Draggable Events" list
  68. $(this).remove();
  69.  
  70. }
  71. }
  72.  
  73. });
  74.  
  75. });
  76.  
  77. </script>
  78. <style type='text/css'>
  79.  
  80. body {
  81. margin-top: 40px;
  82. text-align: center;
  83. font-size: 14px;
  84. font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
  85. }
  86.  
  87. #wrap {
  88. width: 1100px;
  89. margin: 0 auto;
  90. }
  91.  
  92. #external-events {
  93. float: left;
  94. width: 150px;
  95. padding: 0 10px;
  96. border: 1px solid #ccc;
  97. background: #eee;
  98. text-align: left;
  99. }
  100.  
  101. #external-events h4 {
  102. font-size: 16px;
  103. margin-top: 0;
  104. padding-top: 1em;
  105. }
  106.  
  107. .external-event { /* try to mimick the look of a real event */
  108. margin: 10px 0;
  109. padding: 2px 4px;
  110. background: #3366CC;
  111. color: #fff;
  112. font-size: .85em;
  113. cursor: pointer;
  114. }
  115.  
  116. #external-events p {
  117. margin: 1.5em 0;
  118. font-size: 11px;
  119. color: #666;
  120. }
  121.  
  122. #external-events p input {
  123. margin: 0;
  124. vertical-align: middle;
  125. }
  126.  
  127. #calendar {
  128. float: right;
  129. width: 900px;
  130. }
  131.  
  132. </style>
  133. </head>
  134. <body>
  135. <div id='wrap'>
  136.  
  137. <div id='external-events'>
  138. <h4>Draggable Events</h4>
  139. <div class='external-event'>even1</div>
  140. <div class='external-event'>even2</div>
  141.  
  142. <p>
  143. <input type='checkBox' id='drop-remove' /> <label for='drop-remove'>remove after drop</label>
  144. </p>
  145. </div>
  146.  
  147. <div id='calendar'></div>
  148.  
  149. <img src="redmond/images/trash.png" id="trash">
  150.  
  151. <div style='clear:both'></div>
  152. </div>
  153. </body>
  154. </html>

解决方法

完整的教程,如何添加“移至垃圾桶”功能到fullcalendar

HERE IS DEMO

如果不想使用droppable:

从fullcalendar.css删除这行

  1. .fc-view
  2. {
  3. /* prevents dragging outside of widget */
  4. width: 100%;
  5. overflow: hidden;
  6. }

在fullcalenar.js中找到(行cca 6086)

  1. function eachEventElement(event,exceptElement,funcName) {
  2. var elements = eventElementsByID[event._id],i,len = elements.length;
  3. for (i=0; i<len; i++) {
  4. if (!exceptElement || elements[i][0] != exceptElement[0]) {
  5. elements[i][funcName]();
  6. }
  7. }
  8. }

并更改为:

  1. function eachEventElement(event,i;
  2. if (elements != null) {
  3. var len = elements.length;
  4. for (i = 0; i < len; i++) {
  5. if (!exceptElement || elements[i][0] != exceptElement[0]) {
  6. elements[i][funcName]();
  7. }
  8. }
  9. }
  10. }

在js:

  1. //actually cursor position
  2. var currentMousePos = {
  3. x: -1,y: -1
  4. };
  5.  
  6. //set actually cursor pos
  7. jQuery(document).ready(function () {
  8.  
  9. jQuery(document).on("mousemove",function (event) {
  10. currentMousePos.x = event.pageX;
  11. currentMousePos.y = event.pageY;
  12. });
  13.  
  14. });
  15.  
  16. //check if cursor is in trash
  17. function isElemOverDiv() {
  18. var trashEl = jQuery('#calendarTrash');
  19.  
  20. var ofs = trashEl.offset();
  21.  
  22. var x1 = ofs.left;
  23. var x2 = ofs.left + trashEl.outerWidth(true);
  24. var y1 = ofs.top;
  25. var y2 = ofs.top + trashEl.outerHeight(true);
  26.  
  27. if (currentMousePos.x >= x1 && currentMousePos.x <= x2 &&
  28. currentMousePos.y >= y1 && currentMousePos.y <= y2) {
  29. return true;
  30. }
  31. return false;
  32. }
  33.  
  34. //fullcalendar mouSEOver callback
  35.  
  36. eventMouSEOver: function (event,jsEvent) {
  37. $(this).mousemove(function (e) {
  38. var trashEl = jQuery('#calendarTrash');
  39. if (isElemOverDiv()) {
  40. if (!trashEl.hasClass("to-trash")) {
  41. trashEl.addClass("to-trash");
  42. }
  43. } else {
  44. if (trashEl.hasClass("to-trash")) {
  45. trashEl.removeClass("to-trash");
  46. }
  47.  
  48. }
  49. });
  50. },//fullcalendar eventdragstop callback
  51. eventDragStop: function (event,jsEvent,ui,view) {
  52. if (isElemOverDiv()) {
  53.  
  54. jQuery('#fr-calendar').fullCalendar('removeEvents',event.id);
  55.  
  56. var trashEl = jQuery('#calendarTrash');
  57. if (trashEl.hasClass("to-trash")) {
  58. trashEl.removeClass("to-trash");
  59. }
  60. }
  61. },

在fullcalendar设置选项dragRevertDuration:0,

在fullcalendar声明中添加加载回调函数附加trashcalendar:

  1. loading: function (bool) {
  2. if (!bool) {
  3. jQuery('.fc-header-left').append('<div id="calendarTrash" class="calendar-trash"><img src="' + imagePath + '/cal-trash.png"></img></div>');
  4.  
  5. }
  6. },

垃圾的css:

  1. div.calendar-trash{
  2. float: left;
  3. padding-right: 8px;
  4. margin-right:5px;
  5. padding-left:8px;
  6. padding-top: 3px;
  7. cursor: pointer;
  8. }
  9.  
  10. .to-trash{
  11. background-color:#EAEAEA;
  12. -webkit-border-radius: 5em;
  13. border-radius: 5em;
  14. }

如果加载回调不起作用,请在jquery文档就绪功能的末尾添加垃圾桶.

  1. foo.JFS('.fc-header-left').append('<div id="calendarTrash" class="calendar-trash"><img src="/images/cal-trash.png"></img></div>');

垃圾图标:

猜你在找的JavaScript相关文章