What could be the reason for my div being draggable on a smaller screen, yet becoming unresponsive to dragging when viewed on a larger screen during inspection?

The positioning of the div also changes when viewed on a larger screen. When inspected on a bigger screen, the position of the div shifts completely.

Check out the jsfiddle here: https://jsfiddle.net/annahisenberg/ft10ersb/20/

To view it in full screen mode, visit this link: https://jsfiddle.net/annahisenberg/ft10ersb/20/show

Here's the code snippet:

class Drag extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      x: this.props.x,
      y: this.props.y,
      showMoreOptionsPopup: false,
      showHelpModal: false
    };

    this.reff = React.createRef();

    this.dragMouseDown = this.dragMouseDown.bind(this);
    this.elementDrag = this.elementDrag.bind(this);
    this.closeDragElement = this.closeDragElement.bind(this);
    this.showMoreOptionsPopup = this.showMoreOptionsPopup.bind(this);
  }


  componentDidMount() {
    this.pos1 = 0;
    this.pos2 = 0;
    this.pos3 = 0;
    this.pos4 = 0;
  }

  dragMouseDown(e) {
    e.preventDefault();
    this.pos3 = e.clientX;
    this.pos4 = e.clientY;
    document.onmouseup = this.closeDragElement;
    document.onmousemove = this.elementDrag;
  };

  elementDrag(e) {
    e.preventDefault();
    this.pos1 = this.pos3 - e.clientX;
    this.pos2 = this.pos4 - e.clientY;
    this.pos3 = e.clientX;
    this.pos4 = e.clientY;
    this.setState({
      y: this.reff.current.offsetTop - this.pos2 + "px",
      x: this.reff.current.offsetLeft - this.pos1 + "px"
    });
  };

  closeDragElement() {
    document.onmouseup = null;
    document.onmousemove = null;
  };

  showMoreOptionsPopup() {
    this.setState({
      showMoreOptionsPopup: !this.state.showMoreOptionsPopup
    });
  };

render() {
    return (
      <div>
         <a
            id="more_options_button"
             className={this.state.showMoreOptionsPopup ? 'open' : null}
             onClick={this.showMoreOptionsPopup}
             style={{ left: this.state.x, top: this.state.y }}
             onMouseDown={this.dragMouseDown}
             ref={this.reff}
          >
            <div>
              <p>menu</p>
            </div>
          </a>
      </div>
    );
  }
}

Answer №1

When you first set up your fiddle, the initial position was defined as

position: fixed;
left: 95rem;
top: 39rem;

This causes the item to be fixed in a position (likely off-screen on small devices)

As you drag and drop the item, these values are being updated.

Even after resizing the page, the distance from left and top remains unchanged as rem does not relate to viewport or screen size.

To address other positioning concerns: If you clarify your intended behavior, you may come closer to achieving the desired result. Consider using and updating right instead of left, like demonstrated here: https://jsfiddle.net/61z7wkus/1/

The drag and drop functionality only works with mouse events since onmousedown does not handle touch events.

For touchscreen devices, ontouchstart is equivalent to onmousedown, and ontouchend is equivalent to onmouseup. More information available at: https://developer.mozilla.org/de/docs/Web/API/TouchEvent

If you are using a device inspector with touch events, have you disabled touch as suggested by @moosa-saadat?

You may have touch enabled in your Chrome dev-tools. Disabling touch on my end allowed your code to function correctly. Refer to this question for instructions on turning off touch in your dev-tools.

Remember to verify browser compatibility! Check out tools like react-dnd designed for addressing such issues.

Answer №2

The placement of your div is currently set to left: 95rem, top: 39rem. This may lead to the element going out of view on smaller screens. Consider using a percentage or utilizing viewport units for a more responsive design.

#more_options_button {
    left: 95vw;
    top: 39vh;
}

Answer №3

  1. Adjust the positioning according to suggestions from @Joshua-Minkler
  2. Regarding the issue mentioned:

The problem still persists for me when trying to drag it on a larger screen while inspecting.

You may have touch enabled in your chrome dev tools. Disabling touch on my end resolved the issue with your code. To learn how to turn off touch in your dev-tools, check out this question

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

What is the proper method for terminating an Express app.all?

Here's a snippet of my code where I utilize the app.all method. In this scenario, I am invoking the fetchBuildings function based on the building ID or hash provided in the URL. Subsequently, I am assigning values to the title, description, and image ...

Arranging HTML elements using JavaScript or jQuery

Something seems off. The sorting doesn't work as expected. Check out the JavaScript code below: function sortDescending(a, b) { var date1 = $(a).data('date'); var date2 = $(b).data('date'); return date1 > date2; } ...

