The occurrence of the "contextmenu" event can cause disruption to the execution of a function triggered by the "onmousemove" event

Currently in the process of developing a Vue application utilizing a Pinia store system.

Within my BoxView.vue component, I have created a grid layout with draggable elements that have flip functionality implemented within the BoxItem.vue component.

Specifically in the BoxView.vue, there is an EventListener for "mousedown" attached to a "flip-box" element triggering the moveItemInit function:

<template>
  <div class="boxes-grid">
    <div v-for="(box, index) in getAllBoxes" :key="index" class="box-space">
      <BoxItem :id="box.id" @mousedown = 'moveItemInit($event.currentTarget as HTMLElement, $event)'> // <--------------------
        <template v-slot:front>
          <h2>{{ box.frontText }}</h2>
        </template>
        <template v-slot:back>
          <h2>{{ box.backText }}</h2>
        </template>
      </BoxItem>
    </div>
  </div>
</template>

Inside the BoxItem.vue component, there is a function called "rotateInner" that allows flipping an element on right-click:

<template>
    <div class="flip-box" @contextmenu.prevent="rotateInner" > // <--------------------
      <div class="flip-box-inner" :class="{ 'flipped': isFlipped }">
        <div class="flip-box-front">
          <slot name="front"></slot>
        </div>
        <div class="flip-box-back">
          <slot name="back"></slot>
        </div>
      </div>
    </div>
</template>

<script setup>

  ...

  async function rotateInner(event: MouseEvent) {
    if (event.button === 2) {
      event.preventDefault()
      event.stopPropagation()
      isFlipped.value = !isFlipped.value
    }
  }
</script>

The issue arises when the moveItemInit function eventually attaches an EventListener to the entire document like so:

    document.onmousemove = (eventNew) => moveItem(flipBoxElement, eventNew);

The "moveItem" function enables dragging an element by holding down the left mouse button across the page without restrictions. My goal is to be able to flip the element with a right click while it's being dragged. However, whenever I attempt this, the element stops moving and reverts back to its initial position, indicating premature activation of

document.onmouseup = (eventNew) => moveItemEnd(flipBoxElement, eventNew);
.

How can I prevent right-click from interrupting the onmousemove event, ensuring both dragging and flipping functionalities work smoothly together? Any assistance would be highly appreciated!


https://i.sstatic.net/yRK4r.gif

I've already tried using event.preventDefault() and event.stopPropagation() without success.

I anticipated that a right mouse click would not interfere with maintaining the left mouse button pressed down and activating the "moveItem" function.

Answer №1

Discovered the issue and implemented a solution, it turned out to be a simple mistake on my end.

Every mouse click triggered the "moveItemEnd" function:

document.onmouseup = (eventNew) => moveItemEnd(flipBoxElement, eventNew);

To resolve this, I included a mouse button check within the function:

