Creating a vertical slider with an unordered list

Looking to create a vertical menu with a fixed height that smoothly slides up and down when the arrow buttons are clicked.

I'm struggling to figure out how to properly use offsets to determine where to navigate to. Right now, I am using the relative offset from the next list item and navigating there, but it's not functioning as intended. It seems incorrect. I believe I need to check if the bottom of the unordered list is visible in the div. If so, the downward arrow should no longer move. But how can I check for this?

    //get position of next element
    var offsetNextLi = jQuery($nextElement).position();

    $liScrolledto = $nextElement;

    jQuery("ul#inline_navigation").animate({
        scrollTop: offsetNextLi.top
    }, 500);

Here is my fiddle:

http://jsfiddle.net/GxL8v/

Answer №1

Your example utilizes a constant li height, allowing for the use of a constant stepping technique or calculating the next invisible outer li element. I have created a JSFiddle to demonstrate this: http://jsfiddle.net/GxL8v/3/

(function($) {
$(document).ready(function () {
     //SLIDE UP
    jQuery("#arrowup").on("click", function () {
        var $ul = $('#inline_navigation');
        var y = $ul.scrollTop();
        var $firstChild = $ul.children().first();
        var step = $firstChild.outerHeight() + parseInt($firstChild.css('marginBottom'),10);

        console.log('before up', y, step);
        if (y >= 0) {
            y -= step;
            console.log('after up', y);

            $ul.stop().animate({
                scrollTop: y
            }, 100);
        }
    });

    //SLIDE DOWN
    $("#arrowdown").on("click", function () {
        var $ul = $('#inline_navigation');
        var y = $ul.scrollTop();
        var h = $ul.height();
        var $firstChild = $ul.children().first();
        var step = $firstChild.outerHeight() + parseInt($firstChild.css('marginBottom'),10);

        console.log('before down', y, step);
        if (h >= y) {
            y += step;
            console.log('after down', y);

            $ul.stop().animate({
                scrollTop: y
            }, 100);
        }
    });

});
}(jQuery));

This code is provided for educational purposes and is not optimized. It retrieves the "ul" list on every click, calculates the relative coordinate using jQuery's scrollTop() function, and considers whether the y value crosses any borders.

The approach for moving up and down is similar, with only the sign of the step changing from positive to negative. For greater reusability and optimization, consider implementing keypress functionality as well.

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

Pictures in Windows 7 have shifted to the left

After a recent migration to the Windows 7 operating system at my company, I encountered an issue. While using the same Visual Studio 2010 master file that was working fine on my Windows XP machine, I noticed that all the images below were now shifted to t ...

Error message: "Angular dependencies and provider not accessible"

I recently came across a file upload script on Github that I decided to implement. <script type="text/javascript" src="{% static 'bower_components/angular-file-upload/angular-file-upload.min.js' %}"></script> Furthermore, I have cre ...

Running JavaScript code without blocking the main thread

While studying JavaScript and JSON, I encountered some issues. I have a script that functions with JSON data, but the performance of my code is not optimal. The code only works when I debug it step by step using tools like Firebug which leads me to conclud ...

Perform a function within another function in Vue

I am dealing with two nested functions in Vue. The parent function needs to retrieve the value of an attribute, while the child function is responsible for using this attribute value to make an API call. How can I ensure that both parts are executed simult ...

Is it possible to specifically focus on Google Chrome?

Can anyone help me with positioning the update button on www.euroworker.no/order? The button works fine in Firefox and Internet Explorer, but it's not displaying correctly in Chrome or Safari. I've tried targeting Safari and Chrome specifically, ...

Obtain the current user's Windows username without relying on the ActiveX object

Is there a way to retrieve a client's Windows username in ASP.NET when hosted on a remote server without using an ActiveX object? I tried the following code: Response.Write("HttpContext.Current.Request.LogonUserIdentity.Name " & HttpContext.Cur ...

Implement generics within the `setState` function in a React.js and TypeScript application to ensure that it returns a string value

I'm facing an issue with setting the state in the onChange event by specifying the type of the setState hook. Here is my current setState declaration: const [payer, setPayer] = useState<Number>(0); And this is the radio setter function I am usi ...

Clicking on a link with JQuery does not fire the action when the href attribute is set to "#open-site"

My navigation setup is as follows: <ul> <li><a href="index.html#open-site">Test</a></li> <li><a href="#open-site">Test</a></li> <li><a href="test.html#open-site"> ...

Struggling with loading external scripts within background.js in Chrome extensions?

I am facing an issue with my chrome extension. I am unable to call an external script, specifically the ethereum script (web3.min.js), from within my background.js file. The error message I receive is: Uncaught EvalError: Refused to evaluate a string ...

Is Proxy.apply() not functioning correctly on Node.js? I'm unsure if this is a bug or if I am implementing it incorrectly

Utilizing a Proxy object has been quite helpful for me. The getter and setter functions are working perfectly as expected. However, I have encountered an issue where the apply method is never invoked. var p = new Proxy({}, { /* getter */ get(t ...

Tips for defining types for specific CSS properties in TypeScript, such as variables

Perhaps there are already solutions out there, and I appreciate it if you can share a link to an existing thread. Nevertheless... In React, when I use the style prop, I receive good autocompletion and validation features like this example: https://i.sstat ...

How can state be shared between sibling components in React?

As someone who is relatively new to React, I am seeking guidance on the proper method of passing states between components. While I did come across a similar inquiry on Stack Overflow, I would appreciate if someone could provide a specific solution for my ...

Click on the input to add or remove a value

I have written a code where, on click, I insert an email address into a field. However, what I am trying to achieve is that upon the next click on the same field, it will remove the email if one already exists in the input. Below is my current code snippe ...

"Maximizing Bootstrap Functionality with Angular 2: Step-by

I'm experiencing an issue with Angular2, Bootstrap 3.3.7, and Bootstrap Plugins. I have imported all the necessary stylesheets and JavaScript files in the 'index.html' file. Below is the snippet from the index.html: <!-- Bootstrap & ...

height issue with a div

I currently have a header, master container, and footer set up on my website. Within the master container, I have 2 separate containers: A on the left and B on the right. Even as the B container gets filled up, the A container remains static. Is there a ...

When defining the index of arrays using a loop, the function will return NaN

I encountered a problem with my project where this function is returning NaN. In the past, I was able to resolve a similar issue by setting a variable equal to 0. However, that solution is not working in this case. var distanceArray = [ [0, 62.3, 58.8 ...

Invoking a JavaScript function using jQuery's AJAX

I'm currently working with jQuery and Ajax. Within my MainFile, I have the following code: <html> <head> <script src="Myscript.js"> </script> <script type="text/javascript"> $(doc ...

The Enigmatic Essence of TypeScript

I recently conducted a test using the TypeScript code below. When I ran console.log(this.userList);, the output remained the same both times. Is there something incorrect in my code? import { Component } from '@angular/core'; @Component({ sel ...

Improving the layout and size of text within input fields

How can we adjust the input text positioning to the right so it's not directly against the edge? Is there a way to move 'searching' 5px to the right? ...

The padding of the iFrame does not match the padding of the source

After creating a compact javascript Rich Text Editor, I noticed a discrepancy in the padding of certain elements when I view it within an iframe. This issue is apparent in the image directly from the source: However, when I embed it in an iframe using the ...