Code Monkey home page Code Monkey logo

setdragimage-for-ie's Introduction

Custom ghost image support for HTML5 Drag & Drop in Internet Explorer

In the HTML5 spec it is possible to make elements drag-and-drop-able. This is not a new info. However, when you drag a thing whatever gets dragged is a “Ghost Image” of it.

This image is created automatically, so you do not have to create it yourself. But, if a custom image is desired the event.DataTransfer.setDragImage()method can be used to set the custom image to be used.

The image will typically be an image element but it can also be a canvas or any other image element. The method's x and y coordinates are offsets where the image should appear relative to the mouse pointer.

void dataTransfer.setDragImage(img, xOffset, yOffset);

If you plan to use custom ghost image in your application, you will find out the hard way that Internet Explorer does not support the setDragImage method.

There is a hack to make it work in Internet Explorer.

How does this hack work?

I noticed that if you make a change to the element’s style (adding a class that changes the appearance) inside the dragstartevent and then removing it immediately in a setTimeout, Internet Explorer will make a bitmap copy of the modified element and will use it for dragging. So, what this library actually does is, implement the setDragImage method that changes the target's element style by adding a class that includes the image that you want to appear while dragging, and then removes it. In this way, the browser displays the temporary style of the element as the drag image.

Clone and Play !!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.