Troubleshooting compatibility issues between jQuery scrollLeft and Angular

As I attempt to programmatically scroll a horizontally overflowing div, I am confident that my final code should resemble the following:

  var $element = $('#widgetRow');//or maybe $('#widgetRow').parent();
  //With 1 or more parent()'s

  var scrollLeft = $element.scrollLeft();
  $element.animate({'scrollLeft':scrollLeft + delta},10);

Despite my efforts to locate the right element and make it work, I found myself struggling. To test all parent elements for scrolling, I created this snippet:

  $('#someDeepWidget').parents().each(function(){
     $(this).animate({'scrollLeft':300},10);
  });

Although some elements scrolled, the one I needed did not respond. Surprisingly, the correct element does scroll horizontally with:

  $('#someDeepWidget').parents().each(function(){
    $(this).animate({'scrollTop':300},10);
  });

However, I remain puzzled as to which element is causing the reaction. Despite adding IDs throughout my HTML and targeting them, scrollLeft continues to fail on the necessary element.

Considering I am using Angular, I wonder if this could be influencing the situation in any way.

Answer №1

Neht (from www.lin.net.nz: Issue: jQuery ScrollLeft Chrome Hidden Div) brings up the idea that this problem can sometimes be resolved by:

  element.css({'overflow': 'auto'}).scrollLeft();
  element.css({'overflow': 'hidden'});

This made me think, could this approach work?

 $('#someDeepWidget').parents().each(function(){
   $(this).css({'overflow': 'auto'}).animate({'scrollLeft':300},10);
   $(this).css({'overflow': 'hidden'});
 });

And just like that, the content scrolled magically!

Armed with this knowledge, I navigated up the HTML elements in Chrome. Instead of making changes to my HTML document and testing the solution, I noticed Angular was adding an element to my page:

<div data-role="page" data-url="/" tabindex="0" class="ui-page ui-page-theme-a ui-page-active">

So, I included a CSS rule for this newly identified element:

[data-role="page"]{
  overflow:auto;
}

The final code now appears as follows:

