Tips for creating a stylish navbar with a faded effect on the right side and integrating a fresh button into its design

I'm looking to enhance my Navbar by implementing a feature where, if the width of the Navbar exceeds that of its parent div, it will fade on the right side and a new button will be added - similar to what can be seen on Youtube.

1- When the Navbar's width is greater than its parent div, it should resemble this image:https://i.stack.imgur.com/4M5AG.png

2- Conversely, when the Navbar's width is less than its parent div, it should look like this: https://i.stack.imgur.com/Jc4lu.png

How can I go about achieving this effect?

Below is the code for my existing Navbar setup:

  <template>
    <nav class="navbar navbar-expand-lg navbar-light bg-light pl-4">
      <form class="form-inline" v-for="(genre, index) in genres" :key="index">
        <button class="btn btn-outline-dark m-1" type="button">
          {{ genre }}
        </button>
      </form>
    </nav>
  </template>

  <script>
  export default {
  data() {
    return {
      genres: [
        .
        .
        .
        "REALITY TV",
        "SPORTS",
        "HORROR"
        ]
      };
    }
  };
  </script>

Answer №1

Here's a solution for managing button containers with smooth scrolling:

  • Create a parent div to contain all buttons and set overflow hidden to prevent scrolling. Place this container within another parent div.
  • Position arrow buttons on the left and right of the button container using absolute positioning.
  • Manually scroll the container by adjusting the CSS property transform: translateX(0px).
  • Update the value of px to adjust the translation when arrow buttons are clicked.
  • Determine the size of the container div and parent div, calculate max and min scroll values. Ensure that translateX remains within bounds while scrolling to prevent overscrolling. Display or hide arrow buttons based on these values.

Check out the working demo on codesandbox.
You can also find the code provided below:

<template>
  <div>
    <nav class="navbar" ref="navbar">
      <div class="left-arrow" v-if="translateX < 0">
        <div class="left-arrow-button" v-on:click="moveLeft">&lt;</div>
      </div>
      <div class="scroll-container" ref="scroll" :style="scrollStyle">
        <div class="btn" v-for="(genre, index) in genres" :key="index">
          {{ genre }}
        </div>
      </div>
      <div class="right-arrow" v-if="translateX > minVal">
        <div class="right-arrow-button" v-on:click="moveRight">&gt;</div>
      </div>
    </nav>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // Data for genres
    };
  },
  computed: {
    // Computed properties for scroll styles and minimum values
  },
  mounted() {
    // Method for calculating scroll width
  },
  methods: {
    // Methods for moving left and right
  },
};
</script>

<style scoped>
// Styling for navbar, scroll container, buttons, arrows, etc.
</style>

This implementation uses basic CSS and HTML tags. If you prefer to use frameworks like Bootstrap for styling, feel free to adapt the code accordingly. Smooth scrolling animations, like those seen on YouTube, have not been implemented here for simplicity. You can explore adding animations on your own and seek assistance if needed.

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

Using simpleXML to extract HTML code from an XML document

Presented below is content extracted from an xml file: <Cell> <Comment ss:Author="Mark Baker"> <ss:Data xmlns="http://www.w3.org/TR/REC-html40"><B><Font html:Face="Tahoma" html:Size="8" html:Color="#000000">Mark B ...

Can AJAX be considered a backend tool for retrieving data from servers?

I'm curious to find out if ajax is primarily used as a backend technology for retrieving data or if it's mainly considered a frontend tool. My attempts to research this on Google have not yielded a definitive answer. ...

The use of callbacks is ineffective in addressing the asynchronous nature of

Hello everyone, I'm currently working on a weather app and I'm facing an issue with the asynchronous behavior of useState. I've come across some suggestions on Stack Overflow that using a callback in the useState function might solve the pro ...

An AJAX request will only occur if there is an alert triggered on a particular computer

Here's an interesting issue I encountered with my company's CMS newsletter system. It seems that the AJAX call to send an email works flawlessly in all modern browsers and operating systems, except for one client. This particular client is using ...

The extended class possesses a distinct type from the base class, which is reinforced by an interface

