Content is pushed by a scrolling drop down menu

I had a drop-down menu that was functioning well. However, when I made changes to allow scrolling for too many items, it started behaving differently.

The drop-down without scrolling doesn't move down, but the one with scrolling does.

JS

var maxHeight = 300;

$(function(){

$(".dropdown > li").hover(function() {

     var $container = $(this),
         $list = $container.find("ul"),
         $anchor = $container.find("a"),
         height = $list.height() * 1.1,       // ensuring enough space at the bottom
         multiplier = height / maxHeight;     // moves faster if list is taller

    // need to store original height here for reverting on mouseout            
    $container.data("origHeight", $container.height());

    // maintains its rollover color while dropdown is open
    $anchor.addClass("hover");

    // ensures dropdown appears directly below parent list item    
    $list
        .show()
        .css({
            paddingTop: $container.data("origHeight")
        });

    // no animation needed if list is shorter than max height
    if (multiplier > 1) {
        $container
            .css({
                height: maxHeight,
                overflow: "hidden"
            })
            .mousemove(function(e) {
                var offset = $container.offset();
                var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                if (relativeY > $container.data("origHeight")) {
                    $list.css("top", -relativeY + $container.data("origHeight"));
                };
            });
    }

}, function() {

    var $el = $(this);

    // revert back to normal state
    $el
        .height($(this).data("origHeight"))
        .find("ul")
        .css({ top: 0 })
        .hide()
        .end()
        .find("a")
        .removeClass("hover");

});   

});

CSS

