Interactive Sideways Navigation Bar

showcases a sleek horizontal scrolling menu on mobile devices. I aim to replicate this functionality for a website I'm redesigning that features extensive navigation elements.

Key Features:

  1. Left and right scroll button options
  2. Centered list item option within the available space
  3. Display only one list item at a time
  4. Horizontal Scrolling & Responsive design
  5. Clicking on the first or last list item navigates to the appropriate end of the list

This is the current HTML code for the section:

<nav id="sub" class="clearfix">
  <ul class="wrapper">
    <a href="#"><li>Estimate</li></a>
    <a href="#"><li>About</li></a>
    <a href="#"><li>Customer Information</li></a>
    <a href="#"><li>Financing</li></a>
    <a href="#"><li>Careers</li></a>
    <a href="#"><li>Locate Us</li></a>
    <a href="#"><li>Inspiration</li></a>
  </ul>
</nav>

Current CSS styles attached to the menu are as follows:

nav#sub {
  background: #004173;
  /* Additional CSS properties */
}

#sub ul {
  text-align: center;
  /* Additional CSS properties */
}

#sub ul li {
  padding: 10px 3.3%;
  /* Additional CSS properties */
}

#sub a {
  color: #fff;
  font-size: 10pt;
  font-weight: 400;
  text-decoration: none;
  /* Additional CSS properties */
}

#sub ul a:hover li {
  background: #007FEB;
}

Answer №1

After some time, I believe I have found exactly what you need:

Snippet: http://jsfiddle.net/abc123/4/

All the necessary CSS and HTML can be found in the snippet...

JavaScript Code:

$(function(){
    var counter = 0;
    var limit = 7;
    var screenWidth = $(window).width();
    $(window).resize(function(){
        winWidth = $(window).width();
        $('.box,.container_element').width(winWidth-100);
    }).trigger('resize');
    $('#left_arrow').click(function(){
        if (counter == 0) {
           counter = limit;
        } else {
           counter--;
        }
        $('.container_element').animate({scrollLeft:((winWidth-100)*counter)+'px'}, 800);
    });
    $('#right_arrow').click(function(){
        if (counter == maxState) {
           counter = 0;
        } else {
           state++;
        }
        $('.container_element').animate({scrollLeft:((winWidth-100)*counter)+'px'}, 800);
    });
});

jQuery is used once again for this implementation.

Answer №2

With the recent changes to the healthunit site, the original question may not be completely clear anymore.

If you want to create a horizontal scrolling navigation menu using arrow buttons instead of a scrollbar, you can achieve this with some jQuery and easily convert it to pure JavaScript.

var $bar = $('.nav');
var $container = $('#outer');
var widths = {};
var scrollOffset = 0;

var container = document.getElementById("outer");
var bar = document.getElementById("bar");

function setMetrics() {
    metrics = {
        bar: bar.scrollWidth||0,
        container: container.clientWidth||0,
        left: parseInt(bar.offsetLeft),
        getHidden() {
            return (this.bar+this.left)-this.container
        }
    }

    updateArrows();
}

function doSlide(direction){
    setMetrics();
    var pos = metrics.left;
    if (direction==="right") {
        amountToScroll = -(Math.abs(pos) + Math.min(metrics.getHidden(), metrics.container));
    }
    else {
        amountToScroll = Math.min(0, (metrics.container + pos));
    }
    $bar.css("left", amountToScroll);
    setTimeout(function(){
        setMetrics();
    },400)
}

function updateArrows() {
    if (metrics.getHidden() === 0) {
        $(".toggleRight").addClass("text-light");
    }
    else {
        $(".toggleRight").removeClass("text-light");
    }

    if (metrics.left === 0) {
        $(".toggleLeft").addClass("text-light");
    }
    else {
        $(".toggleLeft").removeClass("text-light");
    }
}

function adjust(){
    $bar.css("left", 0);
    setMetrics();
}

$(".toggleRight").click(function(){
    doSlide("right");
});

$(".toggleLeft").click(function(){
    doSlide("left");
});

$(window).on("resize",function(){
    // reset to left pos 0 on window resize
    adjust();
});

setMetrics();

Demo:

Answer №3

Here's a cool example using a fiddle: http://jsfiddle.net/zEPQ5/15/

While the design may not be flawless, it effectively showcases the concept at hand.

The implementation involved utilizing jQuery extensively.

$(function(){
    var count = 0;
    $('#up').click(function(){
        count += 1;
        $('ul.wrapper').animate({marginTop:(15-count*35)+'px'},400);
    });
    $('#down').click(function(){
        count -= 1;
        $('ul.wrapper').animate({marginTop:(15-count*35)+'px'},400);
    });
});

Answer №4

Here is a helpful jsfiddle link: http://jsfiddle.net/7vvdB/

The technique involves creating an outer container with a maximum width of 100% and horizontal scrolling capability. Inside this container, create another element with a fixed width that accommodates all the elements you want to display. Place all your content within this inner container.

.container_element
{ 
    white-space: nowrap;
    min-width: 100%;
    overflow-x: scroll;
    overflow-y: hidden;
}

.inner_container
{
    width: 5000px;
}

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

What is the best way to ensure that a component remains the highest layer on a react-leaflet map?

