- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find left position of element in horizontal scroll container using jQuery?
To find the left position of element in horizontal scroll container using jQuery, use the animate() function with the scrollLeft() function.
Example
You can try to run the following code to learn how to find left position of an element in horizontal scroll container:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { var scrollArea = $('#box'); var toScroll = $('#box .myclass'); function myScroll() { toScroll.each(function() { var self = $(this); $(this).css('cursor', 'pointer'); $(this).on('click', function () { var leftOffset = self.offset().left - scrollArea.offset().left + scrollArea.scrollLeft(); scrollArea.animate({ scrollLeft: leftOffset }); alert(leftOffset); }); }); }; myScroll(); }); </script> <style> #box { width: 250px; height: 300px; margin-left: 20px; border: 1px solid black; overflow-x: scroll; white-space: nowrap; } .myclass { width: 250px; height: 100px; margin: 35px; display: inline-block; background: -webkit-linear-gradient(left, red , blue); background: -o-linear-gradient(right, red, blue); background: -moz-linear-gradient(right, red, blue); background: linear-gradient(to right, red , blue); } </style> </head> <body> <p>Get left position of element.</p> <div id="box"> <div class="myclass">First (Click Me)</div> <div class="myclass">Second (Click Me)</div> <div class="myclass">Third (Click Me)</div> </div> </body> </html>
- Related Articles
- How to find horizontal Scroll Position using jQuery?
- How to scroll to element in horizontal div using jQuery?
- How to set scroll left position in jQuery?
- How to position horizontal scroll bar at center of DIV?
- How to Position Element to Bottom of Its Container with CSS Flex
- How do you check scroll position using selenium?
- How to Scroll to the top of the page using JavaScript/ jQuery?
- Set a fixed element and scroll another - jQuery
- How to scroll to element with Selenium WebDriver using C#?
- How to set the left position of a positioned element with JavaScript?
- How to Drag / Pan an Image in a Container div using jQuery?
- How to set the position of Textbox from left using FabricJS?
- How to set the position of Image from left using FabricJS?
- How to clone an element using jQuery?
- How to set textarea scroll bar to bottom as a default using JavaScript/jQuery?

Advertisements