最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ScrollMagic update trigger positions on resize - Stack Overflow

programmeradmin1浏览0评论

On my website the ScrollMagic scene trigger hook positions are stored in an array slideOffsets = [] which is populated on initialisation using

var scene;
for (var i=0; i<slides.length; i++) {
    //Get offsets
    if(i != 1) {
        scene = new ScrollMagic.Scene({
                triggerElement: slides[i]
            })

            .setPin(slides[i])
            .addIndicators() // INDICATORS FROM PLUGINS
            .addTo(controller);
    } else {
        scene = new ScrollMagic.Scene({
                triggerElement: slides[i], duration: "10%"
            })
            .setPin(slides[i])
            .addIndicators() // INDICATORS FROM PLUGINS
            .addTo(controller);
    }
    slideOffsets.push(Math.ceil(scene.triggerPosition()));
    scenes.push(scene);

}

Then the function attached to each <a> element scrolls to the respective position. This all works perfectly, but after resizing the window this does not work. I have the scenes stored in an array so I can access them, but even after iterating through them and re-storing hook positions after resizing, the returned positions simply do not reflect where the hooks really are on the page, even after trying to take away/add the duration of the scene too.

$(window).on("resize", function() {
    slideOffsets = [];

    for (var i=0; i<slides.length; i++) {
        slideOffsets.push(Math.ceil(scenes[i].triggerPosition() - scenes[i].duration()));
    }
    console.log("regathering done");
});

(btw im aware that this constantly runs as the user is resizing the window, however I'd like to resolve this issue first)

Any ideas? I've been crawling through the documentation for a long time now without any success, .triggerPosition(), .scrollOffset() seem to be doing the exact same thing but neither of them work properly after resizing.

On my website the ScrollMagic scene trigger hook positions are stored in an array slideOffsets = [] which is populated on initialisation using

var scene;
for (var i=0; i<slides.length; i++) {
    //Get offsets
    if(i != 1) {
        scene = new ScrollMagic.Scene({
                triggerElement: slides[i]
            })

            .setPin(slides[i])
            .addIndicators() // INDICATORS FROM PLUGINS
            .addTo(controller);
    } else {
        scene = new ScrollMagic.Scene({
                triggerElement: slides[i], duration: "10%"
            })
            .setPin(slides[i])
            .addIndicators() // INDICATORS FROM PLUGINS
            .addTo(controller);
    }
    slideOffsets.push(Math.ceil(scene.triggerPosition()));
    scenes.push(scene);

}

Then the function attached to each <a> element scrolls to the respective position. This all works perfectly, but after resizing the window this does not work. I have the scenes stored in an array so I can access them, but even after iterating through them and re-storing hook positions after resizing, the returned positions simply do not reflect where the hooks really are on the page, even after trying to take away/add the duration of the scene too.

$(window).on("resize", function() {
    slideOffsets = [];

    for (var i=0; i<slides.length; i++) {
        slideOffsets.push(Math.ceil(scenes[i].triggerPosition() - scenes[i].duration()));
    }
    console.log("regathering done");
});

(btw im aware that this constantly runs as the user is resizing the window, however I'd like to resolve this issue first)

Any ideas? I've been crawling through the documentation for a long time now without any success, .triggerPosition(), .scrollOffset() seem to be doing the exact same thing but neither of them work properly after resizing.

Share Improve this question asked Jun 17, 2018 at 12:23 peterxzpeterxz 8741 gold badge8 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I finally managed to solve to problem, what I had to do was iterate through the saved scenes and individually remove and reset pin, refresh scene and THEN ask for the scrollOffset positions of each:

$(window).on("resize", function() {
    slideOffsets = [];
    for (var i = 0; i < scenes.length; i++) {
        scenes[i].removePin(true);
        scenes[i].setPin(slides[i]);
        scenes[i].refresh();
        slideOffsets.push(Math.ceil(scenes[i].scrollOffset()));
    }
});
发布评论

评论列表(0)

  1. 暂无评论