async function moveItemEnd(element: HTMLElement, event: MouseEvent){
  if (event.button != 0){
    return;
  }
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on extracting the values of checked checkboxes by their ID using javascript

I'm currently attempting to extract the values of selected checkboxes. These checkboxes have distinct IDs because they are specified within a modal window. <input type = 'checkbox' id = 'audience_Name-$row[asset_ID]' value = &a ...

Angular JS Tab Application: A Unique Way to Organize

I am in the process of developing an AngularJS application that includes tabs and dynamic content corresponding to each tab. My goal is to retrieve the content from a JSON file structured as follows: [ { "title": "Hello", "text": "Hi, my name is ...

The chosen font color in the v-select item is fixed and cannot be modified

I attempted to change the font color in a select item using .v-list-items__title, but unfortunately, it did not work as expected. <v-select :items="dataOption" label="item" solo class="header ...

Resetting md-radio-button choices within an Angular 2 application

My Angular app has a sorting filter using radio buttons via md-radio-group for users to choose how they want data displayed. The radio buttons work fine, but I'm struggling to clear them when the "Restore Defaults" button is clicked. This is the code ...

How can we translate this php json_encode function into Node.js?

Seeking the equivalent Node.js code for this PHP Script: $SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS"; $SMAres ...

adjusting the height of a div using jQuery UI's layout feature

Recently, I have been playing around with the adjustable grids plugin (jquery ui layout) to set the width of each div using the plugins. However, I've encountered a challenge when it comes to adjusting the height of inner divs within the layout. Speci ...

Tips for displaying the updated value retrieved from an array

I'm struggling with displaying the values of an object. The table is supposed to show tasks and their remaining time to finish, but the countdown timer isn't updating. The values are stored in an Object by Task ID, like this: Object {1: "4017 D ...

Exploring Vue JS and Quasar: Unraveling the Mystery of @functionname within router-view

Just starting out with vue and quasar. This piece of code is in my vue file, but I'm not quite sure what it does. <router-view v-if="some_object" :abc="abc" :xyz="abc_xyz" :title="title" @updated="getABC" @refreshABC="getABC"/> As far as I kno ...

Unable to use jQuery to choose an item from a dropdown menu and reveal a hidden text box

Seeking assistance in displaying a paragraph with text and a textbox when a specific option is selected from the dropdown menu on my form. Previously used code for radio buttons, but encountering issues with this scenario. Any guidance would be greatly app ...

Tips on storing Jquery click event in local storage to temporarily conceal CSS styling

I need some assistance with Javascript, as I am not very proficient in it. I have created a CSS code that displays an announcement when the webpage loads, along with a close button to hide the entire CSS and its contents using JQuery animate. My goal is t ...

Swap out a particular component during the testing phase

Currently, I am running tests on my React-Redux application using Jest. Within my API calls, I have integrated a fetch module called cross-fetch. However, I am looking to substitute this with fetch-mock. Take a look at my directory structure: Action.js i ...

CSS responsive design: concealing elements does not prevent the page from being displayed

Imagine a scenario where I need to display a template in two different versions based on the user's device. For instance, I have utilized the following code: <div class="desktop"> <body> Hi Desktop user </body> </div> ...

Tips for transferring information between two components when a button is clicked in Angular 2

I am currently working on a code that displays a table on the main page with two buttons, "Edit" and "Delete", for each row. When the Edit button is clicked, a modal opens up. My question is, how can I pass the "employee id" of a specific employee to the ...

Using PHP and XML with Ajax can run into problems with the getElementsByTagName function in Internet

Encountering a problem with getElementsByTagName specifically in IE versions 7 and 8. An address lookup generates suggested addresses as XML strings stored in a PHP session variable. These are then accessed using an AJAX function to retrieve the desired s ...

We are hosting an event focused on DOM text selection outside of Input or TextArea elements

I need help finding a Javascript event that triggers when a user highlights paragraph text with their mouse on a web page. Once the text is highlighted, I want to access it using window.getSelection(). Just to clarify, I am not looking for ways to capture ...

Unpredictable actions of Vue.js application during testing phase in rspec

I've got a Rails 6 app that I'm testing with rspec, Capybara, and Chrome headless on a remote VM. No more of the old poltergeist stuff thanks to the new webdrivers gem. Within this app, there's a user manager mini-app built in Vue 2.somethi ...

What could be the reason why the @media print selector is not showing the correct format when I try to print?

When I click the print button, the text is supposed to display in four columns in the print dialog, but it appears as paragraphs instead. This is my script code where there is a click event for the print button. When I click on it, the print dialog pop ...

The CSS style class is applied to two menus that share the same route but have different query parameters. These menus are styled with the class name

Utilizing both Vue and Vuetify version 3. For my navigation, I have implemented the v-navigation-drawer component alongside v-list for each menu item: <template> <v-navigation-drawer> <v-list nav> <v-list-item title=" ...

Creating a 3D object using three.js framework

When playing video games, I often notice a common pattern where hovering the mouse over an object triggers a gradient highlight around its 2D representation. To recreate this effect in my Three.js scene, I started by setting up the necessary raycaster vari ...

Using v-select to connect a Rest API with v-data-table was not displaying correctly, so I had to resort to using v-for to populate the table instead

Purpose: 1.Rest API >>> by axios 2.Select category >>> by v-select 3.Show table >>> by v-data-table however, there is an issue with displaying the table so I am using v-for to show it instead. View what I have managed to displa ...