Conceal the parent element using v-onclick in Vue 2.0 with a maximum width of 767px

Looking to create a dropdown menu that will only display when the screen size is less than 767px. There are two conditions to hide the menu:

1. If someone clicks outside of the menu.

2. When a random router link is clicked.

export default {
    name: 'navbar',
    data() {
      return {
        isShow: false
      }
    },
    methods: {
      onClick(e) {
        this.isShow = false
      }
    }
}
Home Phones More Products Support News

Answer №1

Displaying the menu based on screen size

Utilizing jQuery to retrieve the width of the browser viewport.

 $(window).width(); 

Create a function that examines the screen width and returns true or false.

showMenu() {
  let screenWidth = $(window).width()
  if (screenWidth < 767) {
    return true
  } else {
    return false
  }
}

Hiding the menu

  1. Implement Vue Clickaway to detect clicks outside the menu.
  2. Include a navigation guard to hide the menu before each route change.

    router.beforeEach((to, from, next) => {
      this.hideMenu() // Hides the Menu
      next()
    })
    

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 it possible to combine asynchronous and synchronous functions in the same code?

I've recently started experimenting with Node.js and I'm running into issues with asynchronous functions. While I was able to create a small game, the only way I could successfully integrate asynchronous functions with synchronous functions was b ...

The criteria is not fulfilled by either item.area_partition.homepage_show or item.area_partition.name in accordance with my needs

I am having an issue with some code snippet: <Col span="8" v-for="(item,index) in this.data" > <Card> <p slot="title" style="">{{ item.area_partition.homepage_show || item.area_partition. ...

Encountering an unexpected token in the JSON file at the very start is causing an

An unexpected error has occurred, showing the following message:- Uncaught SyntaxError: Unexpected token u in JSON at position 0 The code causing the error is as follows:- import { useEffect, useState } from "react"; import { useNavigate, usePar ...

The JavaScript jump function quickly moves back to the top of the webpage

Issue Resolved To prevent the view from jumping to the top of the page, I have implemented three options: Set the href attribute to href="#!" Set the href attribute to href="javascript:;" Pass the object to the event-handler and preve ...

Tips on formatting dates in a Material UI text field

<TextField name="BalDueDate" format="MM/dd/yyyy" value={basicDetails.BalDueDate.slice(0,10)} onChange={event => { ...

Incorporate React to import a file from the parent directory

How can I import a js file called 'usercontent.js' into a react component when the current js file is located in the Component Folder and 'usercontent.js' is located within the Conponent Container folder, which itself is a subfolder of ...

Tips on executing the command `npm run build`

I have a VueJS application that I want to deploy in a cPanel. When I try to run npm run build, I encounter the following error: https://ibb.co/0Qn30TX If I run the npm run command, I get this result: https://ibb.co/Xp9bdDp How can I successfully run np ...

The Verification Tool by Google is malfunctioning on the UI Bootstrap pop-up box

Below is the HTML code where the captcha element is added with comments: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script> <script src="https://ajax.googleapis.com ...

Retrieving JSON data to create and showcase an HTML table

Can you help me figure out what's going wrong with my code? I have an HTML page with a table where I fetch data from the web in JSON format using JavaScript. The logic works perfectly when the fetch code is let to run on its own, but when I try to ex ...

After using promise.all, I successfully obtained an array of links. Now, how do I calculate the total number of links in the array?

function verifyAndTallyURLs(linksArray) { let validations = linksArray.map((link) =>{ return fetch(link) .then((response) => { return { webpageURL: response.url, status: response.status, ...

Implementing coordinate formatting in JavaScript [Node.js]

I'm looking to tweak the JSON output into this specific format: [ 50.87758, 5.78092 ], [ 52.87758, 5.48091 ] and so on. Currently, the output looks like this: [ { lat: 53.1799, lon: 6.98565 }, { lat: 52.02554, lon: 5.82181 }, { lat: 51.87335, l ...

Using PHP to pass values from a MySQL database to JavaScript

hello, $i=0; while($row = $result->fetch_assoc()) { echo " <tr><td>".$row["Number"]."</td><td>".$row["MusikName"]." ".$row["MusikURL"]."</td></tr>"; This portion is successful...it displays -&g ...

Enlarge the div with a click

I was looking for a solution on how to make a div expand when clicked using jQuery I came across a tutorial that seemed simple and perfect, but I couldn't get it to work when I tried to replicate the code. Do you know if this code is still valid wit ...

Discovering the 3D coordinates of a point that is perpendicular to the midpoint of a line

(I am working with Javascript/Typescript and Three.js) Given two vectors, let's say {x:1, y:3, z:5} and {x:7, y:8, z:10}, I have a direct straight line connecting them. At the midpoint of this line, envision a disc with a radius of 1 that is perpend ...

Implementing position sticky on a div depending on its content text - step by step guide

If the text inside the .newsDate matches the previous or next .newsDate, I want to make its position sticky when scrolling, until it reaches the next .newsMiddleCont div. What I'm trying to achieve: The same date on multiple news items should s ...

The issue with json arises when utilizing $.ajax without the definition$

Attempting to make this code work properly Javascript $.ajax({ url: '/endpoint/json/', //Update the path to your JSON file. type: "post", dataType: "json", //Remove the "data" attribute if not needed. data: { ...

Configuring Azure Storage CORS settings for embedding video content

In my project, I am working on creating a panorama tour that includes additional content such as videos using three.js and React. The videos I am using are stored on a private Azure storage, where I generate SAS tokens for specific video files. When atte ...

checking the validity of serialized information in Ajax

I am facing a specific issue where I need to validate data before it is saved through an ajax call. When the user switches to a different URL, the save_ass_rub function is triggered. Within my application, there is a custom Window that allows users to inp ...

Passport is implementing multistrategies for multiple authentications simultaneously

After configuring Passport.js to utilize multiple strategies, I encountered an issue: passport.authenticate(['bearer', 'facebook-token', 'google-token', 'linkedin-token'],function(err, user, info) ... Although it i ...

What is the best way to open Chrome Developer Console using Selenium?

While conducting automated tests with C#, Webdriver, and Chrome, I was exploring the use of the performance.timing.domComplete JavaScript console function through the chrome web developer tool. Here is an example of how I attempted to incorporate it into m ...