How to modify the dragging effect when using HTML5 drag-and-drop?












0















I am using angularJS for my web appliaction, and I need to write a component that contains dragging operation. When I tried to accomplish the goal, I found it hard to control the dragging effect around the pointer, since it was not a real element but an image cut (I assume). For now the dragging image is a copy of the element being drag and contains the hover effect, and even have white background from its parent. (I cut two image and display below)



So how can I modify the dragging effct with css or js?



before drag img



dragging img



I add a class "is-dragging" for the element being dragged when dragstart event is trigger, and change the style of the element being drag. But the dragging image just remain the same.



html



<div ng-if="current == -1" class="form-library">
<div ng-repeat="(key, item) in formMap"
class="form-type-items"
ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
draggable="true"
ng-dragstart="onDragStart($event, key)"
ng-dragend="onDragEnd($event)">
<div class="form-type-icon">
<i class="{{'iconfont icon-'+item.icon}}"></i>
</div>
<span class="form-type-name">{{ item.name }}</span>
</div>
</div>


scss



&:hover {
.form-type-name {
color: $brand-color-1;
}
.form-type-icon {
.iconfont {
color: $brand-color-1;
}
}
}
&.is-dragging {
opacity: 0.36;
background-color: transparent;
.form-type-name {
color: $gray-2;
}
.form-type-icon {
.iconfont {
color: $gray-2;
}
}
}