Exploring the contrasts in employing functions within a react-native environment

Imagine having a component called "wo" that receives a function as a parameter. export default class SomeComp extends Component { constructor(props) { super(props); } _someFunc = () => { return ... } render() { ...

React's useEffect enables the use of an asynchronous context in a component

Is there a way to trigger the useEffect function in my main component as soon as the context is loaded? I am attempting to create an api request with the header value received from the context component. However, upon loading the page component, it throws ...

Utilizing an external link to dynamically modify the zoom level of a Google map

I've been struggling for hours to adjust the zoom level of a Google map using an onClick JavaScript function. It seems like my variable "map" is inside the initialize function of the map, which might be causing it not to work, but I'm not complet ...

Mastering the proper utilization of document.readyState in conjunction with Selenium

I am exploring a method to automatically scroll down a page that loads content dynamically as it is scrolled, ensuring everything is loaded before interacting with it using Selenium. I came across this code originally written for c#, which I have converte ...

Sending a POST request to Mailchimp via Express using React: A step-by-step guide

I'm currently working on a project where users can sign up for a new membership through a form in React and have their information added to the Mailchimp member list via my Express server. However, I've encountered a CORS error and am unsure if I ...

How can I showcase both a username and email address in a Material UI chip?

I'm currently using Material UI chip to show name and email next to each other. However, when the name is long, the email goes beyond the chip boundary. Here's my function that generates the chips: getGuestList() { let {guests} = this.sta ...

Using TypeScript to Trigger Events in Three.js

After recently diving into Typescript, I encountered an issue when using EventEmitter from the ThreeJS library. Whenever I attempt to trigger an event: const event: THREE.EventDispatcher = new THREE.EventDispatcher(); event.addEventListener('test&apo ...

Creating a Yeoman application with a personalized Node.js server

As I embark on the journey of developing a node.js and angular application using the powerful Yeoman tool, I can't help but wonder about one thing. Upon generating my application, I noticed that there are predefined tasks for grunt, such as a server ...

Rendering with ReactDom in a SharePoint Framework application

Our current project requires us to generate a PDF file using the <div></div> elements. Most of the code I've seen renders from ReactDom.Render() instead of the render class: Take for instance React-pdf: import React from 'react&apo ...

Creating a dynamic lineup of images that adjust seamlessly to different screen sizes

Trying to figure out how to create a row of images that adjust responsively to the width of the window. Here's what I have so far: <div class="image-slider"> <div><img src="/img1.jpg"></div> <div><img src="/img2.j ...

Is it best practice to incorporate CSS into NPM component libraries or is it better to manually load it

My dilemma lies in the React component I have created and released as an NPM package. This particular package includes CSS styles that are specific to the component. I am torn between two options when it comes to including these styles. Should they be par ...

Is it possible to dynamically insert one module into another module?

Let's say I have 2 modules in two separate files: The first one is for everyone, let's call it myApp: var myApp = angular.module('myApp', ['dependency.one', 'dependency.one']); In another file named admin.js, I ha ...

unable to view icons from Summernote inline

I am currently using the Summernote editor, but I am experiencing some issues with the icons. Here is the image of the problem: https://i.sstatic.net/fRm0B.png My goal is to have all the icons aligned in a single line. Below is the code I am using: $(&a ...

Guide on utilizing the root feature in babel-plugin-module-resolver?

Currently, I'm attempting to integrate babel-plugin-module-resolver into a react-native application. When I include the plugin configuration in my babel.config.js, everything functions as expected. plugins: [ [ 'module-resolver', ...

Guide on sending the unique identifier of an element to the backend following a click event

Currently, I am working on a specific page in my project that lists movies. When you click on a movie, it directs you to a page where you can view all the information about that particular movie. I am utilizing SQL in my database and one of my ideas is to ...

Loading SVG images in advance

I am in possession of around one hundred simple SVG images, distributed among five different image folders. These images are currently retrieved on demand when they need to be displayed, which generally works well but occasionally results in flickering tha ...

The pseudo right arrow is failing to display in the vertical center

My pseudo right arrow is not displaying vertically centered within the .nav-link class. Can anyone suggest how I can achieve vertical alignment for my right arrow in an <a> tag? I have already attempted using top:50% but it did not work. Here is th ...

Can the JS DOM Element method be utilized in Postman to navigate through an HTML document?

How can I declare this in the Postman sandbox? I am looking for a solution like the following: var h1Text = window.document.querySelector("h1").innerHTML; console.log(h1Text); Additionally, I need to search for something in the following manner: I have ...