I am encountering an issue where the table on my page only shows for a brief second when I refresh the page, and then it disappears. The Map Component being used is a react-leaflet map. I would like to have a component always displayed on top of the map wi ...

The ng-model binding does not automatically update another ng-model within the same object

Check out this code snippet: http://plnkr.co/edit/aycnNVoD96UMbsC7rFmg?p=preview <div data-ng-app="" data-ng-init="names=['One']"> <input type="text" ng-model="names[0]"> <p>Using ng-repeat to loop:</p> <ul> ...

I encountered a console issue that I am struggling with. It is showing the error message "TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'"

When running this code, I encountered an error in the console (TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'). Can someone help me identify where I made a mistake and provide guidance ...

How to manage simultaneous requests in Parse Server Cloud Code

Imagine having some Cloud Code running on a Parse Server: Parse.Cloud.beforeSave("UserProfiles", function(request, response) { const query = new Parse.Query("UserProfiles"); query.equalTo("user", request.user); query.count({ success: f ...

Node.js is struggling to parse JSON data, resulting in undefined values appearing in the console

Issue with parsing JSON data in a node.js environment resulting in 'undefined' display in console. Below is the HTML code snippet: var jsonObjects = [{id:1, name:"amit"}]; jQuery.ajax({ url: 'http://localhost:8081', type: ...

Is Ursina the right choice for web development?

Looking for a method to compile Ursina for HTML5 using WebAssembly or another technology? I am seeking to make my Ursina Game compatible with Linux & Mac (and potentially Android/iOS with webview), but the current cross-platform compilation options for Py ...

ReactJS frontend is experiencing issues resolving a significant number of its node modules all of a sudden

After dedicating multiple weeks, on and off, to developing this application, I encountered a frustrating issue today. Everything had been running smoothly until now - I followed my usual setup process, installed all modules using npm install, and initializ ...

Try implementing jQuery to switch out an href link

Is there a way to replace a specific link using jQuery without impacting other links in the navigation menu? <ul class="navigation" <li class="level0 nav-1 level-top first parent"> <a href="mylink.com/whats-new.html" class="level- ...

Is it possible to initially design a login page using HTML/CSS and JavaScript, and then integrate VUE into it?

As part of a school project, I am tasked with developing a web-based application for a library system. The goal is to create a platform where librarians can login and manage books by adding, removing, or editing them. My responsibility lies in handling the ...

Attempted everything... Clicking away but a div refuses to show up post-click, resulting in an error message

When attempting to click a button, I encountered an error stating that the element is not clickable at point (1333, 75) as another element would receive the click. This issue arose while using Google Chrome version 65. <li class="slds-dropdown-trigge ...

Encountering an Uncaught TypeError: Attempting to call a controller function using AJAX in Magento results in undefined not being a function

I've developed a custom module in the admin panel called createadmincontroller. It's configured in the config.xml file. I'm trying to call the controller index function from a .phtml file using Ajax, but I keep getting an error that says "Un ...

Unlock the secrets of programming with the must-have guide to mastering variables

As a newcomer to the world of programming, I may be jumping ahead by asking this question too quickly. While reading a book, I came across something that confused me. Why is it necessary to create a variable for the password box element? Wouldn't the ...

Conditionals causing issue with React Button disabled property functionality

Having a DIV with two child elements, namely buttons that should be disabled under certain conditions. Despite correct conditions being applied, the buttons remain enabled which is causing confusion. The code snippet in question is as below : < di ...

Display a remote page on hover using Javascript, CSS, and HTML without providing any clickable link

How can I display a preview of another page within a div container without actually making the text clickable? I want the preview to show when the mouse hovers over specific text, but I need the original text to stay on the main page. I've experiment ...

Terminate the execution of the process.exec function

Currently, I have a function in my code that is responsible for executing a specific process. Here's how it looks: static async runTest() { await process.exec(`start ${currentDir}/forward.py`); } runTest(); Here's the thing – once this Python ...

The css property of *ngContainerOutlet is ineffective when applied to an ng-component with encapsulation

When I utilize *ngContainerOutlet to dynamically insert components, it wraps the component's template within an ng-component tag which causes my CSS styles to become ineffective. For example: <div class="my-class"> <ng-container *ngComp ...

Populate Jquery datatables programmatically

After implementing the Jquery Datatables plugin, I initially had hardcoded content in the table. However, I made some modifications to dynamically populate the table with fetched data. While this change worked fine, I encountered issues with the search f ...

`How can I activate caching for getServerSideProps in Next.js?`

We have implemented server-side rendering for a few pages and components. In an attempt to optimize performance, we have been experimenting with caching API responses. export async function getServerSideProps(context) { const res = await getRequest(API ...

What is the best way to integrate an AJAX callback into the stop condition of a `do while` loop?

Take a look at the code snippet below: let count = 0; do { $.ajax({ type: "POST", url: someurl, dataType: 'xml', data: xmlString, success: function(xml) { count++; } } while(co ...

Issues with CSS z-index in negative values not functioning correctly

Help! I'm encountering a perplexing z-index issue on my website. Check out the problem here: Upon closer inspection of each step's header, you'll see that only step 3 features a ribbon while steps 1 and 2 do not (if this isn't visible, ...