Configuring a fixed menu in one direction using jQuery

Currently, I have a left-sided "fixed" menu bar on my website that should follow the user's scroll down the page. In my attempt to achieve this functionality, I have experimented with two different methods: one utilizing CSS and the other using jQuery.

1) Initially, I tried fixing the position using CSS styling:

#leftframe {
    position: fixed;
    width: 200px;
}   

This approach worked well for vertical scrolling; however, it posed issues when the window was narrow as it overlapped with the page content while scrolling.

2) Upon further research, I discovered a jQuery technique on Stack Overflow that allowed element fixation along one axis, as explained in this post: CSS: fixed position on x-axis but not y?

Following the guidance provided, I formulated a function to implement this behavior:

function float_vertical_scroll(id) {
    $(window).scroll(function(){
        $(id).css({
            'top': $(this).scrollTop() //Save for later use
       });
   });
}

Unfortunately, testing this method on Chrome and Safari revealed an issue where the menu bar appeared choppy and flickered during vertical scrolling. Unlike the smooth sliding motion achieved through the CSS solution, the jQuery implementation did not provide the desired effect.

If anyone has insights into why the jQuery method is choppy or suggestions for improvement, I would greatly appreciate your input. Is there perhaps a third alternative that combines the benefits of both approaches?

Thank you all for your help.

Answer №1

I have come up with a unique solution to the problem at hand and wanted to share it here for others who may be facing a similar issue.

Previously, I struggled with creating a vertically fixed left menu without it being fixed horizontally as well. CSS limitations made it impossible to achieve this without causing issues such as unwanted horizontal scrollbars. Other solutions involving javascript resulted in choppy movements when scrolling vertically.

After analyzing the typical user behavior of vertical scrolling versus horizontal scrolling on websites, I devised a new approach. By setting the menu frame to a fixed position and adjusting offsets during horizontal scrolling, I managed to eliminate unwanted horizontal fixation. Although horizontal scrolling might not be perfectly smooth, it is rarely used enough to cause inconvenience:

function float_horizontal_scroll(id) {
    jQuery(window).scroll(function(){
        jQuery(id).css({
            'left': 0 - jQuery(this).scrollLeft()
        });
    });
}

#leftframe {
 position:fixed;
 width: 200;
}   

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

Selector matching a descendant element in React styling

I am facing a challenge with using the direct descendant CSS selector within a React inline style element: <style>{` .something>div{ color: blue; } `}</style> While this works fine in development, in production React replaces > ...

Setting the default date for an inline datepicker in jQuery UI

Utilizing jQuery UI for an inline datepicker has been quite beneficial. When applying it to a <div>, a sleek inline datepicker is generated effortlessly, though configuring the default date selection remains unclear. JS: $("div.date").datepicker({} ...

The datepicker is refusing to update the date format

I've been attempting to adjust the date format for my datepicker, but it refuses to change. Below is the code I'm using: $(document).ready(function() { $('#dateselect').datepicker({ format: 'dd/mm/yyyy', o ...

Create a stylish layout with two div elements positioned side by side using flexbox for a responsive design

Struggling with my portfolio website, I encountered a challenge. I attempted to place two divs side by side, each containing text and an image. While the initial setup was simple, trying to make it responsive using flexbox has proven to be quite frustratin ...

Positioning a fixed div can result in the page being cut off on certain mobile browsers that are not

While there are numerous similar issues out there, the bug I am encountering feels unique. This issue involves a page with a div in position:fixed style, specifically affecting certain less common mobile browsers like Star Safari, DU Browser, and Tint Brow ...

Finding the nearest span element with a selector in styled components

Currently experimenting with integrating Styled Components to target the nearest span element. <label> <span className="">Password</span> <input type="password" id="passwordInput" /> </label> ...

Load dynamic content using ajax beforehand

When navigating on my website, users click on various links such as contact or about, and the content is loaded using ajax. The code snippet below reveals how this functionality is achieved: var AjaxContent = function(){ var container_div = '&apos ...

Encountered an Error: Trying to use a function that is undefined - While utilizing Jquery Tabs

Working on implementing Jquery Tabs using the "description" and "reviews" li tags as tabs. Testing it out here . Everything seems to be functioning correctly here Key Points: This is Wordpress Multi-Site setup. The issue occurs in certain folders or "si ...

Why do all the select boxes require two clicks to open the drop-down menu when the function is activated?

After implementing this code on my webpage, I noticed an issue where all select boxes required two clicks from the user to open the drop-down menu. The first click seemed to set focus on the box, and only on the second click would the drop-down actually ap ...

When I set the width of my script to 100% in CSS, it causes the width to become distorted

I encountered an issue with my Div Slider that swaps out Divs on click. When I modified the CSS to include the following: .hslide-item {width: 100%}; The script started ignoring the entire div width. I am looking for a solution where the .hslide-item ca ...

Shorten a string once a particular word is found in either Smarty or JavaScript/jQuery

I'm currently working on a website and encountering a minor bug related to the addition of content at the end of some strings. For instance: style="background-image:url({$sub.image});" displays style="background-image:url(http://blablalba.fr/phoenix ...

The rapid execution of multiple API calls causes the subsequent script to start before the initial script has completed

I am facing an issue with a script that is triggered by an API. The problem lies in the fact that the script is being invoked too frequently within a short span, sometimes as little as 1/1000 of a second. Within this script, there are SQL queries that perf ...

Is there a way to determine if a key is being pressed while the click event is happening using jQuery?

Is it possible in jQuery to detect a click event and determine if a key was pressed simultaneously so that different actions can be taken within the callback function based on the keypress event? For instance: $("button").click(function() { if([KEYPR ...

Using an overlay with figure-img in Bootstrap 4 is a visually appealing design

I need assistance with adding an overlay to only the bootstrap 4 figure-img. In order to achieve this, I inserted a div with the class overlay beneath the figure-img. Below is the code snippet: <div class="col-md-4"> <figure class="service tex ...

How can I transfer a variable between two scripts on the same webpage?

Everyone, I've hit a roadblock with this issue and I can't seem to pinpoint what the mistake might be. I have a PHP file containing multiple scripts, including both PHP and jQuery sections. My aim is to transfer a PHP variable from the HTML Head ...

Whenever trying to access data from the database using DataTable row(), it consistently remains undefined and fails to retrieve any information

While working on my application, I attempted to display all the data in a DataTable from the database. Unfortunately, instead of achieving this, I encountered an error from the server. Upon receiving the error, the dataTable object was displayed as follows ...

Is the gridviewscroll script missing when performing an asynchronous postback on the page?

After making an asynchronous postback, the page loses the gridviewscroll script even though I registered the script on the code behind. This issue occurs when clicking "add new row" in an ASP.NET grid view that is inside an update panel. I have experiment ...

External JavaScript file not executing defined function

My homepage includes a register form that should open when the register button is clicked. However, I am encountering an issue where the function responsible for opening the form from another JavaScript file is not being triggered. Here is the HTML code: ...

Revamp Password Security with PHP, Ajax, and JQuery

Working on creating a form to update a user's password. No errors in console but the data isn't updating. The validation part is functioning, so the issue might lie with the Ajax implementation? Note: I am aware of not using mysql in PHP long-te ...

Stop all file uploads using jQuery

I have integrated the jQuery File Upload plugin () into my website for image uploads. Here is my code snippet: $('#fileupload').fileupload({ url: 'server/index.php', dataType: 'json', dropZone: $('#dropzone&a ...