Spinning Twitter Bootstrap4 Navbar Menu/Submenu Arrow

Seeking assistance from the experts! I've successfully rotated the original drop down caret on BS4. However, struggling to rotate the submenu caret. Can someone please review and provide feedback on what might be going wrong? Ideally looking to achieve this using CSS and Bootstrap4 without FontAwesome.

Thank you in advance!

<nav class="navbar navbar-expand-lg navbar-custom py-1" style="background-color:#0082bb;">
  <a class="navbar-brand" href="#" base target="parent">Brand</a>
  <button class="navbar-toggler update-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerToggle" aria-controls="navbarTogglerToggle" aria-expanded="true" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>

  </button>
  <div class="collapse show navbar-collapse" id="navbarTogglerToggle">

    <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
      <li class="nav-item active">
        <a class="nav-link" href="#" base target="parent">Home <span class="sr-only">(current)</span></a>
      </li>
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Resources
        </a>
        <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <li><a class="dropdown-item" href="#" base target="parent">Action</a></li>
          <li><a class="dropdown-item" href="#" base target="parent">Another action</a></li>
          <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" href="#">Submenu</a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#" base target="parent">Submenu action</a></li>
              <li><a class="dropdown-item" href="#" base target="parent">Another submenu action</a></li>
.dropdown-toggle[aria-expanded="true"]:after {
  transform: rotate(-180deg);
}


.dropdown-toggle:after {
  transition: 0.2s;
}

.dropdown-submenu {
  position: relative;
}

.dropdown-submenu a::after {
  transform: rotate(-90deg);
  position: absolute;
  right: 6px;
  top: .8em;
}

Answer №1

In order to tackle this issue, we monitor for a click on

.dropdown-submenu .dropdown-toggle
and dynamically add or remove a class that alters the direction of the caret symbol from left to down and vice versa. The complete code snippet is provided below:

$('.dropdown-menu a.dropdown-toggle').on('click', function(e) {
  if (!$(this).next().hasClass('show')) {
    $(this).parents('.dropdown-menu').first().find('.show').removeClass("show");
  }
  var $subMenu = $(this).next(".dropdown-menu");
  $subMenu.toggleClass('show');

  $(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) {
    $('.dropdown-submenu .show').removeClass("show");
  });

  return false;
});

/* for TOP LEVEL MENU */
$('.dropdown>.dropdown-toggle').click(function() {
  if ($(this).hasClass('caretDown')) {
    $(this).removeClass('caretDown');
  } else {
    $(this).addClass('caretDown');
  }
});

/* for SUB MENUS */
$('.dropdown-submenu>.dropdown-toggle').click(function() {
  if ($(this).hasClass('caretDown')) {
    $(this).removeClass('caretDown');
  } else {
    $(this).addClass('caretDown');
  }
});
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu a::after {
  transform: rotate(-90deg);
  position: absolute;
  right: 6px;
  top: .8em;
}

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-left: .1rem;
  margin-right: .1rem;
}

#navbarDropdownMenuLink::after {
  transform: rotate(-90deg);
}

