Having a Jquery resizing problem? No worries! When the width is less than 768, simply enable the click option. And when the width is

HTML

<div class="profile">
        <a href="#" class="hoverdropdown" onclick="return false;">Profile</a>
                    <ul class="dropdown" style="display: none;">
                      <li><a href="#">Dashboard</a></li>
                      <li><a href="#">Logout</a></li>
                    </ul>
                </div>

Jquery

$(document).ready(function(){
    listDropdown();
});
$(window).resize(function(){
    listDropdown();
});

function listDropdown()
{
    var windowWidth=$(window).width();
    if(windowWidth>=768)
    {
        $(".profile").each(function(){
            $(this).mouseover(function(){
                $(this).find(".dropdown").show();
            });
            $(this).find(".dropdown").mouseout(function(){
                $(this).hide();
            });         
        });
        $(document).mouseout(function(e) { 
            if(($(e.target).parents('.profile').length<=0))
            {
                $(".profile .dropdown").hide();
            }
        });
    }
    else if(windowWidth<769)
    {
        $(".profile").each(function(){
            $(this).click(function(){
                $(this).find(".dropdown").toggle();
                $(this).find(".hoverdropdown").children(".fa-plus").toggleClass("fa-minus");
            });
        });     
    }
}

For window widths less than 768, click option is enabled. For window widths above 768, mouseover option is enabled.

Any alternative solutions are welcome.

View the fiddle here

Answer №1

Give this code a try:


function adjustDropdown() {
    var windowWidth = $(window).width();
    if (windowWidth >= 768) {
        $('.profile').addClass('over');
        $('.profile').removeClass('click');

    } else if (windowWidth < 769) {
         $('.profile').removeClass('over');
        $('.profile').addClass('click');
    }
}
$("body").on('mouseover', '.profile.over', function() {
    $(this).find(".dropdown").show();
});
$(".dropdown").mouseout(function() {
    if ($(this).closest('.profile').is('.over')) {
        $(this).hide();
    }
});

$(document).mouseout(function(e) {
    if ($(e.target).parents('.profile.over').length <= 0) {
        $(".profile.over .dropdown").hide();
    }
});

$("body").on('click', '.profile.click', function() {
    $(this).find(".dropdown").toggle();
    $(this).find(".hoverdropdown").children(".fa-plus").toggleClass("fa-minus");
}); 

Link to the working example

Answer №2

Consider this alternate approach.

HTML

<div class="user-profile">
  <a href="#" class="hover-effect" onclick="return false;">Profile</a>
  <ul class="dropdown-menu">
    <li><a href="#">Dashboard</a></li>
    <li><a href="#">Sign Out</a></li>
  </ul>
</div>

CSS

.dropdown-menu{
  display: none;
}

.hover-effect:hover + ul{
  display: block;
}

// Media query for smaller screens
@media (max-width:767px){
  .open .dropdown-menu{
    display: block;
  }
}

jQuery

$('.hover-effect').click(function(){
  $(this).parent().toggleClass('active')
})

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

Guide to retrieving a byte array from a server using JavaScript and converting it into a downloadable PDF

I am attempting to convert a byte array received from the server and download it as a pdf. The download functionality is working, but unfortunately, the file appears to be corrupt. My approach involves using JavaScript and vue.js. Function responsible fo ...

Do you know the method to make a Youtube iframe go fullscreen?

I am encountering an issue with developing an iframe where I am unable to make it go fullscreen from within the iframe. Fullscreen API works when both the iframe and hosting website are on the same domain, as well as triggering fullscreen from outside the ...

When an element is dragged within the mcustomscrollbar container, the scroll does not automatically move downward

I am facing an issue where I have multiple draggable elements inside a Scrollbar using the mcustomscrollbar plugin. When I try to drag one of these elements to a droppable area located below the visible area of the scroller, the scroll does not automatical ...

When an AJAX request is successful in Angular, utilize the ng-hide directive

