Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i change style of the body when my modal it will be open?

How can i change the body{overflow:hidden} when my modal it will be open?

for example it will be my modal, when its open, i would like to apply this style body{overflow:hidden}

<div  v-if="dialogFoundation">

i am using vuejs3, i am using setup(){…}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The best performance would be to use javascript plain. You can add Eventlistener top the modal trigger Element. In my example i use a button. If it triggered then you can use classList and assign the body a class. In my example .dark.

Vue version

<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template>
  <div id="app">
    <h1>{{message}}</h1>
    <p></p>

    <button @click="doSomething">Modal</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Welcome to Vue!'
    };
  },
  methods: {
    doSomething() {
      const b = document.querySelector('body');
  b.classList.toggle('dark');
    }
  }
};
</script>

<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

a,
button {
  color: #4fc08d;
}

button {
  background: none;
  border: solid 1px;
  border-radius: 2em;
  font: inherit;
  padding: 0.75em 2em;
}
.dark {
  background: black;
  opacity: 0.4;
}
</style>

Vanilla JS

const btn = document.querySelector('button');

btn.addEventListener('click', () => {
  const b = document.querySelector('body');
  b.classList.toggle('dark');
})
.dark {
  background: black;
  opacity: 0.4;
}
<body>
  <div></div>
  <button>click</button>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading