get percentage scrolled of an element with jquery

前端 未结 3 2121
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 21:13

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 t

3条回答
  •  误落风尘
    2020-12-14 21:37

    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());
    

提交回复
热议问题