简体   繁体   中英

Dynamically increasing the height - ScrollSpy

I have implemented Scrollspy in my application.Since i am using the knockout framework in my application user can add upto 15 rows in a section.So if they add 15 rows the scroll spy height is getting affected.How can i make it dynamically.

Code :

$(window).scroll(function () {
    var myArray = new Array();
    var curArray = '';
    $('div.spyClass').each(function () {
        myArray.push($(this).offset().top);
    });
    for (var i = 0; i <= myArray.length; i++) {
        if ($(this).scrollTop() >= myArray[i]) {
//When i add new rows the height of this wil become (say 520+).Then automatically the second link is getting the active .Eventhought it is not active.
                if ($(this).scrollTop() >= -20 && $(this).scrollTop() <= 101) {
                }
                else if ($(this).scrollTop() >= 101 && $(this).scrollTop() <= 520) {
                }
                else if ($(this).scrollTop() >= 520 && $(this).scrollTop() <= 840) {
                }
                else if ($(this).scrollTop() >= 830 && $(this).scrollTop() <= 1300) {
                }
                else if ($(this).scrollTop() >= 1300 && $(this).scrollTop() <= 1600) {
                }
            }
        }
    });

KIndly give me the solution to make it dynamic.

Here is how i achieve it.

Getting the individual content height and adding upto the calculation on the scroll event. Say i have three sections.

$(window).scroll(function () {

    var firstCont= ($('#Content1').height() + 20); -- Since i have fixed position header 20 is needed
    var secondCont = envEnd + ($('#Content2').height() + 20);
    var thirdCon = serEnd + ($('#Content3').height() + 20);


    var myArray = new Array();
    var curArray = '';
    $('div.spyClass').each(function () {
        myArray.push($(this).offset().top);
    });
    for (var i = 0; i <= myArray.length; i++) {
        if ($(this).scrollTop() >= myArray[i]) {
                   if ($(this).scrollTop() >= -20 && $(this).scrollTop() <= firstCont) {
                }
                else if ($(this).scrollTop() >= firstCont && $(this).scrollTop() <= secondCont) {
                }
                else if ($(this).scrollTop() >= secondCont && $(this).scrollTop() <= thirdCont) {
                }
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM