Displaying a label in AngularJS and Ionic based on the current scroll position

I am facing a challenge with an ion-content scrollable item that contains anchors to jump to specific positions. In addition to the ion-content, there is also a button outside that allows users to jump to the next position. Here's an example:

Jump to Position2
**Position1**
Text
Text
Text
Text
**Position2**
Text
Text
Text

A quick solution was to add a listener for on-scroll completion:

<ion-content delegate-handle="scroll1" on-scroll-complete="setTextForScroll()">

By setting the label depending on the scroll position in the controller.

 var settingsScroller = $ionicScrollDelegate.$getByHandle('scroll1');
 var posXScroll = settingsScroller.getScrollPosition().top;
 if posXScroll === xy ...

However, this workaround may not be effective for different screen sizes or dynamically changing templates.

My idea now is to create an attribute directive that identifies marked elements and their positions in the scroll, then calculates the closest element to the current scroll position.

Despite my efforts, I am struggling to retrieve the DOM Element position in the controller and wonder if my approach is too complicated :/.

Any suggestions? Your assistance would be greatly appreciated! It seems like it should be simple... Scroll -> Label. Done :D

Cheers AucklandDB

Answer №1

To create a scrollable anchor in your document, insert the following code at the desired location:

<a name="scroll-anchor"></a>

You can then navigate to that specific position by using this link:

<a href="#scroll-anchor">Scroll to anchor!</a>

Answer №2

If you're looking to accomplish this in an ionic way, take a look at the documentation

Here's how you can do it in HTML:

<ion-content delegate-handle="myPageDelegate">
  <a ng-click="scrollTo('Contact')"></a>

  <div id="Contact">
  //content here
  </div>

Then, in your JavaScript file:

myApp.controller('MyCtrl', function ($scope,$ionicScrollDelegate,$location) {
  $scope.scrollTo = function(target){
    $location.hash(target);   //set the location hash
    var handle = $ionicScrollDelegate.$getByHandle('myPageDelegate');
    handle.anchorScroll(true);  // 'true' for animation
  };
});

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 route function is not being executed

I'm currently developing a straightforward restaurant web application that utilizes a mongo-db database to manage the menu items. The problem lies with my client js file, which is supposed to utilize a routing function to access the database and retri ...

Is there a way to divide my time during work hours using Javascript?

This is just a simple example. 9.00 - 18.00 I am looking to modify the schedule as follows: 9.00 - 10.00 10.00 - 11.00 11.00 - 12.00 12.00 - 13.00 13.00 - 14.00 14.00 - 15.00 15.00 - 16.00 16.00 - 17.00 17.00 - 18.00 The current code implementation see ...

Tips for displaying a slideshow within a div using JavaScript

When the HTML loads for the first time, the div slideshow remains hidden until a button is clicked. Any suggestions on how to display the first div without requiring a button click? var slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { s ...

Troubleshooting the Autocomplete Problem in Kendo UI AngularJS with ngMap

My current challenge involves attempting to combine the autocomplete feature of ngmap with Kendo UI. However, I am encountering an issue where the placeChanged event is not triggering and the autocomplete widget is not displaying. The input placeholder i ...

Issue with Sequelize failing to update a row within a database table

This is my first experience using sequelize. Within my database, I have a table named feed containing columns such as ID, caption, and url. In my controller, there is a straightforward function aimed at updating a row within this table: router.patch(' ...

Different Option for Ajax/Json Data Instead of Using Multiple Jquery Append Operations

I am working on a complex data-driven application that heavily relies on Ajax and Javascript. As the amount of data returned for certain value selections increases, the application is starting to struggle. This is more of a brainstorming session than a q ...

The powerful combination of AJAX and the onbeforeunload event

I am working with an aspx page that includes a javascript function window.onbeforeunload = confirmExit; function confirmExit() { return "You have tried to navigate away from this page. If you made changes without clicking Submit, they will be lost. Are yo ...

Encountering an error with [object%20Object] when utilizing ajaxFileUpload

I wrote a JavaSscript script that looks like this: $.ajaxFileUpload({ url: url, secureuri: false, fileElementId: ['upload-file'], dataType: "JSON", data:{ "sample_path":$(".demo-view-container-left .vie ...

Enhancing Forms with Redux and Styled Components

I'm currently working on developing a reusable component that involves using a redux-form <Field /> and styling it with styled-components within the component. The issue I'm facing is that none of the styles are being applied. Here is my ...

Header frame is not valid

I have been working on developing a real-time application using NodeJS as my server and Socket.IO to enable live updates. However, I encountered an error message that reads: WebSocket connection to 'wss://localhost:1234/socket.io/?EIO=3&transpo ...

Send an ArrayList to jQuery Flot

Good day. Apologies for any language barriers as English is not my first language. I am a novice in [see tags] and I have a web application that gathers a date and a serial number to execute a SQL query. The query returns some data (measurement values an ...

IE8 and IE9 encountering "Access is denied" error due to XML causing XDomainRequest (CORS) issue

Sorry if this seems repetitive, but I am unable to find a definitive answer to similar questions. Whenever I attempt to make a CORS request for XML, I consistently encounter an "Access is denied" JavaScript error in IE8. The code I am using is based on t ...

Tips for bringing in an npm package in JavaScript stimulus

Looking to utilize the imageToZ64() function within the zpl-image module. After installing it with: npm install zpl-image I attempted importing it using: import './../../../node_modules/zpl-image'; However, when trying to use the function like ...

What is the reason for this Javascript regular expression only successfully matching two out of three identical strings when filtering?

To better illustrate my question, I would like to reference the following link: http://codepen.io/anon/pen/mRxOEP Here are three seemingly identical inputs from customers: <span class="customer_country" id="SW1">, Switzerland</span> <span ...

Accessing JSON array values in a nested controller in AngularJS can be achieved by using

How can I access a Json array value in another controller? I am working with nested controllers where the nested controller should return the first character of the first name and last name. For example, if my first name is Anand Jee, it should return AJ. ...

Guide to converting a form into a structured object format (with a tree layout)

Looking to transform a form into an object structure? <form> <input type="text" name="Name" /> <input type="checkbox" name="Feature.Translate" /> <input type="checkbox" name="Feature.Share" /> <input type="submi ...

Can AR.js be used to implement extended tracking in Augmented Reality on the web?

Currently, I am working on a web-based project using THREE.js and AR.js for augmented reality. One issue I have encountered is that when the "marker" moves out of view of my camera, the augmented reality image either disappears or remains stuck on the edg ...

Three.js interpolation surface solution

Greetings everyone, I have a question regarding surfaces in Three.js. I have multiple Vec3 points and I want to create a surface that interpolates through them. While researching, I came across bezier curves (source: three.js bezier - but only as lines) a ...

Getting the ng-model value from a Directive's template and passing it to a different HTML file

When working with my name directive, I am unable to retrieve the value of ng-model from the template-url. team-two.html <form name="userForm" novalidate> <div name-directive></div> </form> <pre>{{userForm.firstname}}< ...

A more efficient method for refreshing Discord Message Embeds using a MessageComponentInteraction collector to streamline updates

Currently, I am working on developing a horse race command for my discord bot using TypeScript. The code is functioning properly; however, there is an issue with updating an embed that displays the race and the participants. To ensure the update works co ...