How to implement horizontal scrolling in an Ionic/AngularJS app

Struggling to implement a scroll navigation with ionic, but encountering issues with navigation and styling. Seeking guidance on how to achieve the desired outcome:

Desired navigation look: https://i.sstatic.net/vQ2CS.png

Current implementation showcases a horizontal scrollable list, but aiming for a navigation bar where the selected item moves to the center:

https://i.sstatic.net/9vP7y.png

When the first element is active, the left side of the list should remain empty and scroll like a navigation. Currently have a horizontal list, but struggling to center the active element during scrolling.

 <ion-scroll direction="x" class="wide-as-needed">
  <div ng-repeat="type in types" style='display: inline-block; margin: 5px;' >
    <a href="" id="anchor{{type}}" scroll-to="anchor{{type}}" class="button inline-button button-clear button-dark">{{type|uppercase}}</a>
  </div>
</ion-scroll>

The directive for anchor navigation aims to navigate to the element itself, but currently only makes it visible rather than centering it within the navigation list. More details can be found here: https://docs.angularjs.org/api/ng/service/$anchorScroll. It's worth noting that Angular $anchorScroll is primarily designed for vertical scrolling.

Consider utilizing the TabbedSlideBox plugin for tab navigation, although it doesn't natively support centering the tab when active.

Answer №1

Update: Just wanted to share this for future reference.

If you're looking for a solution, you could give a shot.

Personally, I found creating a custom directive from this to be more effective.

function center() {
    var currentElement = document.getElementById("active");
    currentElement.className = "center-menu";
    var nav = document.getElementById("nav");
    var navWidth = document.getElementById("nav2").offsetWidth;
    var margin = 0;
    for(var i =0; i<nav.children.length; i++){

        if(currentElement == nav.children[i]){
            console.log(nav.children[i]);
            break;
        }else {
            margin += nav.children[i].offsetWidth;
        }
    }
    nav.style.marginLeft = (navWidth/2 - margin - currentElement.offsetWidth);
}

CSS Styling:

nav {
  background: #9df0e8;
  overflow: hidden;
  border-bottom: 8px solid #40b5a2; 
  border-top: 2px solid #40b5a2;
  width: 80%;
  margin: 0 auto;
}

nav ul { margin: 0 0 2em;
margin-right:-999em;
white-space:nowrap; }

nav ul li { float: left; }

nav ul li a,
nav ul li span {
  display: block;
  background: #9df0e8;
  color: #345661;
  text-decoration: none;
  padding: 1em;
  cursor: pointer;
  -webkit-transition-duration: .3s;
  transition-duration: .3s;
}

