Can menus in Vue.js be customized differently for various "pages"?

I am new to Vue and facing a challenge with a common component - menu - that needs to be styled differently on the "TOP page" compared to all other "pages".

<template>
  <div>
    <menu></menu>
    <transition name="fade" mode="out-in">
      <router-view>TOP page and all the other pages</router-view>
    </transition>
  </div>
</template>

I am looking to apply distinct CSS properties like background and link color to the "TOP page". Is this achievable?

Answer №1

To efficiently handle this situation, you can establish a computed property named isTop that will evaluate to true if the user is currently on the "Top page."

If the menu is a subcomponent, you can transmit a prop to it like this:

<menu :isTop='isTop'>

Then, utilize Vue's conditional class syntax inside the child component to dynamically apply the appropriate CSS class depending on the value of isTop:

// Child component template:
<div :class='[{"menuTop": isTop}, {"menuGeneral": !isTop}]'>
...
</div>

If you intend to use the <menu> HTML tag directly, you can apply a similar syntax to it without using a child component:

<menu :class='[{"menuTop": isTop}, {"menuGeneral": !isTop}]'></menu>

In both cases, your computed property will resemble this:

computed: {
  isTop: function() {
    // The method to determine if you are on the "Top page" goes beyond the scope of this discussion.
    // For instance, with Vue Router, you could compare the current route with the Top page's route.
    // Return true if you are on the "Top page" and false otherwise.
  }
}

Your stylesheet will then include something along these lines:

<style>
  .menuTop {
    color: blue;
  }
  .menuGeneral{
    color: green;
  }
</style>

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

Is there a way for me to expand the dropdown menu?

My current dropdown looks like this. I'd like the dropdown menu to span across the screen, something like this: To create my dropdown, I am using https://github.com/angular-ui/ui-select2. AngularJS JQuery Select2 I'm not sure if there is an ...

What is the best way to ensure that an input group expands to fill the entire horizontal space

I am currently utilizing bootstrap 4 and have a responsive navbar for smaller screens. On this navbar, I am trying to implement a search input group that spans the full width from left to right up until the menu bar icon on the right side. You can view my ...

Issue with Vue and Inertia.js: database value not being updated

Check out the form I have created: <form @change="updatePipeline"> <select v-model="pipeline.input_mode"> <option value="text">Text</option> <option value="html">HTML</opt ...

Firefox does not support CSS transitions

I've put in a lot of effort and even tried searching on Google, but I'm unable to find a solution to my problem: To help you understand my issue better, I have created a jsfiddle with my source code: Click here to view my Source Code Although e ...

Tips for effectively applying an image as a border

Just starting out with HTML and CSS here, and I'm trying to figure out how to use an image as a border for a div element. Can someone help point out my mistake? div { width: 240px; height: 510px; background-color: lightblue; border-image ...

VueJS fails to display table information

I am facing an issue with rendering data from my API using VueJS 2. Although the backend services are successfully sending the data, the HTML table does not display it. There are no errors shown in the debug console, and even the Vue Debug extension for Fi ...

Is there a way to stack multiple Divs on top of each other using Float instead of relying on absolute positioning?

I've decided to revamp my approach and transition from using absolute positions to utilizing floats for positioning elements the way I desire. Now the challenge lies in figuring out how to float multiple divs on top of each other, allowing users to s ...

Styling elements with CSS and using it to toggle the visibility of divs based on radio

When the radio button #my102 is selected, the div #navi102 should be visible and all other #navi??? divs should be hidden. Similarly, if the radio button #my7 is selected, the div #navi7 should be visible and others hidden. What would be the best approach ...

Having trouble making SCSS mixins work with CSS variables in NextJS?

My current next.config.js setup looks like this: module.exports = (phase, {defaultConfig}) => { if ('sassOptions' in defaultConfig) { defaultConfig['sassOptions'] = { includePaths: ['./styles'], ...

Adjust the element's height as you scroll

I am currently experimenting with a method to dynamically increase the height of an element based on user scrolling behavior. Despite trying multiple approaches, I have encountered challenges in getting it to work effectively. The concept is as follows: ...

Creating a CSS layout that eliminates the need for a vertical scroll bar

Currently, I am experimenting with creating an HTML CSS Layout using the div tag. You can view the code here Currently, the layout displays a vertical bar that I would like to eliminate. Ideally, I only want it to display if the content is lengthy. ...

Utilizing grid for styling headings and paragraphs with h3, h4, and p tags

Looking for help with aligning posts in columns? Here's the code and display challenge: <div class="post-inner"> <h3 class="post-header"><a class="post-title" href="http://www.yellowfishjobs.com/job/sales-representative/">Sales Repr ...

What could be the reason the background-color isn't changing when I apply button:hover?

body{ font-family:"Arial","Lucida Grande", "Lucida Sans Unicode", Verdana, Helvetica, sans-serif; font-size:12px; } p, h1, form, button{border:0; margin:0; padding:0;} .spacer{clear:both; height:1px;} /* ----------- My Form ----------- */ .myform{ margin:0 ...

Unlocking the Power of Transition: Effortlessly Submitting a Form Post

After the modal finishes fading out, I want my form to be submitted and sent to the email file "refreshform.php". However, currently after the modal fades out, the form does not submit or post anything to the PHP file for sending the email. It simply fades ...

What causes Chrome Extension Images to appear broken when they are inserted into the DOM?

Currently working on a Chrome extension, I am attempting to insert a div with a background image into the DOM using a content script. The CSS is loading correctly, and upon inspecting the Developer Tools, the image URL appears to be correct. $('.clos ...

Tips for utilizing a variable within a variable containing HTML code

Is it possible to incorporate variables in a JavaScript function that includes HTML code? Let's consider the following example: function SetCFonts() { var Color = $('#CColor').val(); var Font = $('#CFont').val(); var S ...

generateError('Circular reference found' + nodeRep)

Incorporating numerous charts in my application, I aim to manage all chart colors from the vuex store. Implementing the following code has allowed me to achieve this functionality. store.js import Vue from 'vue' import Vuex from 'vuex&apos ...

Incorporating a hyperlink within a list item for an image

Currently, I am attempting to create a clickable image within an unordered list. Although the paragraph is clickable, unfortunately, the image itself is not responding as expected. <ul> <a href="/movies/test/"><li> ...

Creating columns of equal height using Bootstrap 3 and flexbox

Working with Bootstrap 3 (unable to switch to Bootstrap 4) and looking to achieve the same-height columns effect as shown in the image: https://i.sstatic.net/WR55a.jpg Managed to get it done using position absolute and @media screen for image size and padd ...

How can the ordering of dynamically generated components be synchronized with the order of other components?

Currently, I'm delving into Vue 3 and encountering a specific issue. The tabs library I'm using only provides tab headers without content panels. To work around this limitation, I've come up with the following solution: <myTabs/><!- ...