app.directive("gphBubbleHorizontalScrolling", function($swipe) {
  'use strict';
  return {
    restrict: 'EA',
    link: function(scope, ele, attrs, ctrl) {
      $swipe.bind(ele, {
        'start': function(coords,event) {
          startX = coords.x;
          startY = coords.y;
        },
        'move': function(coords) {
          pointX = coords.x;
          pointY = coords.y;
          if(pointY < startY + 20 && pointY > startY - 20){
            var delta =  startX - pointX;
            $('[data-role="page"]').animate({'scrollLeft':scrollLeft + delta},10);
            var scrollLeft = $('[data-role="page"]').scrollLeft();
          }
        }
      });
    }
  }
}

Answer №2

When I needed to focus on a specific column two-thirds of the way across my grid upon loading my page, I opted for a simple solution that involved scrolling horizontally.

$(".ngViewport").scrollLeft(($("#datagrid").width())/4);
  1. I made use of ngViewport and scrollLeft functions from jQuery with minimal effort. The selector .ngViewport is designed for targeting any ng grid. By referencing #datagrid and manipulating its width value, I was able to achieve the desired result.
  2. By dividing by 4, I successfully positioned the column in the middle upon data grid load. This approach worked effectively due to the fixed number of columns present in my data.

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

Let's explore further - delving into JSON & array manipulation using the foreach loop in Pure JavaScript

Although I have some experience with Java Script, I still consider myself a beginner in certain areas, particularly when it comes to accessing JSON objects and arrays. I've tried various syntax and options for accessing arrays using [], but so far, I ...

How to place a scrollable content below a fixed flexbox navigation menu

I am struggling with a certain snippet (best viewed in full screen mode) where I need to position the <main> element directly underneath the <header> element. The <header> is set in a fixed position to remain at the top of the screen whil ...

Create a dynamic AppBar Toolbar in React Admin that changes based on the current page

Recently delving into the world of React, I've been facing some challenges when it comes to loading IconButtons onto the toolBar of an AppBar (from Material UI). For instance, I'm looking to have two specific IconButtons displayed on pageA and d ...

Problem with Ionic App crashing

Currently, I am developing an Ionic app that relies on local storage for offline data storage. The app consists of approximately 30 different templates and can accommodate any number of users. Local storage is primarily used to store three key pieces of i ...

Internet Explorer 8 comes with excess padding at the top and bottom, causing un

Why is IE 8 adding unnecessary padding to a div? I have checked the styling and nothing seems out of place. Can someone assist with this issue? Html The highlighted div in blue is the main concern. .block { clear: both; } .inline { float: lef ...

What is the best way to choose a specific section of a string using JQuery?

When utilizing the .html() function to retrieve html code, I encounter a need to extract a specific segment of the string. var newImgAdr = $(this).html(); The variable newImgAdr currently consists of: <img class="thumbnail-holder image-responsive cen ...

Tips for styling buttons in react-admin with custom CSS

I need some help with customizing buttons in react-admin. I am new to the platform and unsure about how to go about changing the button CSS for various actions such as create, edit, and export. Can anyone provide guidance on the best way to customize these ...

The plugin that simplifies adding and removing form fields in jQuery

I am in search of a quality plugin that can easily add or remove a set of form fields on a webpage. I came across this one , which seems to fit my requirements. However, I am exploring other options to see if there is an even better plugin available. Ther ...

Executing an AJAX request on Rails 3 without triggering a redirect by using a link/button

What is the most effective method for executing an ajax command using a link in Rails 3.1 with jQuery? Currently, I am attempting to use a link to trigger code from the "update_me" controller action. The line in views/products/new.html.erb looks like this ...

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

Am I on the right track in my understanding of how document and viewport relate to mouse position in JavaScript?

After reviewing responses from a previous inquiry, it is evident that both pertain to the x and y coordinates of mouse positions. In relation to the document and In relation to the viewport. I have delved into an article on QuirksMode, yet I feel ther ...

Adding properties with strings as identifiers to classes in TypeScript: A step-by-step guide

I am working with a list of string values that represent the identifiers of fields I need to add to a class. For instance: Consider the following string array: let stringArr = ['player1score', 'player2score', 'player3score' ...

What is the best way to implement a new search input for my datatable while also enabling the searchHighlight feature?

I'm attempting to implement a search bar for my datatables. I need to hide the default search engine that comes with datatables, so I added a script that I found in a forum. However, when I try to run it in my code, it doesn't work and displays a ...

CSS layout: The full-width header should align its left margin with the centered content-div

Can someone help me out with a solution to my problem? My page has a 1040px centered div for content, menu, and footer. I want the header image to align with the left margin of the content div and expand towards the right side for higher screen resolutio ...

How can an external style sheet be inserted into an iframe?

My website features an editor on one side and an iframe on the other. Currently, anytime HTML code is entered into the editor and a keyup event occurs, the iframe updates automatically. I am looking to add default styling to the code entered in the editor ...

I am interested in redirecting command line output to a file rather than displaying it in the standard

Is it possible to use child-process and spawn in node.js to execute a command and save the output to a file instead of displaying it on the standard output? test.js const expect = require('chai').expect; const { spawn } = require('child_pr ...

Assign a background image to a button using an element that is already present on the page

Is there a way to set the background-image of a button without using an image URL? I am hoping to use an element already in the DOM as the background-image to avoid fetching it again when the button is clicked. For example, caching a loading gif within the ...

Uploading images dynamically using Ajax and PHP upon submission

Currently, I am working on a modal that submits information and an image through a form to a database using PHP and JavaScript. However, my expertise in JavaScript is limited, so I could use some assistance. So far, I have successfully inserted data from ...

Unexpected Error: Unable to access the 'position' property of an undefined element (D3.JS)

This error occurred when I attempted to fetch JSON data and convert it into a line graph. An unexpected TypeError was thrown: Unable to access the 'position' property of undefined Below is the relevant portion of the JS code: d3.json("../js/ ...

Position a Paper component in the center of a div

I'm having trouble centering my <Paper> element inside a div. Can anyone help me with this? I have placed both the <Paper> and <RaisedButton> elements inside the div and applied the following CSS: .timerArea { text-align: center; ...