Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get percentage scrolled of an element with jquery

I'm trying to get an div to animate 0% - 100% relative to the percentage scrolled of an element.

Now I've set up a few variables, but I'm having trouble trying to calculate the height of one by percentage.

We can set the starting width quite easily and detect the scroll easily enough too, as can we get the height of the element that'll trigger the animation, I just can't get it as a percentage.

If I can figure out how to return the percent of conheight scrolled then this should be easy.

$(window).scroll(function() {

    // calculate the percentage the user has scrolled down the page
    var scrollPercent = ($(window).scrollTop() / $(document).height()) * 100;

    $('.bar-long').css('width', scrollPercent +"%"  );

});

Here's a jsfiddle, http://jsfiddle.net/SnJXQ/

This is animating bar-long based on the percent scrolled of the body element.

Animates from 0% - 100% (well, it doesn't really, but I can't figure out why).

What I'd like to do is get scroll percent of the .post div, then animate bar-long relative to that. ie. Scrolled 10% of .post, .bar-long is 10% width, scrolled 70% of .post, .bar-long is 70% width.

like image 345
andy Avatar asked Oct 27 '13 02:10

andy


People also ask

How to get scroll percentage of HTML element in JavaScript?

Your problem is the same as How to get horizontal scrolling percentage of an HTML element in JavaScript?, but vertically. /* JS */ var scrollPercentage = 100 * containeR.scrollTop / (containeR.scrollHeight-containeR.clientHeight); /*jQuery*/ var scrollPercent = 100 * $ (containeR).scrollTop () / ($ (containeD).height () - $ (containeR).height ());

What should be the scroll percentage of a document?

So when you scroll, and you start to see the element at the bottom the percentage should be around 1% and when the element is almost disappear in the top it should be around 99%. I was not able to find the formula for this case, only for the scroll percentage of the complete document. Thanks. :)

How to animate bar-long based on the scroll percentage of page?

$ (window).scroll (function () { // calculate the percentage the user has scrolled down the page var scrollPercent = ($ (window).scrollTop () / $ (document).height ()) * 100; $ ('.bar-long').css ('width', scrollPercent +"%" ); }); This is animating bar-long based on the percent scrolled of the body element.

What is a scroll event in JavaScript?

The scroll event occurs when the user scrolls in the specified element. The scroll event works for all scrollable elements and the window object (browser window). The scroll() method triggers the scroll event, or attaches a function to run when a scroll event occurs.


2 Answers

Demo

Your problem is the same as How to get horizontal scrolling percentage of an HTML element in JavaScript?, but vertically.

Then, to get the vertically scrolled percentage, use

/*  JS  */ var scrollPercentage = 100 * containeR.scrollTop / (containeR.scrollHeight-containeR.clientHeight); 
/*jQuery*/ var scrollPercent = 100 * $(containeR).scrollTop() / ($(containeD).height() - $(containeR).height());

In your case, containeR = window; containeD = document:

var scrollPercent = 100 * $(window).scrollTop() / ($(document).height() - $(window).height());
like image 106
Oriol Avatar answered Sep 21 '22 17:09

Oriol


Ok I see what you are trying to achieve....in fact I just did something very similar. Most solutions I found out there also were only for full page examples with window or document properties. Instead I needed this in a specific div which in my case was actually used to update the horizontal position of another div.

First, you are going to want the scroll event attached to your $('.post')

Next, the height of the $('.content') is going to equal your actual Scroll Height

Lastly, apply your percentage formula : (Y / (scrollHeight - postHeight)) * 100 = scrollPercent

So instead of using document or window attributes your code should be as follows:

$('.post').scroll(function() {
  var currY = $(this).scrollTop();
  var postHeight = $(this).height();
  var scrollHeight = $('.content').height();
  var scrollPercent = (currY / (scrollHeight - postHeight)) * 100;
});

You can find the fiddle here: JS Fiddle For Div Scroll Percentage

The current project where I have implemented this is located here: Vertical Scroll Drives Horizontal Position

I hope this solves your problem :)

like image 28
Xanfar Avatar answered Sep 22 '22 17:09

Xanfar