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

Add a text to the values in a column of a table when the IDs are the same

I am working on a project that involves updating a table based on a data change. The table has a column for ID and when the ID matches a specific variable, I need to append the word 'Updated!' next to it in the table cell. However, the code I hav ...

Limiting the zoom in three.js to prevent object distortion caused by the camera

I'm currently in the process of developing a three.js application where I have successfully loaded my STL objects and incorporated 'OrbitControls'. However, I am encountering an issue when attempting to zoom using the middle scroll button on ...

Troubleshooting Tips: Investigating a Service Call in AngularJS 2 using ES6 Syntax

MY DILEMMA: I have recently started learning Angular2 and found myself wanting to debug a service call. The service appears to have been properly called, as evidenced by the view display. However, when trying to log the content of the variable holding t ...

Adjusting the height of a GAS iframe in WordPress with iframe-resizer: A step-by-step guide

I would like to embed an iframe of my Google Application Script Web into my WordPress site without the scroll bar. Refer to the image below for context. https://i.stack.imgur.com/7L6Tw.png I encountered an error message while attempting to use the iframe ...

How to maintain the selected value in a Vue.js dropdown even after the page is refreshed

When a user selects a value from a dropdown, it is pushed to the URL. If the page is refreshed, the dropdown should default to the selected value in the URL. This functionality is being implemented in Laravel. I have attempted the following approach, but w ...

Using PHPs nth-child in media queries

There are multiple images on MyPage, typically displayed in rows of 6. However, the number of images per row adjusts based on browser size using media queries. The issue arises with the margin set for the last image in each row, as the nth-child property r ...

Deactivating Touchable Opacity Sounds in React Native

Currently, I am in the process of developing an application, utilizing TouchableOpacity instead of a button. I am looking to disable the auditory feedback that occurs when the TouchableOpacity component is pressed. <TouchableOpacity activeOpacity={1} to ...

Is the p tag not becoming absolute from the bottom even when set to position: absolute?

My p tag in the section properties of div class="sf sf_2 won't align to 0 px from the bottom of the div. It seems to move 0px from the top, right, and left but not bottom. I've tried changing its position property and where it's nested, but ...

When using the "Content-Disposition" header with the value "inline;filename=" + fileName, it does not necessarily guarantee that PDF files will be displayed directly

When a link is clicked, I want the PDF file to first show in a new tab as a preview before allowing users to download it. I researched and found advice suggesting that including these two headers would achieve this: Response.AddHeader("Content-Dispositio ...

How can I change the background color of my notification box to red if the count is not equal to zero?

When the count equals 0, I don't want any effect on the notification box. However, when the count is not equal to zero, I want the notification box to turn red. I tried implementing this, but it's not working as expected. By not working, I mean n ...

Instructions on creating a Superfish menu with a vertical layout in the first level and a horizontal layout in the second level

Currently, I am using the Superfish menu in Drupal7 and have designed my first item level to be vertical. However, I now want to style my second item level horizontally. I have tried various CSS approaches and added some class names via jQuery like $(&apo ...

Steps for adding a div to a Vue template

When working with my template, I utilize a for loop to iterate through data entries. <tr v-for="entry in filteredData"> <td> @{{ entry.position}} </td> <td :class="addNewElement(entry.value)"> </td> <td> ...

The instantiation of the cloudinary module failed because of an error with the injector

I am currently developing a MEAN application based on the template provided by Linnovate. You can find the template at https://github.com/linnovate/mean My goal is to integrate a module called Cloudinary into my application. To achieve this, I followed th ...

Tips for achieving a design aesthetic similar to that of the JQuery UI website

On my website, I am using jQuery UI along with the "Smooth Default" CSS theme. However, I have noticed that everything appears larger compared to the jQuery UI website itself. For instance, when looking at the buttons here: http://jqueryui.com/button/ The ...

Incorporating a for loop, ExpressJS and Mongoose repeatedly utilize the create method to generate

When users input tags separated by commas on my website, ExpressJS is supposed to search for those tags and create objects if they don't already exist. Currently, I am using a for loop to iterate through an array of tags, but only one object is being ...

Exploring the elimination of box shadow on elements that overlap

view image hereI'm experiencing an issue with my HTML code where a box shadow appears in the mobile view. Despite my efforts, I cannot seem to remove it. This problem only arises in the mobile view, leading me to believe that it may be related to how ...

The @click function does not function properly when employing runtimeConfig in Nuxt 3

Here is a demonstration I created: https://stackblitz.com/edit/github-jsuxnz-rst5gg-git?file=app.vue Initially, the @click function works fine until I introduce runtimeConfig with const config = useRuntimeConfig();, after which all events cease to work. ...

Setting up authorization levels for roles in Discord.js

Hi everyone, I came across this script that deals with users posting invite links. How can I whitelist specific channels to prevent the bot from banning or kicking users for posting invite links? Any help would be greatly appreciated. Thank you. adminCli ...

`Is there a way to manipulate the timer in JavaScript?`

My current project involves creating a timer in minutes and seconds. The user inputs the desired time in minutes into a textbox named "textbox2". However, when I attempt to retrieve the value of this input using `getElementById.value`, it returns a NULL er ...

Ensuring consistency in aligning float elements

I've been struggling to create a concept design for my internship project. I aim to have a page with six clickable elements (images). When one is clicked, the others should disappear and the active one moves to the top. I managed to achieve this using ...