Scroll-triggered state change in Angular ui-router

Imagine a layout where each section is designed to be the size of the user's viewport or larger:

+----------+
| Section1 |
+----------+
| Section2 |
+----------+
| Section3 |
+----------+

All sections are visible within one view. As the user scrolls through the page, I want the browser URL to change dynamically based on which section is in view. For example, when the user first sees the page, the URL should be http://example.com/#/section1. Then as they scroll and reach Section 2, the URL should update to http://example.com/#/section2. Additionally, if a user copies a link like http://example.com/#/section2 and opens it later, the page should automatically scroll to that specific section.

To determine which section is currently in view, I am utilizing the angular-inview directive.

Currently, I am achieving this by using a parameter:

$state.go($state.current, {
    section: $scope.activeSectionId // e.g. 'section2'
}, {
    notify: false,
    location: 'replace'
});

However, this method has its drawbacks. The URLs don't look very friendly, and there is a bug where clicking a link with ui-sref causes the page to jump and then adjust the inview section, triggering the code above. This misnavigates on the initial click but works correctly after a page reload.

If anyone has suggestions on how to improve this functionality, I would greatly appreciate it!

Answer №1

Is there a reason you are considering changing the state when using $state.go($state.current)? Perhaps using anchorScroll would be a better option, especially with concerns about html5.

https://docs.angularjs.org/api/ng/service/$anchorScroll

UPDATE:

Apologies for missing the information regarding mouse scroll. Please refer to this helpful answer and the utilization of $location.hash(id);

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

Obtaining information from a intricate string input

{JSON.stringify(walletData.transactions, null, 2)} My goal is to extract backend data and display it as a table. The response has been converted into an array as shown below. [ { "date": "Nov 07, 2023", "description" ...

The functionality of Ionic State.go seems to be malfunctioning

MY UNIQUE LOGIN CONTROLLER app.controller('authCtrl', function ($scope, $state) { $scope.login = function () { $state.go('home'); } }); EXPLORING MY App.js $stateProvider.state('loginPage', { url: &apo ...

Explore the AngularJS ui-routing project by visiting the website: https://angular-ui.github.io/ui-router/#resources

I am currently working on creating a sample app using AngularJS ui-routing. There is a tutorial that I am following which can be found here When I try to run the site locally in Chrome, I am encountering some errors in the console. Below are the errors tha ...

Locate multiple entries using Mongoose

Just beginning to explore the world of technology and diving into Node and Express server with Mongoose. Below is the schema for a collection of documents that I'm working with. var empSchema = new mongoose.Schema({ _id: String, orgName: {type: Str ...

Implement Angular's ng-show directive within Laravel

I've recently discovered that I can utilize a Laravel variable in an html class and define it in an Angular file. Let me show you the code snippet to better explain: master.blade.php: <div ng-controller="AppCtrl"> <div class =" {{ statusC ...

JSON-formatted time-series data

When it comes to modeling over 1,000,000 data points in JSON, there are two potential paths to consider: a) Utilizing an array of objects: [{time:123456789,value:1432423},{time:123456790,value:1432424},....] or b) Implementing nested arrays [[123456 ...

Activate the textbox without utilizing `.focus()` method

I am encountering an issue with a small iframe on my page. The content within the iframe is larger than the window itself, requiring users to scroll around to view it in its entirety. Within this iframe, there is a button that, when clicked, triggers an an ...

How can you correctly make an Ajax request using computed properties sourced from VueX Store?

Is there a way to make an AJAX call where one of the parameters is a computed State in VueX? For instance, if I use this.$axios.get('someUrl/' + accID ), with accID being a computed property from VueX (using MapState), sometimes it returns ' ...

AngularJS UI.router directs users to a very unpleasant website address

Currently utilizing angular-ui-router 1.0.3 and attempting to set up the URL http://localhost:8888/details/2020?pending=true. However, upon entering the URL in the browser and hitting enter, the page automatically redirects to http://localhost:8888/app/det ...

Unusual Glitch in Bootstrap 3 Dropdown Menu

I am currently developing a website using Bootstrap 3. I am encountering an issue with the navbar dropdown. When I click on the link, it changes to show "expand child menu" and "collapse child menu". To clarify, here is the image I am referring to: Initi ...

Adjusting image dynamically based on conditions

I need to dynamically display images on my HTML based on specific conditions using TypeScript. In my TypeScript file: styleArray = ["Solitary", "Visual","Auditory","Logical","Physical","Social","Verbal",]; constructor(){ for (var i = 0; this.sty ...

Is it possible to create a carousel using a JQuery slideshow that connects the first and last list elements?

I am working on creating a carousel effect with three list elements that should slide in and out of the container when the left or right arrows are clicked. I want the list elements to wrap so that when the last element is reached, it goes back to the firs ...

The Stencil EventEmitter fails to send data to the Vue instance

Attempting to develop a custom component using Stencil with an input feature. The goal is to create a component with an input field that, upon value change, emits the input to the Vue instance and logs this event to the console (later updating the value in ...

Saving an array to a database in Concrete5

I have created a block where multiple names can be dynamically added. However, when I click save and return to edit the block, the newly added names are not visible. I suspect there is an issue with saving to the database. Can someone please assist me with ...

The .forEach() method in Javascript is not suitable for DOM nodes as they are subject to change during the iteration

Having an issue with moving all DOM elements from one node to another, I initially used this code: div.childNodes.forEach((n) => me.container.appendChild(n)); However, I noticed that only half of the nodes were being copied. It seems that the problem ...

Changing the shape of a background using CSS when hovering

My Bootstrap navigation has a unique setup, as shown below. I've observed that many users tend to only interact with the headings and ignore the submenus where the actual products are located. To address this issue, I want to change the design of th ...

Changing a CSS class dynamically upon updating a React.js component

Creating a React.js stateful component that showcases a counter with numbers updating every second. I am looking to incorporate a scroll effect where the new digit scrolls down from the top each time the counter increases. To visualize the project, check ...

When should you use isRequired for PropType versus defaultProps in a React application?

I often find myself confused about when to use .isRequired versus .defaultProps={...}. I personally feel that I should avoid using isRequired, as it seems like creating a potential bug in the application. By using isRequired, it automatically removes the n ...

Issues with compilation arise in Angular5 application due to CSS import errors stemming from a corrupt library

Currently working on an application using the latest Angular version. I recently imported a library for a component, but it has caused more issues than assistance. Despite that, I am still encountering compilation errors and reluctant to start over with a ...

Implementing a different background image for every menu item when hovering over its parent

I am trying to figure out how to achieve this. My menu is placed in a container that is 500px high, with a background image. I would like the background image to change when hovering over a parent menu item. I believed I could do this using something like: ...