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

  • SEARCH
  • Home
  • 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 7545187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:51:50+00:00 2026-05-30T08:51:50+00:00

I have a function that allows me to drag a something around. My js:

  • 0

I have a function that allows me to drag a something around. My js:

  $(function() {
        $( svgcanv ).css('cursor', '-moz-grab');

        var leftButtonDown = false;
        $(document).mousedown(function(e){
            console.log('pressed');
            // Left mouse button was pressed, set flag
            if(e.which === 1) leftButtonDown = 1;
        });

        $(document).mouseup(function(e){
            console.log('released');
            // Left mouse button was released, clear flag
            leftButtonDown = 0;
            $( svgcanv ).css('cursor', '-moz-grab');
            firstDragCanvas = 1;
        });

        $( mainGroup ).mousemove(function(e){
            if (leftButtonDown == 1)
            {
                $( svgcanv ).css('cursor', '-moz-grabbing');
                var posX = e.pageX;
                var posY = e.pageY;
                if (firstDragCanvas == 0)
                {
                    posX = e.pageX - canvasPosX;
                    posY = e.pageY - canvasPosY;
                }
                else
                {
                    canvasPosX = e.pageX - translateX;
                    canvasPosY = e.pageY - translateY;
                    posX = translateX;
                    posY = translateY;
                    firstDragCanvas = 0;
                }

                if (posX > 0)
                    posX = 0;

                if (posX < -canvasWidth + $("#holder").width())
                    posX = -canvasWidth + $("#holder").width();

                translateX = posX;
                translateY = posY;

                mainGroup.setAttribute("transform","translate("+posX+","+posY+") scale("+currentZoomLevel+")");                    
            }
        });               
    });

The problem is that mouseup event isn’t always triggered. The other 2 works fine. First time I move things around everything works OK. But second if I press and drag immediately the mouseup won’t be triggered. If I stop dragging, click somewhere then drag again the script works OK again.

My console (with 2 drags, then click then drag again. notice the 2 consecutive “pressed” strings – logged from the mousedown ):

enter image description here

And no… that is an SVG and need to drag inside it so can’t use jQuery draggable.

  • 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-30T08:51:52+00:00Added an answer on May 30, 2026 at 8:51 am

    Try adding e.preventDefault(); in each event method to prevent any default UA activity.

    E.g.

    $(document).mousedown(function(e){
        e.preventDefault();
        console.log('pressed');
        // Left mouse button was pressed, set flag
        if(e.which === 1) leftButtonDown = 1;
    });
    

    and so on…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that allows users to edit a row in a table
Does .NET have some type of built in function that allows filtering on Gridviews?
I have the follow code that allows me to drag a div into 3
I'm new to VBScript, and I have a function that allows me to pull
I have a PHP function that allows users to pick which categories there post
I have the following python function that allows me to run shell commands from
I have a Bourne Shell script that has several functions in it, and allows
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035

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.