Move the DIV element to a static section within the DOM

In my Vue app, I have implemented methods to dynamically move a DIV called 'toolbox' to different sections of the DOM. Currently, the DIV is positioned on the bottom right of the screen and remains static even when scrolling.

My goal is to use the methods I created to move the DIV to the "Top Left", "Top Right", and "Bottom Left" corners of the screen. However, I am facing challenges in determining the correct pixel values to provide to these functions. Is there a more efficient way to achieve this?

Here's a snippet of my code:

<button @click="changePositionTopLeft">Move Top Left</button>
<button @click="changePositionTopRight">Move Top Right</button>
<button @click="changePositionBottomLeft">Move Bottom Left</button>

methods: {
  changePositionTopLeft() {
    const divPosition = document.getElementById('sdk-ts-root')
    divPosition.style.left = '200px'
    divPosition.style.top = '200px'
  },
  changePositionTopRight() {
    const divPosition = document.getElementById('sdk-ts-root')
    divPosition.style.right = '400px'
    divPosition.style.top = '400px'
  },
  changePositionBottomLeft() {
    const divPosition = document.getElementById('sdk-ts-root')
    divPosition.style.left = '400px'
    divPosition.style.bottom = '400px'
  }
}

}

Answer №1

When utilizing Vue (or any similar library such as React, Svelte, etc.), it is recommended not to directly manipulate the DOM. Instead, create data bindings that will automatically update your HTML content.

Here is an example of how to follow the "Vue way":

let app = new Vue({
  el: '#app',
  data: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,

  },
  computed: {
    style() {
      return `top: ${this.top}px; right: ${this.right}px; bottom: ${this.bottom}px; left: ${this.left}px;`
    }
  },
  methods: {
    changePositionTopLeft() {
      this.top = 200
      this.left = 200
    },
    changePositionTopRight() {
      this.top = 400
      this.right = 400
    },
    changePositionBottomLeft() {
      this.bottom = 400,
        this.left = 400
    },
    reset() {
      this.top = 0;
      this.right = 0;
      this.bottom = 0;
      this.left = 0;
    }
  }
})
#app {
  position: relative;
  height: 500px;
}

