Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 912521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:23:44+00:00 2026-05-15T17:23:44+00:00

I am using JavaScript and trying to make a skew effect on a div.

  • 0

I am using JavaScript and trying to make a skew effect on a div.

First, take a look at this video: http://www.youtube.com/watch?v=ny5Uy81smpE (0:40-0:60 should be enough). The video shows some nice transformations (skew) when you move the window. What I want to do is the same thing: to skew a div when I move it.

Currently I just have a plain simple div:

<div id="a" style="background: #0f0; position: absolute; left: 0px; top: 0px;"></div>

I have done a simple skew transformation using the CSS3’s transform property, but my implementation is buggy. Are there good tutorials or maths sites or resources that describe the logic behind this? I know JavaScript and CSS well enough to implement, if I just knew the logic and maths. I tried reading FreeWins source code, but I am not good in C.

I am accepting any resourceful answers or pseudo code. My dragging system is part of a bigger system, thus, now that I post some real code, it does not work without giving you the entire system (that I can not do at this point). So, you can’t run this code as is. The code I use is this (slightly modified though) to demonstrate my idea:

/**
 * The draggable object.
 */
Draggable = function(targetElement, options) {
    this.targetElement = targetElement;

    // Initialize drag data.
    this.dragData = {
        startX: null,
        startY: null,
        lastX: null,
        lastY: null,
        offsetX: null,
        offsetY: null,
        lastTime: null,
        occuring: false
    };

    // Set the cursor style.
    targetElement.style.cursor = 'move';

    // The element to move.
    this.applyTo = options.applyTo || targetElement;

    // Event methods for "mouse down", "up" and "move".
    // Mouse up and move are binded to window.
    // We can attach and deattach "move" and "up" events as needed.
    var me = this;

    targetElement.addEventListener('mousedown', function(event) {
        me.onMouseDown.call(me, event);
    }, false);

    this.mouseUp = function(event) {
        me.onMouseUp.call(me, event);
    };

    this.mouseMove = function(event) {
        me.onMouseMove.call(me, event);
    };
};

/**
 * The mouse down event.
 * @param {Object} event
 */
Draggable.prototype.onMouseDown = function(event) {
    // New drag event.
    if (this.dragData.occuring === false) {
        this.dragData.occuring = true;

        this.dragData.startX = this.dragData.lastX = event.clientX;
        this.dragData.startY = this.dragData.lastY = event.clientY;
        this.dragData.offsetX = parseInt(this.applyTo.style.left, 10) - event.clientX;
        this.dragData.offsetY = parseInt(this.applyTo.style.top, 10) - event.clientY;
        this.dragData.lastTime = (new Date()).getTime();

        // Mouse up and move events.
        var me = this;
        window.addEventListener('mousemove', this.mouseMove, false);
        window.addEventListener('mouseup', this.mouseUp, false);
    }
};

/**
 * The mouse movement event.
 * @param {Object} event
 */
Draggable.prototype.onMouseMove = function(event) {
    if (this.dragData.occuring === true) {
        // He is dragging me now, we move if there is need for that.
        var moved = (this.dragData.lastX !== event.clientX || this.dragData.lastY !== event.clientY);

        if (moved === true) {
            var element = this.applyTo;

            // The skew animation. :)
            var skew = (this.dragData.lastX - event.clientX) * 1;
            var limit = 25;
            if (Math.abs(skew) > limit) {
                skew = limit * (skew > 0 ? 1 : -1);
            }

            var transform = 'translateX(' + (event.clientX + this.dragData.offsetX - parseInt(element.style.left, 10)) + 'px)';
            transform += 'translateY(' + (event.clientY + this.dragData.offsetY - parseInt(element.style.top, 10)) + 'px)';
            transform += 'skew(' + skew + 'deg)';
            element.style.MozTransform = transform;
            element.style.webkitTransform = transform;

            this.dragData.lastX = event.clientX;
            this.dragData.lastY = event.clientY;

            this.dragData.lastTime = (new Date()).getTime();
        }
    }
};

/**
 * The mouse up event.
 * @param {Object} event
 */
Draggable.prototype.onMouseUp = function(event) {
    this.dragData.occuring = false;
    var element = this.applyTo;

    // Reset transformations.
    element.style.MozTransform = '';
    element.style.webkitTransform = '';

    // Save the new position.
    element.style.left = (this.dragData.lastX + this.dragData.offsetX) + 'px';
    element.style.top = (this.dragData.lastY + this.dragData.offsetY) + 'px';

    // Remove useless events.
    window.removeEventListener('mousemove', this.mouseMove, false);
    window.removeEventListener('mousemove', this.mouseUp, false);
};

