using style binding with event handling in vue.js

I am trying to toggle one element and, at the same time, bind styles to another element. I'm having trouble figuring out how to do this using @click.

data(){
    return {
        show:false,
        filterStyle: {
            top: 0,
            background: "#dfe4ea",
            marginTop: "15px",
            marginBottom: "15px",
        },
    }
}

methods: {
    closing(){
        this.show = !this.show
    },
}

<p class="closeMap" @click="closing()">close</p>

Div for toggling below.

<div v-show="!show"></>

Div for applying styles below.

<div :style="filterStyle" class="filter"></div>

Can anyone help me understand how to achieve this?

Edit: Currently binding styles as shown above, but want to do it using @click.

Answer №1

If you're unsure whether to apply style to 'show' or '!show', there's a way to do it like this:

<div :style="show ? customStyle : null" class="custom-container"></div>

The 'customStyle' will only be implemented when 'show' is set to true.

Answer №2

One approach could be creating a computed property that dynamically changes based on the value of this.show:

// HTML Template
<div :style="dynamicStyle" class="filter"></div>

// Computed Property
computed: {
  dynamicStyle() {
    if (this.show) {
       return {
         top: 0,
         background: '#dfe4ea',
         marginTop: '15px',
         marginBottom: '15px'
       };
    } else {
      return '';
    }
  }
}

Alternatively, you can also update the value of dynamicStyle in a click event function.

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

Showcasing the selected menu item's value on an Angular button

I've encountered an issue where I have a list of menu items: <md-menu-item ng-value="menuItem.value" ng-repeat="menuItem in filtermenu.menuItems" ng-click="activeFilterCtrl.selectedfilter(menuItem)" translate> <md-button> {{ m ...

Overcoming Troublesome Login Input Box in Web

In an effort to streamline tasks in our office, I am working on automating certain processes. Specifically, this program is designed to log into our insurance company's website and extract information for payroll purposes. Below is the code snippet th ...

Chromium based browsers are displaying varying values when using canvas.getimagedata()

Update: The solution is to enable willReadFrequently: true on the canvas in Chromium so that getImageData() will consistently return values. Please refer to the answer below for more details. Currently, I am working on a program that selects a pixel from ...

When using Expressjs MVC, encountering difficulties in retrieving data from mongoose in the listAll() function within the router

I'm currently working on implementing MVC-like architecture in Express.js for a very specific scenario. I suspect there may be an issue with promises, but I'm struggling to debug the problem effectively. Here's how the architecture is set u ...

I am attempting to activate the "about us" button on the website. I have successfully included the path and added a router link to the containing div of the button. However, there seems to be something

In my app, the first step involves specifying the path in the routing module. Following that is defining the home component, then the app component, and finally creating the button using HTML. Setting up the path in the app.routing.module.ts file <div ...

Maintaining the aspect ratio of images in Bootstrap Carousel: A complete guide

Using the default carousel from Bootstrap template has been working well for me. However, I've encountered an issue where resizing the browser window distorts the image aspect ratio by squishing it horizontally. I've tried various solutions to m ...

Starting a fresh Angular project yields a series of NPM warnings, notably one that mentions skipping an optional dependency with the message: "npm

Upon creating a new Angular project, I encounter warning messages from NPM: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68e01b0d1e0d061c7518d7">[email protecte ...

Discover a corresponding object within an array

I am currently dealing with an array of objects in Javascript where I need to verify if a certain value exists within any object and return the corresponding id. The issue arises when using the some() function as it only seems to match the first object. T ...

The syntax of React Native's <{}> component structure

After using the command react-native init to create a new React Native project, I noticed that within the default template, the main component class is defined as follows: export default class App extends Component<{}> { ... } The <{}> par ...

Proper Usage of Vue3 with Vue Router

Trying out Vue3 has been a bit challenging for me. The router setup is custom to my needs. import router from './router' When I include the router like this: createApp(App).use(Antd,VueAxios,axios,qs,router).mount('#app') The page fa ...

What could be causing my CSS navigation toggle button to malfunction?

My attempt at creating a toggle button for tablets and phones seems to be failing. Despite the javascript class being triggered when I click the toggle button, it is not functioning as expected... https://i.stack.imgur.com/j5BN8.png https://i.stack.imgur. ...

Clear information upon clicking to switch to a new controller

I have a scenario where one div contains another div. The contained div has its own controller that controls its visibility based on a variable changed by clicking an icon button in the container. The structure is as follows: <div ng-controller="BarCo ...

What could be the reason for my https source being restricted from accessing the navigator Geolocation service?

While trying to retrieve a user's location using the code below: var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(BingMap.map); geoLocationProvider.getCurrentPosition( { showAccuracyCircle: false, timeout: 6000, successCa ...

I am puzzled by the fact that my data is showing as undefined even though I have logged it to the console in

I am currently working on an app using React for the frontend and Node.js for the backend, which connects to a cloud-based MongoDB instance. Although I can successfully retrieve data from the database and see it logged in the console, the returned result i ...

Customizing the background color of an option using HTML and CSS

I am currently developing an option form where each selection will appear in a different color as the user scrolls down. Once a selection is made, it should be saved and displayed with the chosen color. https://i.sstatic.net/gvdpt.png My goal is to save ...

Utilizing React JS to Export Axios Response

I have an getAllUsers.js File that retrieves all users from the API. import axios from 'axios' export const fetchData = async () => { let response try { response = await axios.get('http://127.0.0.1:8000/api/users') } catc ...

What is the correct way to trigger an event specified as a string parameter in the emit() function?

My current goal is to pass the emit name as a string (for example, 'showComponent') from child to parent. I then want to trigger another emit in the emitAction(callbackName: string) function, and finally execute the showComponent() function. I&a ...

How can I adjust the time in a range slider using AngularJS?

Currently, I am utilizing a Slider with draggable range in angular js for time selection. The Slider can be found here: https://jsfiddle.net/ValentinH/954eve2L/. I aim to configure the time on this slider to span from 00.00 to 24.00, with a 10-minute inter ...

What is the process for displaying an image based on a selection from a dropdown menu?

I am attempting to create a form that will show a movie poster once a user selects which movie they want to view. I have written a function for this purpose, but as a beginner in javascript, something seems to be going wrong. Any assistance you can provid ...

Activate Angular Material's autocomplete feature once the user has entered three characters

My goal is to implement an Angular Material Autocomplete feature that only triggers after the user has inputted at least three characters. Currently, I have it set up so that every click in the input field prompts an API call and fetches all the data, whic ...