nav ul li a:hover,
nav ul li span:hover { background: #40b5a2; }

.arrow{
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 10px 10px 0 10px;
    border-color: #9df0e8 transparent transparent transparent;
    display: none;
    position: absolute;
    left:0;
    right:0;
    margin-left:auto;
    margin-right:auto;
}
.center-menu .arrow{display: block;}

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

After the completion of the AJAX request, navigate to the bottom of the specified

I have come across a solution on how to scroll to the bottom of a div here. However, it seems that it is not working for me, the same goes for autofocus. I suspect the problem lies in the fact that I am making an AJAX call, and it is not functioning prope ...

jQuery Autocomplete without dropdown menu suggestion available

I'm working on a form for my webpage and I want to enhance user experience by adding autocomplete functionality. The goal is to suggest existing names as users type in the 'name' input text box. Although I can see in my console that it&apos ...

Exploring the possibilities of DOM events within nodeJS and express

Does anyone have experience catching a DOM event with NodeJS and Express? I've been looking into the "socket.io" module but am having trouble using it properly. My goal is to run some code whenever the content of a "file input" changes in the DOM. & ...

The onSubmit event handler seems to be malfunctioning within a Reactjs form

I recently started using React and encountered an issue with my form: class CustomForm extends React.Component { handleFormSubmit = (e) => { e.preventDefault(); const title = e.target.elements.title.value; const content = e ...

What are the ways to identify if a window has been refreshed or closed using JavaScript?

I've encountered this issue before, but I'm having trouble figuring it out. In my .aspx file, there is a function in the onunload body tag that refreshes the parent page. Now, I need to determine if the page has been refreshed or reloaded becau ...

Develop diverse canvases with shapes such as ovals, rectangles, and circles

My task involves creating a canvas in an oval shape. Below is the code I am using in jQuery with the created object of canvas. $("#decalshap").change(function() { alert("decal"); var shap = $(this).val(); if(shap=="oval") ...

Utilizing Stored Variables and Random Numbers in Selenium IDE

Can you explain how Selenium IDE handles stored variables (stored text) and random numbers? I've been trying to combine the two without much success. For example: <td>type<td> <td>css=input.some-text</td> <td>javascript ...

New data field is created with AngularFire2 update instead of updating existing field

I am facing an issue with updating a Firestore model in Angular 6. The model consists of a profile name and a list of hashtags. The "name" is stored as the value of a document field, while the "hashtags" are stored as keys in an object. However, every time ...

How can I enhance data from an AJAX callback in Mapbox with an additional layer?

With the code snippet below, I am aiming to utilize event data from an API to create a mapbox layer. This layer will be used to place markers and symbols on the map. However, I prefer not to use Mapbox markers as they cause issues with my code. I have suc ...

Combining jQuery methods and selectors within a loop

Can a sequence of selectors and methods be generated within a loop? For instance, assuming an array of elements is given: array[0] = '.type1value1, .type1value2, .type1value3'; array[1] = '.type2value1, .type2value2, .type2value3'; ar ...

Having trouble with Bootstrap 4 card image alignment within a tab panel

Encountering a challenge with nav-tabs nested within a card on a Bootstrap 4 webpage. My goal is to switch both the card text and the card-img-bottom simultaneously when toggling between nav-link options. To achieve this, I'm using a tab-pane that inc ...

Caution: flattenKids(): Two children with identical keys, `false`, have been detected in the ReactJS environment

Currently, I am working on creating a clickable list where each item redirects me to another page upon clicking. Below is the render method: render() { const quesItems = this.state.questions.map((question, i) => { return ( <li key={this.prop ...

Guide on triggering a Bootstrap dropdown using an external button click

I have scoured almost half of the Internet, but I can't seem to find a clear example on how to accomplish this task. There are plenty of "solutions" out there, but nothing has been successful so far. Here is what I have attempted: $('#openner&a ...

JavaScript - Issues with Cookie Setting

I created a function for setting cookies: function storeCookie(cname, cvalue, exdays){ var d = new Date(); d.setTime(d.getTime() + (1000*60*60*24*exdays)); var expires = "expires="+d.toGMTString(); document.cookie = cname + "=" + cvalue + ...

Angular.js: Ensure all services are loaded before initializing the application

I am currently developing an application using angular.js and I need to ensure that the result from a specific service is accessible throughout the entire application right from the beginning. How can this be accomplished? The service in question is as f ...

Is there a way to eliminate the underline in TextField components in Material-UI?

Is there a way to remove the underline from a material-ui TextField without using InputBase? I would prefer to stick with the TextField API, but have not been able to find a solution in the documentation. ...

Encountering a problem with Node.js because of a SyntaxError: Receiving an

Recently, I decided to dive into learning node.js and attempted something very basic: displaying a "Hello World" message from the server. The code snippet I used (taken directly from a book) is as follows: var http = require("http"); http.createServer(fu ...

Trouble with toggling class "collapsed" in Bootstrap v3 Navbar-toggle when using multiple data-targets

When using the data-target attribute with multiple CSS selectors, the behavior of toggling the "collapsed" class in navbar-toggle does not work as expected. If only one selector is used, the class is toggled properly, but when multiple selectors are specif ...

Adjust the position of a trimmed image within an HTML5 canvas

Is there a way to shift the cropped image down by 100 pixels and left by 50 pixels within the canvas? View the code snippet below. Javascript // Grab the Canvas and Drawing Context var canvas = document.getElementById('c'); var ctx = canvas.get ...

Steps to creating a proper cascade using the label statement

I am currently customizing the appearance of a checkbox. The HTML code in the file called fin1.cfm is as follows: <td>Master Event:</td> <td> <input type = "checkbox" id = "cb_e" name = "datesumm" value = "" > <label f ...