Issues with Navigating through a Scrollable Menu

I'm having a difficult time grasping the concept and implementing a functional scrolling mechanism.

Objective: Develop a large image viewer/gallery where users can navigate through images by clicking arrow keys or thumbnails in a menu. The gallery and menu should be able to track which thumbnail the user is currently viewing.

Situation: I have a #menu with position:absolute that becomes visible when hovering over the right edge of the window. Within this #menu, there is a child element called #galleryMenu with overflow:scroll applied. This setup allows for scroll navigation within the menu using the mouse wheel.

Issue: Whenever the mouse moves away from the #menu, the scroll position resets. I am struggling to find a way to retain the user's previous scroll position within the #menu.

Sample Website Link:

I have also experimented with plugins, but they interfere with the functionality of #galleryMenu's overflow:scroll property, ultimately disrupting the #menu layout.

Any assistance would be greatly appreciated.

Below is a refined version of the code to better explain my goal and problem.

HTML

<div id="resolution">
    <div id="main">
        <img />
    </div>
    <div id="menu">
        <div id="galleryMenu">
            <ul id="galleryThumbnail">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
             </ul>
         </div>
     </div>
</div>

CSS

#resolution{
    position: absolute;
    margin:0;
    padding:0;
    width: 100%;
    height: 100%;
}

#main{
    position: absolute;
    width: 100%;
    height: 100%;
}

#menu{
    position: absolute;
    display: block;
    cursor: pointer;
    right: 0;
    width: 10px;
}

#galleryMenu{
    position: absolute;
    display: none;
    width: 350px;
    right: 0;
    overflow: scroll;
}

#galleryThumbnail{
    list-style-type: none;
    padding: 20px 0 20px 0;
    margin: 0;
}
#galleryThumbnail li{
    padding: 0;
    margin: 0;
}

JQUERY

//Show and Hide Gallery Menu
$("#menu").hover(function(){
    $("#galleryMenu").show('slide', {direction: 'right'}, 500);} , function(){
    $("#galleryMenu").hide('slide', {direction: 'right'}, 500);}
);

//Set Window Height as Menu Heights
$("#menu").height($(window).height());
$("#galleryMenu").height($(window).height());

// Scroll to Current Thumbnail on Menu
$("#galleryMenu").animate({ 
    scrollTop: $(document).height()-$(window).height()}, 
    1400,
    "easeOutQuint"
);

Answer №1

Make sure to follow these steps.

let previousScrollTop = 0;
$(document).on('scroll', '#galleryMenu', function(){
    previousScrollTop = $('#galleryMenu').scrollTop();
});
$(document).on('hover', '#galleryMenu', function(){
   $('#galleryMenu').scrollTop(previousScrollTop);
});

I trust this information will be beneficial.

Answer №2

I discovered a clever solution. Instead of using Jquery's slideUp() and slideDown(), which caused issues with the #menu location resetting, I opted for .animate() to smoothly move #menu off the screen to the right. This approach made it simpler to keep track of the scroll position on #menu when users temporarily hovered away.

Although Khaleel's code didn't quite do the trick for me, I eventually came up with a functioning piece of code:

//Menu : Scroll to Thumbnail
$("#galleryMenu").animate(
   {scrollTop: $("li:nth-child(<?php echo $currentImage ?>)").offset().top-()},
    500
);

By utilizing the UL's LI elements along with some PHP magic, I successfully pinpointed the nth-child of the li element that housed the thumbnail image currently being viewed, allowing me to center it onto #menu.

I appreciate all your contributions!

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

When the state changes, initiate the animation

Currently, I am working with Vue.js and need to animate a navigation menu. My goal is to display two li elements when a user hovers over one of the navigation buttons. At the moment, I have set the data type showActivities to false by default and changed ...

What impact does Bootstrap .ratio have in making the img show up over the offcanvas menu?

Is there a way to prevent a ratioed image from appearing above the offcanvas menu? When using Bootstrap 5 and specifying a 16x9 ratio for the image... <div class="ratio ratio-16x9 my-3"> <img width="768" height=&q ...

Adding data to a subdocument array with Mongoose's $push method