.el {
  position: absolute;

  /* some styling */
  height: 50px;
  width: 50px;
  background-color: red;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
    <div :style="style" class="el">DIV</div>
    <button @click="changePositionTopLeft">Move Top Left</button>
    <button @click="changePositionTopRight">Move Top Right</button>
    <button @click="changePositionBottomLeft">Move Bottom Left</button>
    <button @click="reset">Reset</button>
    top: {{top}} right: {{right}} top: {{bottom}} left: {{left}}
</div>

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

Executing mailto URLs from action method

As a newcomer to MVC, I am looking to create an action method in MVC that triggers Mailto:?body=body goes here.&subject=test subject, allowing the default mail client to automatically populate the user's email. Currently, I have a List<String&g ...

Exploring Ngrx: Leveraging a single selector to choose multiple property modifications

I need some help with my reactive Form Angular application that uses the NGRX store. Instead of subscribing to the entire state, I want to subscribe to changes in specific fields like "name" and "city." I have been attempting to use the selector selectFor ...

Having trouble retrieving Array data in Vue 3 with Reactive Properties?

I'm facing an issue with using the array of my reactive component because it is wrapped in a proxy. I attempted to retrieve the value from the Proxy using Json.stringify but it only returns an empty array. How can I access and use my array effectively ...

SliderToggle and Div implementation in Internet Explorer (IE) using jQuery

I'm encountering an issue with jQuery's slideToggle function and a div specifically in IE. Here is the code snippet causing the problem: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.d ...

Implementing a Dynamic Navigation Feature using PHP in a Standalone File

I am currently working on creating a dynamic PHP navigation system that should highlight the active tab, but I'm facing challenges with making the active tab part function properly. While there are simpler methods available, they all involve incorpora ...

I am encountering unexpected issues with the functionality of img srcset and sizes

Attempting to enhance the responsiveness of images on my website has led me to discover the srcset and sizes attributes. The objective is clear: If the screen size exceeds 600px or falls below 300px, a 250x250 image should be displayed For sizes in betw ...

There was an error due to a TypeError: The 'page' property cannot be read because it is undefined

Can anyone assist me with an Angular issue I'm facing? I've been working on integrating server-side pagination, but no matter how many times I revise my code, I keep encountering the same error message: ERROR TypeError: Cannot read properties o ...

Updating a single .jshintrc option for a folder

My project has a .jshintrc file at the root, containing the following settings: { "node": true, "smarttabs": true, "undef": true, "unused": true } While these settings work well for node-related code in my project, they are not suitable for brows ...

Tips for changing the page URL upon click in a Vue.js application

I am currently developing a post board application using Vue.js and I am facing an issue with redirecting the page when a user clicks on each post. To handle this, I have made use of vue-route and axios in my project. Here is a snippet from index.js, ex ...

How can Material UI React handle long strings in menu text wrapping for both mobile and desktop devices?

Is there a way to ensure that long strings in an MUI Select component do not exceed the width and get cut off on mobile devices? How can I add a text-wrap or similar feature? Desktop: Mobile: <FormControl sx={{ m: 1, minWidth: '100%', marg ...

The two <p> elements are being pushed to the right column, which is not their intended display

A snippet of less.css code is available at this link: #content { overflow: hidden; width: 100%; p { float: left; width: 465px; padding: 0px 25px 0px 35px; margin: 0px; br { line-height: 2. ...

Getting rid of quotes in a JSON result

My unique code snippet Retrieve data = Array[2] : 0:object id : "1" lat : "76.23" long:"21.92" 1:object id:"2" lat:"10.23" long:"12.92" var newCoords=[]; for(_i = 0; _i < ...

To determine the class of an element and reveal its parent if the class is present

Typically, the parent element remains hidden by default. However, upon clicking a specific element, its child might gain a class called "selected". How can I check for this class and then display the entire list if it is present? <ul style="display: ...

Encountering difficulty when attempting to load a Vue component within a Blade file in Laravel 8

I successfully loaded Vue components in previous versions of Laravel by following these steps: In app.js located within resources/app.js, I declared the component like so: Vue.component('upload-menu', require('./components/UploadMenu.vue&ap ...

What is the best way to arrange <div> elements with text inside without moving the text?

I need help figuring out how to stack one or more <div> elements on top of another <div> element while maintaining the text content within each. My issue is illustrated in this fiddle: https://jsfiddle.net/rd268ucy/1/. When attempting to drag o ...

How can I find a user's ID in a JSON file using for...in loop in Discord.js?

I'm facing an issue with this loop where I am only able to retrieve the guild id instead of the user id that I actually need. Here is my code: for(let userID in money){ res.push({"guildid": message.guild.id, "id": userID, " ...

Is there a way for me to access a user control property directly from the client side?

I'm in the process of developing several user controls that will be derived from my base class, BaseControl, which is a subclass of UserControl. The BaseControl class contains important features that I will need to utilize, including a string property ...

Ways to extract a number that comes after a specific word in a URL

I have a URL and need to extract a specific value from it by removing a certain step. I am looking for a regex solution to accomplish this task For example, if I have the window.location.href, the URL might look like this: https://mypage/route/step-1/mor ...

Eliminate the header top margin in Bootstrap

HTML <div class="container-fluid"> <div class="row"> <div class="page-header header_site"> <h1><font>ABC Company</font></h1> </div> </div> </div> CSS Code: .header_site { b ...

Creating a platform that allows users to build custom themes on a website using ASP.NET/C# programming language

I'm looking for a sophisticated approach to designing an ASP.NET website where users can easily create personalized themes for their sites using a user-friendly interface. Can ASP.NET themes assist with this? Should the interface enable users to gener ...