Navigating within two containers using the anchorScroll feature in AngularJS

I am trying to create a page with two columns of fixed height. The content in each column is generated using ng-repeat directive.

Is it possible to implement scrolling within each column to a specific id using AngularJS?

Code

<div>
  Scroll to a position
  Column #: <input style="width: 20px;">  
  Item #: <input style="width: 20px;">
</div>

<div class='column'>Column one
  <div id="col-1-{{$index}}" class='item' ng-repeat='item in itemsOne track by $index'>
    {{$index}} ..... {{item}}
  </div> 
</div>
<div class='column'>Column two
  <div id="col-2-{{$index}}" class='item' ng-repeat='item in itemsTwo track by $index'>
    {{$index}} ..... {{item}}
  </div>
</div>

JS:

app.controller( 'myCtrl', [ '$scope', function ( $scope ){

  $scope.value = 'test';
  $scope.itemsOne = [];
  $scope.itemsTwo = [];

  for(var i=0; i<10; i++){
    $scope.itemsOne.push(generateText());
    $scope.itemsTwo.push(generateText());
  }

  function generateText() {
    var text = ""; 
    var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < Math.random() * 200; i++ ){
      for( var i=0; i < Math.random() * characters.length; i++ )
          text += characters.charAt(Math.floor(Math.random() * characters.length));
      text += " ";
    }
    return text;
  }

  $scope.scrollMeTo = function(column, row){
    // Implementation pending
  };

}] );

http://plnkr.co/edit/WYtntRagJdQoK7k6fAPc?p=preview

Answer №1

I've updated your plunkr to ensure functionality, although there is still plenty of room for enhancements. It's important to remember that when dealing with DOM manipulation, it's best to use directives, like the one I created:

app.directive('autoScrollTo', function () {
  return function(scope, element, attrs) {
    scope.$watch(attrs.autoScrollTo, function(value) {
      if (value) {
        var pos = $("#" +attrs.prefixId +value, $(element)).position().top + $(element).scrollTop() - $(element).position().top;
        $(element).animate({
            scrollTop : pos
        }, 1000);
      }
    });
  }
});

Check out the updated version here: http://plnkr.co/edit/2gb8ZdZ5DPanRBVQvTwa?p=preview

I hope this information proves useful.

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

Encountered an unexpected comma token while attempting to map an array in ReactJS

Can't figure out why I'm getting an error when trying to map an array in React with the following code: const { loading, error, posts } = this.props; return( {posts.map(onePost => ({ <p key={onePost.id}>{onePost.title}&l ...

What is the best way to integrate a new unique identifier into an existing MongoDB document using NodeJS?

I am looking to add up a field in a document when I input a new entry that has a replicated unique id. This is the code I have so far: MongoClient.connect(process.env.MONGODB_URI || process.env.DB_CONNECTION, { useUnifiedTopology: true, useNewUrlParser ...

What is causing my React-Testing Library queries to not work at all?

In my current project, I am using Jest along with Testing-Library to create UI unit tests. One issue that I encountered was that the components were not rendering on the DOM. After some investigation, I found that the main culprit was a component called & ...

Guide to turning off the Facebook iframe page plugin

I am facing a challenge with embedding a facebook iframe on my website. I want to disable it once the screen width reaches a specific point, but I am struggling to make the iframe disappear properly. Here is how I have attempted to implement this: window.o ...

Is there a way to utilize JavaScript in order to trigger a CSS animation to occur at a designated time during a video

I have a cool animated image element that I want to play at a specific point in time during a video using JavaScript. I'm not sure how to make it happen, but I know the .currentTime property could be the key. My goal is for the animation to only play ...

Angular JS enabling multiple subscriptions to trigger multiple events simultaneously

Currently, I am developing an application and facing a challenge while trying to integrate AngularJS with a third-party JavaScript library. To establish communication between the two, I have implemented the pubsub method using Mediator JS. The issue arise ...

Why is only the peak of the wave visible? I am eager to showcase the full extent of its beauty

Having an issue with the appearance of a superposed wave using threejs. When displayed, the wave made from plane material only shows the upper half, but when turned upside down using mouse dragging, it appears correctly. // Turn the wave plane upside down ...

Optimal Approach for Managing ASP.NET Ajax Modal with MouseOver - Data Retrieval Only Upon Request

I'm interested in developing a modal popup that dynamically fetches the data inside it upon mouseover, instead of preloading all the data beforehand. Are there any scripts or frameworks available that would simplify this task? ...

Streamlined approach to triggering ng-change on a single textbox

Consider this array within an angular controller: somelist = [ { name: 'John', dirty: false }, { name: 'Max', dirty: false }, { name: 'Betty', dirty: false } ]; To iterat ...

I am a beginner in the world of MEAN stack development. Recently, I attempted to save the data from my form submissions to a MongoDB database, but unfortunately, I have encountered

const express = require('express'); const bodyParser = require('body-parser'); const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); const Schema = new mongoose.Schema({ username: St ...

Customize dynamically loaded data via AJAX

I have a webpage that is loading a table from another source. The CSS is working fine, but I am facing an issue editing the table using jQuery when it's loaded dynamically through a script. It seems like my changes are not getting applied because they ...

Iterating through two classes in a Javascript loop

Hello, I'm facing a small obstacle that I'm hoping to overcome using JavaScript/jquery. Essentially, I have multiple div classes and I want to create a loop that adds a class to specific divs without manually assigning an id to each one. The goal ...

Changing the background of a Muitextfield input element conceals the label

Struggling to customize the Textfield from the global theme? Can't seem to achieve a colored background for the input only (white) without obscuring the label when it moves inside the input. Desired result : Current outcome : Tried using white back ...

Issue with timestamp in the Instagram API call to retrieve media using AJAX (GET /media/search)

My API call is returning a 400 bad request error message: {"meta":{"error_type":"APIInvalidParametersError","code":400,"error_message":"invalid parameters-check the max\/min-timestamps, if you supplied them"}} Even though my request appears to be co ...

What could be the reason for the malfunction of this AngularJS data binding feature?

I am trying to create an angularjs filter that outputs HTML, similar to what is discussed in the link, but I am encountering issues. In my HTML code, I have: <ul> <li ng-repeat="book in books | filter:query"> {{book.title}} ...

Regex tips: Matching multiple words in regex

I am struggling with creating a simple regex. My challenge is to write a regex that ensures a string contains all 3 specific words, instead of just any one of them: /advancebrain|com_ixxocart|p\=completed/ I need the regex to match only if all thre ...

What techniques can be used to maintain the value of 'this' when utilizing async.apply?

Employing async.parallel to simultaneously run 2 functions, initiated from a static function within a mongoose model. In this code snippet (where the model contains a static function named verifyParent), I utilize this to access the model and its functions ...

Ingesting RSS feed into an Express server

I've been searching for hours, but I just can't seem to find a solution. I was able to figure things out when working on the client side, but now that I'm trying to load posts on the server and render them in the view, I'm hitting a roa ...

Error: Unable to access the 'login' property of an undefined object

An error occurred: Uncaught TypeError: Cannot read property 'login' of undefined........... import Login from '../components/Login.jsx'; import { useDeps, composeWithTracker, composeAll } from 'mantra-core'; export const com ...

Issues with making cross-domain requests using jQuery and PHP

I have stumbled upon a similar question that has been asked before, but unfortunately, the answer provided did not give me enough guidance to identify where my code is incorrect. I apologize if this question resembles a previously existing one; I have spen ...