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 6823651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:48:27+00:00 2026-05-26T21:48:27+00:00

I’m trying to create my own Drag and Drop Plugin. My Plugin: $.fn.drag =

  • 0

I’m trying to create my own Drag and Drop Plugin.

My Plugin:

$.fn.drag = function(options) {
    var defaults = {
        revert: false,
        onDrag: function() {},
        onDrop: function() {}
    };
    var o = $.extend(defaults, options);
    return this.each(function() {
        var position = $(this).position();
        var ptop = position.top;
        var pleft = position.left;
        var down = false;
        $(this).mousedown(function(event) {
            down = true;
            $(this).css({
                cursor: 'move',
            }).mousemove(function(event) {
                if (down == true) {
                    $(this).css({
                        cursor: 'move',
                    });
                    var w = $(this).width();
                    var h = $(this).height();
                    var left3 = (w / 2) + 7;
                    var top3 = (h / 2) + 7;
                    $(this).css({
                        cursor: 'move',
                        left: (event.clientX) + (3 * 3) - left3,
                        top: (event.clientY) + (3 * 3) - top3
                    });
                }
            }).bind('mousemove', o.onDrag).mouseup(function() {
                down = false;
                $(this).css({
                    cursor: 'default',
                });
                if (o.revert == true) {
                    $(this).animate({
                        top: ptop,
                        left: pleft
                    }, 300);
                } else {}
            }).bind('mouseup', o.onDrop);
        });
    });
}

My Problem: At the moment when I try to drag the matched element it centers the cursor inside. I did that on purpose because I didnt know how to make it so if I click on any part of the element it will drag. As you can see here

Centering Cursor inside element code:

var w = $(this).width();
var h = $(this).height();
var left3 = (w / 2) + 7;
var top3 = (h / 2) + 7;
$(this).css({
    cursor: 'move',
    left: (event.clientX) + (3 * 3) - left3,
    top: (event.clientY) + (3 * 3) - top3
});

Is there a way not to center the cursor inside the element and be able to drag the element wherever you click it?

Thanks in advance 😀

  • 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-26T21:48:28+00:00Added an answer on May 26, 2026 at 9:48 pm

    You can keep track of the difference in cursor position when mousemoveing with the starting cursor position when you mousedowned, and apply this data appropriately: http://jsfiddle.net/BggPn/15/.

    $(this).mousedown(function(event) {
        down = true;
        var dx = event.pageX - $(this).position().left, // difference from left border
            dy = event.pageY - $(this).position().top;  // difference from top border
    

    and:

    $(this).css({
        cursor: 'move',         // you had set the cursor already by the way
        left: event.pageX - dx, // set left border to cursor x position minus difference
        top:  event.pageY - dy  // set top border to cursor y position minus difference
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.