Currently my dragging system is buggy and simple. I need more information on the logic that I should be applying.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:23:45+00:00Added an answer on May 15, 2026 at 5:23 pm

    Wow, the idea rocks. 🙂 I’ve cleaned your code a bit, and solved the problems with initialization. Now it works fine for me on Firefox and Chrome (even though you said it shouldn’t).

    A few notes:

    • you need to grab the starting top and left positions during initialization (getBoundingClientRect)
    • store references like this.dragData and element.style for shortness and faster execution
    • dragData can be initialized as an empty object. It’s fine in javascript. You can add properties later.
    • options should be conditionally initialized as an empty object, so that you can take zero options
    • moved and dragData.occuring were totally useless because of the event management
    • preventDefault is needed in order not to select text during dragging
    • you may want to keep track of z-indexes to be the active element always visible

    Have fun!

    Code [See it in action]

    /**
     * The draggable object.
     */
    Draggable = function(targetElement, options) {
        this.targetElement = targetElement;
    
        // we can take zero options
        options = options || {};
    
        // Initialize drag data.
    
        // @props: startX, startY, lastX, lastY,
        // offsetX, offsetY, lastTime, occuring
        this.dragData = {};
    
        // Set the cursor style.
        targetElement.style.cursor = 'move';
    
        // The element to move.
        var el = this.applyTo = options.applyTo || targetElement;
    
        // Event methods for "mouse down", "up" and "move".
        // Mouse up and move are binded to window.
        // We can attach and deattach "move" and "up" events as needed.
        var me = this;
    
        targetElement.addEventListener('mousedown', function(event) {
            me.onMouseDown.call(me, event);
        }, false);
    
        this.mouseUp = function(event) {
            me.onMouseUp.call(me, event);
        };
    
        this.mouseMove = function(event) {
            me.onMouseMove.call(me, event);
        };
    
        // initialize position, so it will
        // be smooth even on the first drag
        var position  = el.getBoundingClientRect();
        el.style.left = position.left + "px";
        el.style.top  = position.top  + "px";
        el.style.position = "absolute";
        if (el.style.zIndex > Draggable.zindex)
          Draggable.zindex = el.style.zIndex + 1;
    };
    
    Draggable.zindex = 0;
    
    /**
     * Sets the skew and saves the position
     * @param {Number} skew
     */
    Draggable.prototype.setSkew = function(skew) {
    
        var data  = this.dragData;
        var style = this.applyTo.style;
    
        // Set skew transformations.
        data.skew = skew;
        style.MozTransform    = skew ? 'skew(' + skew + 'deg)' : '';
        style.webkitTransform = skew ? 'skew(' + skew + 'deg)' : '';
    
        // Save the new position.
        style.left = (data.lastX + data.offsetX) + 'px';
        style.top  = (data.lastY + data.offsetY) + 'px';
    }
    
    /**
     * The mouse down event.
     * @param {Object} event
     */
    Draggable.prototype.onMouseDown = function(event) {
    
        var data = this.dragData;
    
        // New drag event.
        var style = this.applyTo.style;
    
        data.startX   = data.lastX = event.clientX;
        data.startY   = data.lastY = event.clientY;
        data.offsetX  = parseInt(style.left, 10) - event.clientX;
        data.offsetY  = parseInt(style.top,  10) - event.clientY;
        style.zIndex  = Draggable.zindex++;
        data.lastTime = (new Date()).getTime();
    
        // Mouse up and move events.
        window.addEventListener('mousemove', this.mouseMove, false);
        window.addEventListener('mouseup', this.mouseUp, false);
        event.preventDefault(); // prevent text selection
    };
    
    /**
     * The mouse movement event.
     * @param {Object} event
     */
    Draggable.prototype.onMouseMove = function(event) {
    
        // He is dragging me now
        var me      = this;
        var data    = me.dragData;
        var element = me.applyTo;
        var clientX = event.clientX;
        var clientY = event.clientY;
    
        data.moving = true;
    
        // The skew animation. :)
        var skew  = (data.lastX - clientX) * 1;
        var limit = 25;
    
        if (Math.abs(skew) > limit) {
            skew = limit * (skew > 0 ? 1 : -1);
        }
    
        var style = element.style;
        var left  = parseInt(style.left, 10);
        var top   = parseInt(style.top,  10);
    
        var transform =
              'translateX(' + (clientX + data.offsetX - left) + 'px)' +
              'translateY(' + (clientY + data.offsetY - top)  + 'px)' +
              'skew(' + skew + 'deg)';
    
        style.MozTransform    = transform;
        style.webkitTransform = transform;
    
        data.lastX = clientX;
        data.lastY = clientY;
    
        data.lastTime = (new Date()).getTime();
    
        // here is the cooldown part in order
        // not to stay in disorted state
        var pre = skew > 0 ? 1 : -1;
        clearInterval(data.timer);
        data.timer = setInterval(function() {
          var skew = data.skew - (pre * 10);
          skew = pre * skew < 0 ? 0 : skew;
          me.setSkew(skew);
          if (data.moving || skew === 0)
            clearInterval(data.timer);
       }, 20);  
       data.moving = false;
    };
    
    /**
     * The mouse up event.
     * @param {Object} event
     */
    Draggable.prototype.onMouseUp = function(event) {
    
        this.setSkew('');
    
        // Remove useless events.
        window.removeEventListener('mousemove', this.mouseMove, false);
        window.removeEventListener('mousemove', this.mouseUp, false);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 526k
  • Answers 526k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer exclude-result-prefixes="xmlns"> What's wrong about my code? This isn't quite meaningful,… May 16, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer I think "debuggers" have stayed the same for a quite… May 16, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer I think you're really close to getting this to work.… May 16, 2026 at 10:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to make a drop down menu using javascript/jquery but can't get my
I'm trying to make an mp3 player that passes javascript events to a flash
Hey all, my first post :D Problem: I'm trying to make a template gallery,
I'm trying to pass the DELETE to a URL in asp.net MVC using JavaScript
i'm trying to make my image search with google ajax search i'm using jQuery.
I'm trying to make a simple image browser for TinyMCE which I am using
I'm trying to compile a file with the V8 the JavaScript Engine by Google.
I'm trying to make a radio group specifying a bunch of options, and an
Just asked how to check if an internet connection exists using javascript and got
I've been learning Unity 3D, slowly. I'm trying to make a maze, and need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.