Here is the model that I am working with: var Customer = mongoose.model('Customer', { firstname : String, lastname : String, phone : String, street : String, city : String, state : String, zip : String, fixed : Bo ...

The error encountered is: `Exception: startDate.getTime is not a valid function

const [currentDate, setCurrentDate] = useState(new Date()); const fetchDataFromAPI = () => { let timeStamp = Number(currentDate.getTime()); axios.get( `https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=BTC,USD,EUR& ...

Error encountered during Angular unit testing: Unable to read the 'id' property of a null value. (Jasmine, Karma)

I am currently working on writing unit tests for a specific component in my Angular application. The component uses a currentUser variable both in the component logic and the HTML template. I have hardcoded this variable by mocking it in every test using c ...

Sending a variable to a function in JavaScript (winJs) from a different function

Greetings! Currently, I am developing a Windows 8 app using Java Script. function fetchFromLiveProvider(currentList, globalList,value) { feedburnerUrl = currentList.url, feedUrl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&outpu ...

Setting a button next to an input field in Bootstrap 4: A step-by-step guide

I have a form group with a label and input field. My goal is to position the button next to the input field on the same line. Here's an example of my code: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstra ...

Ways to customize a directive to show conditionally with no need for container elements

I have created a basic role-based security directive in angularjs. It's my first attempt at making a directive. My goal is to replace the following HTML: <authorize if-granted="GET_POSTS"> Hello WORLD!!!! {{name}} </authorize> with j ...

Issue encountered when trying to import @tensorflow/tfjs-node in conjunction with the face-api.js library within a node.js environment

Using the @tensorflow/tfjs-node package along with face-api.js to enhance performance, here is my code snippet: // Import nodejs bindings to native tensorflow, // Not required but will significantly speed up operations (Python required) require('@tens ...

Combining video.js and vue.js for seamless integration

Can anyone guide me on how to display a youtube video using a video.js player within vue.js? I'm new to integrations and not sure what it entails. Any assistance will be greatly appreciated. ...

Adjust the size of the sliding tool with images of varying dimensions

My mobile-first slider features three different types of images: tall, horizontally long, and square. I want the size of the slider to be determined by the horizontally long image and then scale and center the other images to fit its size. To achieve this, ...

What is the best method for storing dynamic values and attribute labels in a state within a React.js application?

I am currently working with react js. On my single product page, I have an array of objects called attributes that I need to display in the user interface. Here is a preview of how it looks: https://i.sstatic.net/GttrD.png My goal is to retrieve and stor ...

When attempting to send an archiver file in NodeJS, the request may become unresponsive

In my NextJS application, I am facing the challenge of generating a large number of QR codes at once, like 300, 400, or even 500, and then packaging them into a zip file for users to download. The following code snippet demonstrates how I achieve this usin ...

Tips for remaining aligned on the same page after logging in

I have a project where I am creating an event registration system that shows the event registration list without refreshing the page using Ajax if the user is logged in. However, I encountered an issue when trying to login as I received an undefined index ...

What is the method for deactivating a hyperlink with CSS?

Within my wordpress page, there is a specific link present. This particular link belongs to a class. Is it viable to deactivate this link solely through the use of CSS? <a class="select-slot__serviceStaffOrLocationButton___5GUjl"><i class="mater ...

What are some creative ways to design drop-down menus within an email?

The snippet of code I am currently using lacks the ability to format text, and it does not allow content below dropdown items to shift down properly in an email. <select> <option value="first">First-time users of the Vanguard Events app:&l ...

What is the best way to retrieve the canonicalized minimum and maximum values within the beforeShow method of jQuery UI's datepicker?

I have a pair of datepicker elements that I want to function as a range. When a value is set in the "low" datepicker, it should adjust the minDate and yearRange in the other datepicker, and vice versa with the "high" datepicker. The challenge I'm faci ...

The MUI Time picker fails to display the correct time value in the input field with proper formatting

My primary goal is to implement a MUI time picker with yup validation and react-hook-form. However, I am encountering an issue where the selected time from the time picker is not being updated in the text field as expected. https://i.sstatic.net/LVkgK.png ...

how to toggle visibility of bootstrap accordion panel using jQuery

I have a bootstrap accordion that I want to customize. Specifically, I only want to enable a panel under specific conditions. The idea is for the second panel to be collapsible only when the first panel is valid. <div class="panel-group" id="accordion" ...

Processing hover attributes in Tailwind-styled-components

I'm currently working on a website that features a dark mode, and I want to utilize the dark prop in tailwind-styled-components. The props work fine in all instances except for actions like hover, active, focus, etc. When attempting to use hover and t ...