Adapting Vue.js directive based on viewport size - a guide

Having an element with the v-rellax directive, I'm utilizing it for prallax scrolling in this particular div:

<div id="image" v-rellax="{ speed: -5 }"></div>

Currently, there's a need to adjust the speed property to -3 to suit a different screen width. Is it possible to change the speed using:

1. a `media query`
2. `vue.js`
3. `javascript`

Is there a straightforward method to accomplish this with vue?

Edit:

Despite trying to implement the suggested solution, the parallax scroll ceases to work correctly. Am I doing something incorrectly?

<script>
export default {
  name: "home",
  data: { rellax_speed: -5 },
  created() {
    window.addEventListener('resize', (event) => {
      if (event.target.innerWidth >= 576) {
        this.rellax_speed = -3;
        return;
      }
      this.rellax_speed = -5;
    })
  }
}
</script>

template:

<div id="image" v-rellax="{ speed: rellax_speed }"></div>

Edit2:

<script>
export default {
  name: "home",
  data() {
    return {
      r_speed: -5
    }
  },
  methods: {
    onresize(event) {
      if (event.target.innerWidth >= 576) {
        this.r_speed = -3;
        return;
      }
      this.r_speed = -5
    }
  },
  created() {
    window.addEventListener("resize", this.onresize)
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.onresize, true)
  }
}
</script>

template:

<div id="image" v-rellax="{ speed: r_speed }"></div>

Answer №1

Alright, I think I've got a handle on what you're saying.

UPDATE:

Big thanks to tony19 :)

Add a new variable called vrellaxSpeed to your data object with an initial value of -5, and make sure to use it in your directive.

data() {
   return { 
      vrellaxSpeed: -5
   }
},
methods:{
   onResize(event) {
      if (event.target.innerWidth > 1280) {
        this.vrellaxSpeed = -3;
        return;
      }
      this.vrellaxSpeed = -5;
   }
},
created() {
   window.addEventListener('resize', this.onResize)
},
beforeDestroy() {
   window.removeEventListener('resize', this.onResize, true);
}

Implement a computed property in your Vue component.

computed: {
   getVrellaxSpeed() {
      return window.innerWidth > 1280 ? -3 : -5;
   }
}

Then, utilize it within the prop of your directive.

<div id="image" v-rellax="{ speed: getVrellaxSpeed }"></div>

I haven't tested this out myself, so please give it a try and provide feedback for further assistance :)

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

Div's external dimension not being computed

I am attempting to calculate the overall height of the div content in order to expand the sticky-container and create the appearance that the image is tracking the user. Unfortunately, using outerHeight does not seem to be effective for some reason. While ...

How to insert a row above the header in an Excel sheet using JavaScript within

i am using excel js to export json data to excel. The json data is successfully exported to the sheet, but now I need to add a row that provides details of the sheet above the header text. for more details please refer image the code is shown below: i ...

Which is causing the block: the event loop or the CPU?

example: exports.products = (req, res) => { let el = 1; for (let i = 0; i < 100000000000000; i++) { el += i; } console.log(el); ... ... ... res.redirect('/'); }; If I include a loop like this in my code, which resour ...

Choosing2 - incorporate a style to a distinct choice

Let's talk about a select element I have: <select id="mySelect"> <option>Volvo</option> <option value="Cat" class="red">Cat</option> <option value="Dog" class="r ...

The true essence of Angular values only comes to light once the view has been updated

Here is the HTML code I am working with : <div class="container-fluid"> <div class="jumbotron" id="welcomehead"> <br><br><br><br><br><br><br><br><br><br> ...

Issue with React hook state persistence in recursive function

I implemented a recursion custom hook that utilizes a setTimeout function to provide 3 chances for an operation. Once the chances run out, the recursion should stop. However, I encountered an issue where the setTimeout function is not properly decrementin ...

"Make sure to specify Safari input field as an email and mark

I am experiencing an issue with a contact form in my HTML/PHP code. Everything seems to be working fine, but when using the SAFARI browser, the form fails to validate if I try to submit without filling out all input fields. For example, my form includes: ...

Harmonizing Vue and JQuery compatibility

Is there full compatibility between Vue.js and JQuery? How about Vue.js and JQueryUI? I have experience using both frameworks without any issues in integration. Can anyone point out any potential conflicts that may arise? ...

Developed a verification process using Discord.JS, however, I'm not receiving any feedback when trying to configure the command

As I embark on creating my debut Discord bot to enhance my coding skills, I have encountered numerous hurdles along the way. While most of these challenges were overcome by me independently, this particular issue has left me stumped. The objective of the ...

Build an interactive form with Vue.js and PHP for seamless submission

I am having trouble submitting a form that is created inside a v-for loop. I have tried using Ajax and Axios, but they only seem to work when the form is outside of the v-for loop. Can someone provide a solution for this issue? Below is the code snippet: ...

The x-axis title in Apexcharts is consistently misaligned

Dealing with some issues regarding the placement of the x-axis title in my column chart. The position of the title seems to vary based on the y-values range, as illustrated in these examples: Example 1 Example 2 Below is the code I am using: Code block ...

Challenges arise when using two side-by divs with a 100% height

Caution: This page may include NSFW images. I am attempting to set the left div to a width of 300px and have the right one fill the remaining space on the page. After finally getting them to sit side by side, I encountered some strange height issues due ...

Use a for loop to iterate through elements and retrieve input values using getElementById()

As I work through a for loop in JavaScript, I am utilizing the getElementById() method to fetch multiple input values. To begin, I dynamically created various input boxes and assigned distinct id's using a for loop. Subsequently, I am employing anoth ...

How to adjust the timezone settings in PHPMyAdmin on a shared server platform

I'm having trouble changing my timezone to India on my shared server database. I've tried everything but can't seem to get it to work. My website is built using PHP Codeigniter The contact us page on my site saves all inquiry details to my ...

Is there a way to pass a v-modal as an array when setting Axios params?

I am currently dealing with a Vue Multiselect setup where the v-model is an array to accommodate multiple selected options. The challenge I am facing involves calling a route within the loadExtraOptions method and passing the array of IDs as a parameter ...

Unexpected token N error was thrown while using the Jquery .ajax() function

While working on a form submission feature using JQuery .ajax() to save data into a MySQL database, I encountered an error message that says "SyntaxError: Unexpected token N". Can someone please explain what this means and provide guidance on how to fix it ...

Handling multiple patch requests using React and Redux when onBlur event occurs

Currently, I am using Redux-form for editing guest information. Whenever a field is left, the content of that field gets patched to the guest through a simple patch request and the store is updated accordingly. However, an issue arises when I use Google fo ...

Tips for aligning two elements in a row using Bootstrap 4 Flex

I am facing a challenge with positioning an H2 element and a Dropdown button on the same line using bootstrap 4 flex. Here is what I have: https://i.sstatic.net/kV45k.png This is what I am trying to achieve: https://i.sstatic.net/Owt5j.png Below is my ...

The AJAX POST function is not functioning properly when clicking on contextmenus

Can someone please assist me? I am having trouble sending data via ajax post to my nodejs server from contextmenus. It is not functioning as expected. Although the ajax request does not give any error alert, the data is not being sent successfully. I hav ...

I need assistance with this ajax/javascript/php

I am currently working on creating a dynamic chained list. The idea is to have the user make selections from the first four dropdown lists, and based on their choices, a fifth dropdown list should appear. However, I am facing some issues with my code as th ...