a.caretDown::after {
  transform: rotate(0deg) !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home
            <span class="sr-only">(current)</span>
          </a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Dropdown link
          </a>
        <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <li>
            <a class="dropdown-item" href="#">Action</a>
          </li>
          <li>
            <a class="dropdown-item" href="#">Another action</a>
          </li>
          <li class="dropdown-submenu">
            <a class="dropdown-item dropdown-toggle" href="#">Submenu</a>
            <ul class="dropdown-menu">
              <li>
                <a class="dropdown-item" href="#">Submenu action</a>
              </li>
              <li>
                <a class="dropdown-item" href="#">Another submenu action</a>
              </li>


              <li class="dropdown-submenu">
                <a class="dropdown-item dropdown-toggle" href="#">Subsubmenu</a>
                <ul class="dropdown-menu">
                  <li>
                    <a class="dropdown-item" href="#">Subsubmenu action</a>
                  </li>
                  <li>
                    <a class="dropdown-item" href="#">Another subsubmenu action</a>
                  </li>
                </ul>
              </li>
              <li class="dropdown-submenu">
                <a class="dropdown-item dropdown-toggle" href="#">Second subsubmenu</a>
                <ul class="dropdown-menu">
                  <li>
                    <a class="dropdown-item" href="#">Subsubmenu action</a>
                  </li>
                  <li>
                    <a class="dropdown-item" href="#">Another subsubmenu action</a>
                  </li>
                </ul>
              </li>

            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

UPDATE: A correction has been made to include the toggle functionality for the top navigation link caret after being pointed out by the questioner.

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

The accordion is experiencing functionality issues

I created a customized Accordion based on a code snippet I found online to incorporate into my project, but unfortunately, it's not functioning as expected. Currently, it is displaying the address of the high school and failing to hide it when the pag ...

Adjusting the ng-bootstrap carousel to fit within a div inside an ng-bootstrap modal

I have been struggling to correctly adjust the CSS properties in order to make a ng-bootstrap carousel image fit into a specific space (div) within a custom ng-bootstrap modal. Take a look at this modified StackBlitz code. In the provided example, the ima ...

Event submission does not activate on mobile devices when the keyboard is displayed

When using Jquery 1.11.1, I send an ajax request on form submission: ... contactForm.on('submit', function(e) { e.preventDefault(); $.ajax({...}); ... The ContactForm comprises of an input text, a textarea, and a submit button. While in de ...

React Native tutorial: Changing button color on press

When a user clicks on the TouchableOpacity element, I want to change the color of the button from the default gray to blue. Initially, the 'checked={this.state.newProjects}' newProjects variable is not present in the state, so when clicked it sho ...

Show a compact graphic in the upper-right-hand corner

Hey, I have this interesting idea but CSS isn't my strong suit. Any thoughts on how to achieve it? I'm looking to create a new class that, when applied to an item (like a div), displays a small clickable pre-defined image in the Top-Right corne ...

The issue with Navbar images not displaying properly seems to be isolated to Firefox, as other browsers are not experiencing

My webpage is displaying photos incorrectly in the Firefox browser, even though they appear fine on other browsers. I am using Bootstrap 4. View in Chrome View in Firefox ...

Getting data sent via Ajax and jQuery through window.location.href in a new controller in CodeIgniter

How to retrieve ajax jquery data passed through window.location.href in a new controller using CodeIgniter? $(".x").on("click","a.p",function(){ productId = $(this).attr("productId"); alert("product ID : " + product ...

Tips for showcasing the latest Google Map InfoWindows above previous ones

I have a dynamic Google Map with InfoWindows being added dynamically, but I am facing an issue where overlapping InfoWindows do not always display the most recent one on top of older ones. Is there a way to ensure that the latest InfoWindow always shows u ...

Adjust the Bootstrap flex settings to display only 3 cards instead of the default 4 cards - HTML

We currently have a bootstrap setup that displays 4 horizontal scroll cards by default. However, we are looking to only show 3 cards in view and allow the 4th card to be viewed by scrolling. Code On mobile view, the code is still displaying 4 tiles even ...

Adjustable positioning and size of dropdown menu (triggered by clicking)

Currently developing the following webpage: https://jsfiddle.net/t9f2ca4n/212/ Check the raw code at the bottom of the page. The issue at hand involves adding two buttons for each line item (Item I or Item II) to display setup details under "Cate ...

Steps for creating a vertical line divider between two columns using Bootstrap 4

Looking to create a visual divider between my two div columns on the webpage. <div class="container-fluid"> <div class="row"> <div class="col-md-3"></div> <div class="col-md-9"></div> </div> </div ...

Code utilizing the url_for method to reference a Stylesheet image located in a neighboring directory

I'm in the process of creating a website with Flask using Python 3.9, and I have the following directory structure: environment/ main.py app.yaml requirements.txt Templates/ base.html #base template index.html ...

Creating a responsive HTML5 webpage that adjusts seamlessly to all screen sizes without the need for scrolling

I have developed an HTML5 application for mobile devices. Currently, I have managed to address width-height issues by using a simple scroll feature when necessary. However, I have encountered a challenge with a particular page that contains a lot of conten ...

Troubleshooting the issue with the collapse feature of the Angular UI

I am struggling to ensure my navbar starts collapsed using the code provided below. I am utilizing Angular-ui-bootstrap: navbar.directive.html: <button type="button" ng-click="isCollapsed = !isCollapsed"> <span class="sr-only">Toggle naviga ...

IE8 - Unable to use rgba()

I'm currently facing an issue with RGBA() manipulation in jQuery while using IE 8. Here's the code I have so far: $('.set').click(function (e) { var hiddenSection = $('div.hidden'); hiddenSection.fadeIn() . ...

Issue with JQuery UI dialog performance when closing following the execution of a function

I came across a similar query about a slow closing jQuery modal dialog, but the difference is that in this scenario, the buttons attribute is not being used which is causing an issue for me. How can I effectively close the dialog without using $( this ).di ...

The functionality of jQuery binding is not functioning properly

I've been playing around with jQuery and have a code snippet that includes: // buttons for modifying div styles by adding/removing classes <button class="size" id="switcher-large"> Large Print </button> <button class="size" id="switche ...

When attempting to PUT a JSON object to a specific URL, an error occurs

One of my current tasks involves creating a function to send requests to a specific URL function json_object_todo(url, method, param_object, dataObj) { var json_obj; $.support.cors = true; try { var ajax_params = { type: me ...

Center the title vertically APEXCHARTS

I created this chart using Apex charts https://i.sstatic.net/j34Wc.png However, I am facing an issue where the 'Chart' title and caption are not properly aligned. The title should align with the graph horizontally, and the caption vertically. ...

Establish a minimum width requirement for a Bootstrap4 webpage and ensure that scrollbars are visible

As I work on designing a website, I am facing an issue with responsiveness that I am willing to accept for now. I am looking to establish a minimum width for the overall page to be around 1000px, with a horizontal scrollbar appearing if the screen width is ...