/* ===== FIRST LEVEL ===== */
.dropdown{position: relative;   margin: 0 auto;float: right;top: 10px;font-size: 13px;}
.dropdown li {float: left; width: 155px; background-color:#373737; position: relative; border-bottom:1px solid #575757; border-top:1px solid #797979;}
.dropdown li a { display: block; padding: 10px 8px;color: #fff; position: relative; z-index: 2000; text-align:center; }
.dropdown li a:hover,
.dropdown li a.hover{background: #CF5C3F; position: relative; }
ul.dropdown > li:last-child {width: 50px;}
.contact{height: auto;}

/* ===== SECOND LEVEL */
ul.dropdown ul { display: none; position: absolute; top: 0; left: 0; width: 180px; z-index: 2; }
ul.dropdown ul li { font-weight: normal; background: #373737; color: #fff;}
ul.dropdown ul li a{ display: block; text-align:center; background-color: #373737!important;} 
ul.dropdown ul li a:hover{ display: block;background: #CF5C3F!important; } 

HTML

<!-- Drop Down Menu -->
    <ul class="dropdown">
            <li><a id="page1" href="index.html">Home</a></li>
            <li><a href="#">Internet Architecture</a>
                <ul class="sub_menu">
                    <li><a href="pages/page1.html">Web Functionality </a></li>
                    <li><a href="pages/page2.html">TCP/IP</a></li>
                    <li><a href="pages/page3.html">DNS</a></li>
                    <li><a href="pages/page8.html">HTTP Requests</a></li>
                    <li><a href="pages/page9.html">SSL</a></li>
                    <li><a href="pages/page1.html">Web Functionality </a></li>
                    <li><a href="pages/page2.html">TCP/IP</a></li>
                    <li><a href="pages/page3.html">DNS</a></li>
                    <li><a href="pages/page8.html">HTTP Requests</a></li>
                    <li><a href="pages/page9.html">SSL</a></li>
                    <li><a href="pages/page2.html">TCP/IP</a></li>
                    <li><a href="pages/page3.html">DNS</a></li>
                    <li><a href="pages/page8.html">HTTP Requests</a></li>
                    <li><a href="pages/page9.html">SSL</a></li>
                </ul>
            </li>
            <li><a href="#">Internet Security</a>
                <ul class="sub_menu">
                    <li><a href="pages/page11.html">Laws</a></li>
                    <li><a href="pages/page10.html">Security Risks</a></li>
                </ul>
            </li>
            <li><a href="pages/page5.html">TCP/IP Layers</a>
            <li><a href="#">Website Performance</a>
                <ul class="sub_menu">
                    <li><a href="pages/page4.html">Client Side</a></li>
                    <li><a href="pages/page7.html">Vector vs Bitmap</a></li>
                    <li><a href="pages/page6.html">Server Side</a></li>
                </ul>
            </li>
            <li class="contact"><a style="padding: 0;" href="pages/contact.html"><img src="images/contact_white.png" width="33px" height="auto"></a></li>

    </ul>

Answer №1

Within your fiddle's JavaScript code on line 28, a height value is being assigned to the parent LI element. As a result, the height of the LI is expanding and pushing down the surrounding elements. To resolve this issue, consider enclosing your UL dropdowns within another element and applying the height and absolute positioning properties to that container instead.

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

Ensure that the form is validated even when setState is not executed immediately

I am currently working on a form in React and I am facing an issue with validation. When the Submit Form button is clicked without filling in the input fields, an error should be displayed. This part is functioning correctly. However, even when the fields ...

The ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

The XMLHTTPRequest Access-Control-Allow-Origin allows cross-origin resource sharing to

I am attempting to send a request to a PHP file. I collect the longitude and latitude from a function in the Maps API, then use AJAX to store these points in a MySQL database. Using AJAX function savePoint(latitude, longitude){ $.ajax({ ...

Changing the orientation of a dropdown menu from horizontal to vertical

Hello, I am seeking assistance to transform the top dropdown menu layout into a vertical one similar to the menu on the left side. Any help would be greatly appreciated as I am running out of ideas! Thank you in advance! @charset "utf-8"; /* CSS Docum ...

Changing all object values to true with React useState

In a certain file, I have defined an object with the following structure: export const items = { first: false, second: false, third: false } Within a component, I am using this object as shown below: import { items } from 'file'; const [el ...

Leverage AngularJS templates across multiple locations

I currently have a table that showcases a list of products. This table is utilized on both the "All products" page and the "My products" page. Here is a snippet of the table code: <tr ng-repeat="product in products> <td>{{product.id}}</td ...

Creating a Precise Millisecond Countdown Timer using Angular 4 in Javascript

I'm currently facing a challenge in Angular 4 as I attempt to develop a countdown timer that accurately displays milliseconds once the timer falls below 1 minute (59 seconds or less) for a sporting event clock. My issue lies with the setInterval funct ...

Symfony2's W3C XHTML validation fails when validating form submissions according to the W3C standards

Having an issue with valid w3.org XHTML - the validator is showing an error on the required attribute of an input: The attribute "required" is not recognized. I firmly believe that this attribute is necessary, as it is included by Symfony2 form builder f ...

Changing the border color of an HTML input is easy with these simple steps:

As part of an educational project, I am developing a login-registration system using HTML, CSS, and PHP. The system will include various HTML text boxes and elements. My goal is to change the border color of a textbox to green when I input something into ...

Cross domain Ajax POST requests using CodeIgniter and AjaxBy utilizing CodeIgn

Initially, I would like to clarify ... I own two domains: www.one.com and www.two.com The first domain www.one.com has a form input below <div class="hidden cswrap2"> <h3>Edit Data Mustahik</h3> <div class="cscontent"> ...

Problem with opening the keyboard feature in an Ionic app

Hello everyone, I'm relatively new to Ionic development and I've been trying to integrate a keyboard plugin into my application that opens from the footer and focuses on input fields for entering values. Here is the link to the plugin I used: ht ...

Determine the parent sibling of the image wrapper by using the specified selector

I am trying to write code that, when a button is clicked on, will find the parent elements' siblings (specifically an image within an "img_wp" div). However, my current code doesn't seem to be working. Can anyone spot where I might have gone wron ...

Tips for centering a bootstrap card on the screen using reactjs

I've been struggling to keep my card centered on the screen despite using align-items and justify-content. I've researched on various websites to find a solution. //Login.js import React, { Component } from 'react'; import './App ...

"Integrating Associated Models with Sequelize: A Step-by-Step Guide

I am attempting to retrieve all transactions along with their associated stripePayments, but I keep encountering the error include.model.getTableName is not a function. In my database schema, there is a table for transactions that has a one-to-many relati ...

CSS styles do not apply to PDF files when downloading

My table headers are styled with writing-mode: vertical-lr, and they look perfect on the screen. https://i.sstatic.net/cQ6Vm.jpg However, when I used the same CSS to style the PDF file, the headers did not display vertically. It seems like the PDF file d ...

JavaScript can be used to create dynamic frames within a

Can anyone help identify the issue in my code? frame 1 <script type="text/javascript"> var Clicks = 0 ; function AddClick(){ Clicks = Clicks + 1; document.getElementById('CountedClicks').innerHTML = Clicks ; } localStorage.setItem(' ...

Convert the existing JavaScript code to TypeScript in order to resolve the implicit error

I'm currently working on my initial React project using Typescript, but I've hit a snag with the code snippet below. An error message is popping up - Parameter 'name' implicitly has an 'any' type.ts(7006) Here is the complet ...

Is there a space added after applying overflow:hidden to a div?

.d1 { height: 10px; } .dd { display: inline-block; overflow: hidden; } .dd1 { display: inline-block; } <div class="d1"> <div class="dd"></div> <div class="dd1"></div> </div> To adjust the layout so that th ...

Alter the properties based on the size of the window

I am working with React-Spring and I need to adjust the offset of a component when the window size changes. I attempted the following solution, but it is not functioning correctly and requires me to refresh the browser each time. <ParallaxLayer ...

Error: Unable to return callback response from NodeJs function

I have a function that handles the process of reading a private key from the filesystem and generating a JWT token. The code successfully reads the file and creates a token, but it encounters an issue when attempting to callback to the calling function. De ...