share|improve this question





























    0















    I am using angularJS for my web appliaction, and I need to write a component that contains dragging operation. When I tried to accomplish the goal, I found it hard to control the dragging effect around the pointer, since it was not a real element but an image cut (I assume). For now the dragging image is a copy of the element being drag and contains the hover effect, and even have white background from its parent. (I cut two image and display below)



    So how can I modify the dragging effct with css or js?



    before drag img



    dragging img



    I add a class "is-dragging" for the element being dragged when dragstart event is trigger, and change the style of the element being drag. But the dragging image just remain the same.



    html



    <div ng-if="current == -1" class="form-library">
    <div ng-repeat="(key, item) in formMap"
    class="form-type-items"
    ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
    draggable="true"
    ng-dragstart="onDragStart($event, key)"
    ng-dragend="onDragEnd($event)">
    <div class="form-type-icon">
    <i class="{{'iconfont icon-'+item.icon}}"></i>
    </div>
    <span class="form-type-name">{{ item.name }}</span>
    </div>
    </div>


    scss



    &:hover {
    .form-type-name {
    color: $brand-color-1;
    }
    .form-type-icon {
    .iconfont {
    color: $brand-color-1;
    }
    }
    }
    &.is-dragging {
    opacity: 0.36;
    background-color: transparent;
    .form-type-name {
    color: $gray-2;
    }
    .form-type-icon {
    .iconfont {
    color: $gray-2;
    }
    }
    }









    share|improve this question



























      0












      0








      0








      I am using angularJS for my web appliaction, and I need to write a component that contains dragging operation. When I tried to accomplish the goal, I found it hard to control the dragging effect around the pointer, since it was not a real element but an image cut (I assume). For now the dragging image is a copy of the element being drag and contains the hover effect, and even have white background from its parent. (I cut two image and display below)



      So how can I modify the dragging effct with css or js?



      before drag img



      dragging img



      I add a class "is-dragging" for the element being dragged when dragstart event is trigger, and change the style of the element being drag. But the dragging image just remain the same.



      html



      <div ng-if="current == -1" class="form-library">
      <div ng-repeat="(key, item) in formMap"
      class="form-type-items"
      ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
      draggable="true"
      ng-dragstart="onDragStart($event, key)"
      ng-dragend="onDragEnd($event)">
      <div class="form-type-icon">
      <i class="{{'iconfont icon-'+item.icon}}"></i>
      </div>
      <span class="form-type-name">{{ item.name }}</span>
      </div>
      </div>


      scss



      &:hover {
      .form-type-name {
      color: $brand-color-1;
      }
      .form-type-icon {
      .iconfont {
      color: $brand-color-1;
      }
      }
      }
      &.is-dragging {
      opacity: 0.36;
      background-color: transparent;
      .form-type-name {
      color: $gray-2;
      }
      .form-type-icon {
      .iconfont {
      color: $gray-2;
      }
      }
      }









      share|improve this question
















      I am using angularJS for my web appliaction, and I need to write a component that contains dragging operation. When I tried to accomplish the goal, I found it hard to control the dragging effect around the pointer, since it was not a real element but an image cut (I assume). For now the dragging image is a copy of the element being drag and contains the hover effect, and even have white background from its parent. (I cut two image and display below)



      So how can I modify the dragging effct with css or js?



      before drag img



      dragging img



      I add a class "is-dragging" for the element being dragged when dragstart event is trigger, and change the style of the element being drag. But the dragging image just remain the same.



      html



      <div ng-if="current == -1" class="form-library">
      <div ng-repeat="(key, item) in formMap"
      class="form-type-items"
      ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
      draggable="true"
      ng-dragstart="onDragStart($event, key)"
      ng-dragend="onDragEnd($event)">
      <div class="form-type-icon">
      <i class="{{'iconfont icon-'+item.icon}}"></i>
      </div>
      <span class="form-type-name">{{ item.name }}</span>
      </div>
      </div>


      scss



      &:hover {
      .form-type-name {
      color: $brand-color-1;
      }
      .form-type-icon {
      .iconfont {
      color: $brand-color-1;
      }
      }
      }
      &.is-dragging {
      opacity: 0.36;
      background-color: transparent;
      .form-type-name {
      color: $gray-2;
      }
      .form-type-icon {
      .iconfont {
      color: $gray-2;
      }
      }
      }






      javascript angularjs html5 drag-and-drop






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 4:23









      Maarti

      1,8523822




      1,8523822










      asked Jan 2 at 2:52









      Rice LinRice Lin

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released



          <div ng-if="current == -1" class="form-library">
          <div ng-repeat="(key, item) in formMap"
          class="form-type-items"
          ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
          draggable="true"
          on-mousedown="onMouseDown($event, key)"
          on-mouseup="onMouseUp($event)">
          <div class="form-type-icon">
          <i class="{{'iconfont icon-'+item.icon}}"></i>
          </div>
          <span class="form-type-name">{{ item.name }}</span>
          </div>
          </div>


          <script>
          function onMouseDown(e) {
          // set variable that will apply necessary class to TRUE

          // then add draggable attribute
          e.target.setAttribute('draggable', true)
          }


          function onMouseUp(e) {
          // set variable for drabble class to FALSE

          // then remove draggable attribute
          e.target.setAttribute('draggable', false)
          }

          </script>





          share|improve this answer
























          • @RiceLin Great:) I'm glad you got it working!

            – Uma
            Jan 2 at 5:09






          • 1





            Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

            – Rice Lin
            Jan 2 at 5:16






          • 1





            Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

            – Rice Lin
            Jan 2 at 7:12











          • yeah, looks like something angularJS framework overlooked!

            – Uma
            Jan 2 at 14:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000709%2fhow-to-modify-the-dragging-effect-when-using-html5-drag-and-drop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released



          <div ng-if="current == -1" class="form-library">
          <div ng-repeat="(key, item) in formMap"
          class="form-type-items"
          ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
          draggable="true"
          on-mousedown="onMouseDown($event, key)"
          on-mouseup="onMouseUp($event)">
          <div class="form-type-icon">
          <i class="{{'iconfont icon-'+item.icon}}"></i>
          </div>
          <span class="form-type-name">{{ item.name }}</span>
          </div>
          </div>


          <script>
          function onMouseDown(e) {
          // set variable that will apply necessary class to TRUE

          // then add draggable attribute
          e.target.setAttribute('draggable', true)
          }


          function onMouseUp(e) {
          // set variable for drabble class to FALSE

          // then remove draggable attribute
          e.target.setAttribute('draggable', false)
          }

          </script>





          share|improve this answer
























          • @RiceLin Great:) I'm glad you got it working!

            – Uma
            Jan 2 at 5:09






          • 1





            Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

            – Rice Lin
            Jan 2 at 5:16






          • 1





            Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

            – Rice Lin
            Jan 2 at 7:12











          • yeah, looks like something angularJS framework overlooked!

            – Uma
            Jan 2 at 14:07
















          0














          It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released



          <div ng-if="current == -1" class="form-library">
          <div ng-repeat="(key, item) in formMap"
          class="form-type-items"
          ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
          draggable="true"
          on-mousedown="onMouseDown($event, key)"
          on-mouseup="onMouseUp($event)">
          <div class="form-type-icon">
          <i class="{{'iconfont icon-'+item.icon}}"></i>
          </div>
          <span class="form-type-name">{{ item.name }}</span>
          </div>
          </div>


          <script>
          function onMouseDown(e) {
          // set variable that will apply necessary class to TRUE

          // then add draggable attribute
          e.target.setAttribute('draggable', true)
          }


          function onMouseUp(e) {
          // set variable for drabble class to FALSE

          // then remove draggable attribute
          e.target.setAttribute('draggable', false)
          }

          </script>





          share|improve this answer
























          • @RiceLin Great:) I'm glad you got it working!

            – Uma
            Jan 2 at 5:09






          • 1





            Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

            – Rice Lin
            Jan 2 at 5:16






          • 1





            Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

            – Rice Lin
            Jan 2 at 7:12











          • yeah, looks like something angularJS framework overlooked!

            – Uma
            Jan 2 at 14:07














          0












          0








          0







          It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released



          <div ng-if="current == -1" class="form-library">
          <div ng-repeat="(key, item) in formMap"
          class="form-type-items"
          ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
          draggable="true"
          on-mousedown="onMouseDown($event, key)"
          on-mouseup="onMouseUp($event)">
          <div class="form-type-icon">
          <i class="{{'iconfont icon-'+item.icon}}"></i>
          </div>
          <span class="form-type-name">{{ item.name }}</span>
          </div>
          </div>


          <script>
          function onMouseDown(e) {
          // set variable that will apply necessary class to TRUE

          // then add draggable attribute
          e.target.setAttribute('draggable', true)
          }


          function onMouseUp(e) {
          // set variable for drabble class to FALSE

          // then remove draggable attribute
          e.target.setAttribute('draggable', false)
          }

          </script>





          share|improve this answer













          It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released



          <div ng-if="current == -1" class="form-library">
          <div ng-repeat="(key, item) in formMap"
          class="form-type-items"
          ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
          draggable="true"
          on-mousedown="onMouseDown($event, key)"
          on-mouseup="onMouseUp($event)">
          <div class="form-type-icon">
          <i class="{{'iconfont icon-'+item.icon}}"></i>
          </div>
          <span class="form-type-name">{{ item.name }}</span>
          </div>
          </div>


          <script>
          function onMouseDown(e) {
          // set variable that will apply necessary class to TRUE

          // then add draggable attribute
          e.target.setAttribute('draggable', true)
          }


          function onMouseUp(e) {
          // set variable for drabble class to FALSE

          // then remove draggable attribute
          e.target.setAttribute('draggable', false)
          }

          </script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 3:20









          UmaUma

          37615




          37615













          • @RiceLin Great:) I'm glad you got it working!

            – Uma
            Jan 2 at 5:09






          • 1





            Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

            – Rice Lin
            Jan 2 at 5:16






          • 1





            Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

            – Rice Lin
            Jan 2 at 7:12











          • yeah, looks like something angularJS framework overlooked!

            – Uma
            Jan 2 at 14:07



















          • @RiceLin Great:) I'm glad you got it working!

            – Uma
            Jan 2 at 5:09






          • 1





            Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

            – Rice Lin
            Jan 2 at 5:16






          • 1





            Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

            – Rice Lin
            Jan 2 at 7:12











          • yeah, looks like something angularJS framework overlooked!

            – Uma
            Jan 2 at 14:07

















          @RiceLin Great:) I'm glad you got it working!

          – Uma
          Jan 2 at 5:09





          @RiceLin Great:) I'm glad you got it working!

          – Uma
          Jan 2 at 5:09




          1




          1





          Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

          – Rice Lin
          Jan 2 at 5:16





          Thanks! As you say, I change the class in mousedown callback before dragstart and it seems to work, even solve the problem of white background in dragging image, kind of wired.

          – Rice Lin
          Jan 2 at 5:16




          1




          1





          Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

          – Rice Lin
          Jan 2 at 7:12





          Update: I found that the real reason why styles are applied after the dragging starts is using angularJS ng-class to change the style of the element. If I simply write e.target.className += ' is-dragging'; in the dragstart callback, dragging image will be change as I need. I think this is because angularJS rerender the element after dragstrart perform when using ng-class.

          – Rice Lin
          Jan 2 at 7:12













          yeah, looks like something angularJS framework overlooked!

          – Uma
          Jan 2 at 14:07





          yeah, looks like something angularJS framework overlooked!

          – Uma
          Jan 2 at 14:07




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000709%2fhow-to-modify-the-dragging-effect-when-using-html5-drag-and-drop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas