javascript - Vuejs toggle class to the body on button click in a components

Javascript - Vuejs toggle class to the body on button click in a components

In Vue.js, you can toggle a class on the body element when a button is clicked inside a component by using a combination of Vue's event handling and class binding. Here's how you can do it:

<template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { // Toggle the class on the body element document.body.classList.toggle('your-class-name'); } } }; </script> <style> /* Define the style for the class you want to toggle */ .your-class-name { /* Your CSS styles here */ } </style> 

This example assumes you have a Vue.js component with a button inside it. When the button is clicked, the toggleBodyClass method is called, which toggles the class your-class-name on the body element using document.body.classList.toggle('your-class-name').

Make sure to replace 'your-class-name' with the actual class name you want to toggle on the body element. You can define the styles for this class in the component's style section or in a global CSS file.

Examples

  1. "Vue.js toggle class on body click button"

    Description: This query seeks ways to toggle a class on the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { document.body.classList.toggle('some-class'); } } } </script> 

    This code defines a Vue.js component with a button. When the button is clicked, it toggles the presence of the class 'some-class' on the <body> element.

  2. "Vue.js toggle body class on button click component"

    Description: This query searches for methods to toggle a class on the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { document.body.classList.toggle('some-class'); } } } </script> 

    This Vue.js component contains a button that, when clicked, toggles the class 'some-class' on the <body> element using JavaScript DOM manipulation.

  3. "Vue.js add class to body on button click component"

    Description: This query aims to add a class to the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="addClassToBody">Add Class to Body</button> </div> </template> <script> export default { methods: { addClassToBody() { document.body.classList.add('some-class'); } } } </script> 

    In this Vue.js component, clicking the button triggers the addClassToBody method, which adds the class 'some-class' to the <body> element.

  4. "Vue.js toggle body class on button click Vue component"

    Description: This query looks for methods to toggle a class on the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { document.body.classList.toggle('some-class'); } } } </script> 

    In this Vue.js component, clicking the button toggles the class 'some-class' on the <body> element using the toggleBodyClass method.

  5. "Vue.js button click toggle class body element"

    Description: This query aims to toggle a class on the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { document.body.classList.toggle('some-class'); } } } </script> 

    This Vue.js component contains a button that, when clicked, toggles the class 'some-class' on the <body> element using JavaScript DOM manipulation.

  6. "Vue.js add class to body on button click Vue component"

    Description: This query searches for methods to add a class to the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="addClassToBody">Add Class to Body</button> </div> </template> <script> export default { methods: { addClassToBody() { document.body.classList.add('some-class'); } } } </script> 

    Clicking the button in this Vue.js component triggers the addClassToBody method, which adds the class 'some-class' to the <body> element.

  7. "Vue.js toggle body class on button click Vue"

    Description: This query looks for methods to toggle a class on the <body> element when a button is clicked within a Vue.js component.

    <template> <div> <button @click="toggleBodyClass">Toggle Body Class</button> </div> </template> <script> export default { methods: { toggleBodyClass() { document.body.classList.toggle('some-class'); } } } </script> 

    Clicking the button in this Vue.js component toggles the class 'some-class' on the <body> element using the toggleBodyClass method.


More Tags

gpu wifi-direct postgresql-11 real-time-data kettle amazon-route53 react-native-ios pdf-form internet-explorer-8 abaddressbook

More Programming Questions

More Weather Calculators

More Financial Calculators

More Tax and Salary Calculators

More Geometry Calculators