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

The value of the jQuery data on click event handler becomes 'undefined' when used within an AJAX handler

When I click on the Positive or Negative buttons, a jQuery event handler function is triggered. Each button passes different data objects to the handler. However, within the AJAX POST handler, I am unable to access the data from the click event. $('# ...

The Chrome Extension XHR always gets a 403 response except when it is triggered from the file:/// protocol

My current project involves the development of a Chrome Extension that relies on the fixer.io API for accessing currency exchange rates. To test this extension, I typically use a simple HTML page where I embed my JavaScript using <script> tags. When ...

Efficient Ways to Utilize Global CSS in an Angular Project Without CLI

I am utilizing ASP.NET MVC as the server and Angular as the client application. Instead of a static index.html file, I have index.cshtml. The styles I am using are global styles rather than component-scoped. My query revolves around working with a bunch ...

CSS - Make the entire div clickable while leaving a specific area non-clickable

I have a block containing some information. Within this div is a hidden button labeled .remove. Clicking on the main block will take you to another page, while hovering over the div will reveal the button. However, clicking the button also navigates you aw ...

React Native: A guide to triggering a modal or action sheet when a button tab is clicked using Wix React Native navigation

Is it possible to trigger a modal or actionsheet by clicking on a specific bottom tab in a tab-based application using wix react native v2 navigation? These are the current packages and versions I am working with: react-native : "0.59.8" react : "16.8. ...

Every individual child component must be assigned a distinct key prop, even if they are pre-defined. - Utilizing REACT

My navigation bar routes are sourced from a JSON file structured like this: { "categorias": [ { "nombre": "Teacher absences", "componentes": [ { "type": "url", ...

Apply CSS styles to the checkbox

I am new to CSS and I need help styling a checkbox element. When the user selects one of the checkboxes, I want the background color to change to yellow. Thank you for any assistance you can provide! .radio_toggle { float:left; } .radio_toggle label ...

How can an array of file paths be transformed into a tree structure?

I am looking to transform a list of file and folder paths into a tree object structure (an array of objects where the children points to the array itself): type TreeItem<T> = { title: T key: T type: 'tree' | 'blob' childr ...

How can I make the arrows work on a secondary slider with EasySlider1.7?

I finally managed to get 2 sliders working on my page after reading through several other tutorials. However, I am facing an issue with the Prev & Next arrows on the second slider as they don't seem to work properly. I inherited this page from someon ...

CSS animation for image hover effect in Revolution Slider

I'm currently facing an issue while using Revolution Slider. To better illustrate, I am referring to the original demo found at this link: . When browsing through the 'portfolio' tab, you'll notice that the images shrink and darken upo ...

Utilizing Ajax for submitting data in a Spring application

I'm attempting to send a PUT request to the controller using AJAX. Here is my code: $().ready(function(){ $('#submit').click(function(){ var toUrl = '/users/' + $('#id').val() + '/profile'; ...

Looking for a Plugin that Can Responsively Display Images in a Vertical Layout - Does One Exist using Jquery

Looking for a gallery slider plugin that is responsive both horizontally and vertically. Have tried Flexslider1/2, Galleria, and others but they do not adjust to vertical resizing. Changing the CSS did not help. If you resize the browser horizontally with ...

How do I combine Firefox binary specification with adding the Firebug extension when using Selenium?

Presently I am utilizing the code below. var co = require('co'); var WebDriver = require('selenium-webdriver'); var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer; co(function *() { // async var ser ...

Issue with Angular controller failing to load when link is clicked

I am currently developing a webpage that incorporates Angular and responds to ajax requests by populating data into a table. When I manually enter the address, everything works as expected. However, if I navigate to the page using a link ( <a href="/pro ...

establishing irregular edges for the div container

Looking to create a class that can transform an element into a specific shape using CSS. Not entirely sure if this is achievable with pure CSS alone, but applying a class to the image element will morph it into the desired shape as shown below. <style ...

GTM - monitoring the most recent clicked element's input data

Custom Script function() { var inputs = document.getElementsByTagName("input"), selectedRadios = []; for (var i = 0;i < inputs.length;i++) { if(inputs[i].type==="checkbox" && inputs[i].checked) { selectedRadios.push(inputs[i].value); ...

Align Horizontal Fixed Element

<html> <head> <style> #rectangle { position: absolute; right: 0; bottom: 0; width: 150px; height: 200px; } </st ...

Tips for creating an effective dark mode toggle switch on a website

I was experimenting with creating a dark-mode switch for a website, but I'm not convinced that my implementation is the most efficient. Essentially, I ended up duplicating each file and adding a "2" at the end to create modified versions for each sect ...

What is the reason behind the delayed mutation of the state when invoking the setState method in React?

Currently diving into the Forms section within the documentation of ReactJS. Just implemented this code snippet to showcase the use of onChange (JSBIN). var React= require('react'); var ControlledForm= React.createClass({ getInitialState: f ...

Console output shows that the function results in undefined

When I pass a string parameter to a function, I expect the console to display "reff", but it is showing "undefined" instead. Here is the code snippet: var _ref; function foo(_ref='reff') { var bar = _ref.bar; return console.log(bar); } foo ...