Is it possible to write a method that is an extension of a base class, but with a different return type, if supported by the shared interface, without adding a type declaration in class 'a'? In practical terms, classes a & b exist in JavaScript ...

Tracking the last time an app was run in Javascript can be achieved by creating a variable to store the date when

Exploring the world of Javascript as a newbie, I find myself with index.html, stylesheet.css and div.js in my app. Within div.js lies a code that magically generates a schedule for our team members over 2 weeks. However, there's a catch - consistency ...

Update the content of an HTML element without having direct access to the HTML code

I am currently in the process of creating a website using a website builder, and I am interested in customizing the default message that is displayed when a required field in the website form is left empty. The form in question is an integral part of the w ...

HTML tables with stripes and row spans

I found the initial table on this link. After reviewing the original table, I am looking to make modifications as shown in the highlighted image below. However, when attempting to add rowspan and colspan attributes to the td elements, I encountered an iss ...

Utilizing the Global Module in NestJs: A Step-by-Step Guide

My current project is built using NestJS for the back-end. I recently discovered that in NestJS, we have the ability to create Global Modules. Here is an example of how my global module is structured: //Module import {Global, Module} from "@nestjs/commo ...

jQuery: Track mouse movement with a delay

I'm looking to create a div that follows cursor movement with a slight delay, similar to the effect seen on this website: In the example link provided, you can observe that the 'follower' has a brief delay in its animation. I attempted to ...

Is there a way to convert an array into an object where the first value in the array becomes the name and the properties are an array of the remaining values within subarrays?

Looking to efficiently group elements of a multidimensional array with an unknown list, and transform it into an object while removing duplicate values in the first element of each subarray: For instance, here's the initial array: const arr = [[a, 1 ...

Steps to create a loading bar that starts loading when an ajax task begins

What is the best way to show a loading bar as an AJAX task begins on a page? Additionally, how can we make sure that hitting the back button will return to what was being viewed before that specific AJAX task? ...

What could be the reason for encountering a TypeError while attaching event listeners using a for loop?

When attempting to add a "click" event listener to a single element, it functions correctly: var blog1 = document.getElementById("b1"); blog1.addEventListener("click", function(){ window.location.href="blog1.html"; }); However, when I try to use a for l ...

What is the best way to send $(this) to a function?

Here is my code snippet: $(document).ready(function(){ var hCount = 0, eCount = 0, nCount = 0, mCount = 0; $("#head").click(function() { var pPos = calculatePosition(hCount); $(this).animate({left:pPos+"px"}, ...

What is the best way to use PHP in order to retrieve the element containing the visit ID from the specific row within an HTML table?

I am working on a project that involves populating an HTML table with data from a database. The table includes an approval button for administrators to approve visits and change their status to APPROVED. I am trying to figure out how to select the element ...

IntelliJ does not support the use of newlines within Vue.js component templates

While working with Vue.js in IntelliJ IDEA, I encountered a small problem related to defining component templates. The issue is that IntelliJ seems to struggle when the template spans more than one line and attempts to concatenate them together. For examp ...

Troubleshooting issue with Vue3 - TS Custom State Management

Issue: I am facing a challenge in transferring data between two separate components - the main component and another component. Despite attempting to implement reactive State Management based on Vue documentation, the object's value remains unchanged ...

Unable to transfer array from backend to vuex store

Issue: Encountered an error in the created hook (Promise/async) - "TypeError: state.push is not a function" Function used in the page created: async function () { this.$store.commit('TicketSystem/ADD_BOARDS', (await this.$axios.get('htt ...

Can an infowindow be automatically closed based on specific criteria?

Show the infowindow only when hovering over the marker. It should disappear when moving the mouse away from the marker. The infowindow should stay open only if you click on the marker, and can be closed by clicking the close button on the infowindow. ...

What is the best way to have Vue i18n fetch translations from a .json file during Unit Testing?

Previously, with vue-i18n (v8.25.0 and vue v2.6.14), I stored all translations in .ts files containing JS objects: import { LocaleMessages } from 'vue-i18n' const translations: LocaleMessages = { en: { test: 'Test', }, } expor ...