I've implemented a feature where double-clicking the name displays an input field, but I'm facing an issue with switching back after a successful put-request. Despite setting $scope.edit to false, it doesn't seem to work as expected. This is ...

The div in Bootstrap is not becoming scrollable despite using "overflow: auto"

I have a unique setup with two divs nested inside another div. The first div contains a table, while the second div holds buttons. I'm trying to make the table div scrollable by using overflow-auto, but it's not working as expected. I suspect tha ...

Generate a dot density map with the help of Google Maps

I am looking to create a dot density map using Google Maps for my state. I have all the counties outlined with their respective populations, and I want to scatter dots randomly within each county to represent the population. The goal is to make a dot densi ...

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

Angular 2, the change event is not triggering when a bootstrap checkbox is clicked

Encountering an issue, the (change) function is not triggering in a specific checkbox. <input [(ngModel)]="driver.glasses" name="glasses" type="checkbox" class="make-switch" (change)="changeFunction()" data-on-color="primary" data-off-color="info"&g ...

Protractor: How to Handle Multiple Browser Instances in a Non-Angular Application and Troubleshoot Issues with ignoreSynchronization

I have encountered an issue while using protractor to test a Non-Angular application. After implementing the browser.forkNewDriverInstance(), it appears that this function is no longer functioning correctly as I am receiving the following error message dur ...

Tips for invoking the ajax method repeatedlyWould you like to learn

I am facing an issue with my ajax call. In the code snippet below, the pageReload() function is automatically triggered when the page loads, subsequently calling the ajaxCall() function every 10 seconds. I am trying to invoke a method in a Grails control ...

Tips for aligning two divs on top of each other

I'm trying to figure out how to have divs start drawing from the same point. Essentially, I want to be able to add new divs and have them appear stacked on top of each other, so that they can all move together based on a single reference point. Here& ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

Effortless method to handle package.json configurations

Is there a better approach for seamlessly transitioning between using npm link and git, or another solution that caters well to both front end and back end developers? The dilemma I'm facing revolves around developing a website that utilizes multiple ...

Failing to retain hyperlinks with ajax

Currently, I am utilizing ajax to transmit data from a sophisticated custom field wysiwyg editor. Within this setup, the specific div with the class 'bio' is what I'm addressing. The issue arises when the data is retrieved - all the original ...

The implementation of display flex is causing issues with the material-ui grid layout

Struggling to implement the grid system from material-ui due to an issue where grid items do not resize when resizing the browser window. The culprit seems to be a parent div with the style property "display": "flex". The dilemma arises from using a mater ...

Is there a method to display a loading animation while the micro apps are being loaded in a single spa project?

Currently, I am working on a project using single spa and I need to implement a loader while my micro app is being loaded. Additionally, I also need the loader to be displayed when switching between these micro apps. Are there any methods to accomplish t ...

What is the best method to send user information from a registration form to a MySQL database table using AJAX without having to refresh

Currently facing an issue where user registration data (email, username, password) is not being submitted to the register table tbl_register when the submit button is clicked. Utilizing PHP with AJAX to prevent page refresh upon clicking submit and sendin ...

Locate the final element within an array using JavaScript

Provided with a file path like new/lib/java.exe, I am looking to eliminate the root folder 'new' and structure the new path as lib/java.exe. Challenge: After my attempts, I am left with the path as lib/java.exe/ which includes an unwanted "/". I ...

How to retrieve the object's property with the highest numerical value using JavaScript

My current object is structured as follows: const obj = { happy: 0.6, neutral: 0.1, said: 0.3 } I'm trying to determine the best method to retrieve the property with the highest value (in this case, it would be "happy"). Any suggestions o ...

What is the most effective way to implement a CSS stylesheet on a particular individual element?

Just starting out in web development and I recently experimented with a CSS stylesheet on a basic HTML element. It worked great when I targeted the element by name like this: label { color: green; } However, I wondered if there was a way to apply sty ...