简体   繁体   English

在调整窗口大小时更新滚动位置

[英]Update scroll position on resize of window

I'm currently using a combination of smooth scroll and IDs/anchor tags to scroll to content on my site. 我目前正在使用平滑滚动和ID /锚标签的组合来滚动到我网站上的内容。 The code below is getting the ID of the next 'section' in the DOM, and adding it's ID as the 'view next section' href, so once it's clicked, it'll scroll to the top of that div. 下面的代码获取DOM中下一个“部分”的ID,并将其ID添加为“查看下一个部分” href,因此,一旦单击它,它将滚动到该div的顶部。 Then, it iterates through, updating the href with the next ID each time etc until the last section is seen and it scrolls back to the top. 然后,进行遍历,每次使用下一个ID更新href等等,直到看到最后一部分,然后滚动回到顶部。 Pretty straightforward. 非常简单。

The only problem is that the 'sections' are fullscreen images, so as it's scrolling to the top of the next section, if you resize the browser, the top position of that section (where we scrolled to) has moved, and means the position is lost. 唯一的问题是“部分”是全屏图像,因此当它滚动到下一部分的顶部时,如果您调整浏览器的大小,则该部分的顶部位置(我们滚动到的位置)已经移动,并且意味着该位置迷路了。

I've created a JSFiddle. 我创建了一个JSFiddle。 You can see this happening after you click the arrow to visit the next section then resize the window: http://jsfiddle.net/WFQ9t/3/ 单击箭头访问下一部分,然后调整窗口大小后,您可以看到这种情况: http : //jsfiddle.net/WFQ9t/3/

I'm wanting to keep this top position fixed at all times so even if you resize the browser, the scroll position is updated to reflect this. 我想一直保持此顶部位置不变,因此即使您调整浏览器的大小,滚动位置也会更新以反映这一点。

Thanks in advance, R 预先感谢,R

var firstSectionID = $('body .each-section').eq(1).attr('id');
$('.next-section').attr('href', '#' + firstSectionID);

var i = 1;
$('.next-section').click(function() {

    var nextSectionID = $('body .each-section').eq(i).attr('id');
    i++;
    $('.next-section').attr('href', '#' + nextSectionID);

    var numberOfSections = $('body .each-section').length;
    var lastSectionID = $('body .each-section').eq(numberOfSections).attr('id');

    if ($('.next-section').attr('href') == '#' + lastSectionID ) { 
        $('.next-section').attr('href', '#introduction');
        i = 1;
    }

});

Ok, Please check out this fiddle: http://jsfiddle.net/WFQ9t/9/ 好的,请检查此小提琴: http : //jsfiddle.net/WFQ9t/9/

The few things I did were: 我所做的几件事是:

  1. Made some global variables to handle the screen number (which screen you're on and also the initial window height. You will use this when the screen loads and also when you click on the .next-session arrow. 做了一些全局变量来处理屏幕号(您所在的屏幕以及初始窗口高度。在屏幕加载时以及单击.next-session箭头时将使用此变量。

     var initWinHeight = $(window).height(); var numSection = 0; 
  2. Then I tossed those variables into your resizeContent() function 然后,我将这些变量扔到您的resizeContent()函数中

     resizeContent(initWinHeight, numSection) 

    so that it will work on load and resize 这样它就可以加载并调整大小

  3. I made the body move around where it needs to, to accomodate for the movement of the divs (I still don't understand what divs are moving when the regular animation happens). 我使body在需要的地方移动,以适应div的移动(当常规动画发生时,我仍然不知道div在移动什么)。

     $('body').css({ top: (((windowHeight - initWinHeight)*numSection)*-1) + "px" }); 
  4. Then in your click function, I add 1 to the section number, reset the initial window height and then also reset the body to top:0 . 然后在您的单击功能中,将1加到段号上,重置初始窗口高度,然后还将body重置为top:0 The normal animation you have already puts the next section at the top of the page. 您已经将普通动画放在页面顶部的下一部分。

     numSection++; initWinHeight = $(window).height(); $('body').css({top:"0px"}, 1000); 
  5. Finally, I reset the numSections counter when you reach the last page (You might have to make this 0 instead of 1) 最后,当您到达最后一页时,我重置了numSections计数器(您可能必须将其设置为0而不是1)

     numSection = 0; 

The fiddle has all of this in the correct places, these are just the steps I took to change the code. 小提琴把所有这些都放在正确的位置,这些只是我更改代码所采取的步骤。

Here is a solution that i found, but I dont use anchor links at this point, i use classes 这是我找到的解决方案,但此时我不使用锚链接,而是使用类

Here is my HTML code: 这是我的HTML代码:

<section class="section">
    Section 1
</section>

<section class="section">
    Section 2
</section>

<section class="section">
    Section 3
</section>

<section class="section">
    Section 4
</section>

And here is my jQuery/Javascript code, I actually used a preety simple way: 这是我的jQuery / Javascript代码,实际上我使用了一种简单的方法:

    $('.section').first().addClass('active');

/* handle the mousewheel event together with 
 DOMMouseScroll to work on cross browser */
$(document).on('mousewheel DOMMouseScroll', function (e) {
    e.preventDefault();//prevent the default mousewheel scrolling
    var active = $('.section.active');
    //get the delta to determine the mousewheel scrol UP and DOWN
    var delta = e.originalEvent.detail < 0 || e.originalEvent.wheelDelta > 0 ? 1 : -1;

    //if the delta value is negative, the user is scrolling down
    if (delta < 0) {
        next = active.next();
        //check if the next section exist and animate the anchoring
        if (next.hasClass('section')) {
            var timer = setTimeout(function () {
                $('body, html').animate({
                    scrollTop: next.offset().top
                }, 800);

                next.addClass('active')
                    .siblings().removeClass('active');

                clearTimeout(timer);
            }, 200);
        }

    } else {
        prev = active.prev();

        if (prev.length) {
            var timer = setTimeout(function () {
                $('body, html').animate({
                    scrollTop: prev.offset().top
                }, 800);

                prev.addClass('active')
                    .siblings().removeClass('active');

                clearTimeout(timer);
            }, 200);
        }

    }
});


/*THE SIMPLE SOLUTION*/
$(window).resize(function(){
    var active = $('.section.active')

     $('body, html').animate({
        scrollTop: active.offset().top
    }, 10);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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