简体   繁体   English

jQuery以某种方式导致页面滚动到页面底部

[英]jQuery somehow causing page to scroll past page bottom

I can't figure out what is going on with a site I am working on, after I made a layout change. 更改布局后,我无法弄清正在处理的网站的状况。

It has 2 columns - one on left for navigation, and a main body area. 它有2列-左侧用于导航,还有一个主体区域。 Both are scrollable. 两者都是可滚动的。 The main content area is somehow scrolling indefinitely past page bottom and I am trying to fix the problem via CSS. 主要内容区域以某种方式无限期地滚动到页面底部之外,我正在尝试通过CSS修复问题。

The structure is 结构是

<div class="overall-wrapper">
   <div class = "wrapper for header, tabs, panels, main content">
      <div class="header"></div>
      <div class="tabs"></div>
      <div class="sidebar"></div> <!-- causing all the trouble it seems! -->
      <div class="main-wrapper">
         <div class = "main"></div>
      </div>
   </div>
</div>

Somehow the 2 columns are coupled in such a way where the main content keeps scrolling past page bottom anytime the navigation panel is not scrolled to its bottom. 不知何故,两列以某种方式耦合,只要导航面板没有滚动到其底部,主要内容就会一直滚动到页面底部。

This screen video shows the issue: 此屏幕视频显示了问题:

https://drive.google.com/open?id=1SLUAKGPLchTCpEhXe55v8o4dMW_w8dvT https://drive.google.com/open?id=1SLUAKGPLchTCpEhXe55v8o4dMW_w8dvT

Here is where you can see various behaviors: 在这里您可以看到各种行为:

First 10 seconds - here you see the odd behavior in main content scrolling when Syllabus tab is selected, and the tab panel's scroll bar is not at bottom 前10秒-在这里,当您选择了Syllabus选项卡并且选项卡面板的滚动条不在底部时,您会看到主要内容滚动中的异常行为

0:13-0:20 - here I scroll the tab panel's content to bottom, and now main content scroll behavior is normal 0:13-0:20-在这里我将选项卡面板的内容滚动到底部,现在主要内容滚动行为是正常的

0:21-0:28 - Here I choose a different tab panel, and the main content scrolls normally 0:21-0:28-在这里我选择一个不同的选项卡面板,并且主要内容正常滚动

0:42 - 0:50 - Here I collapse the navigation panel and the main content still scrolls past bottom of page indefinitely 0:42-0:50-在这里我折叠了导航面板,主要内容仍然无限期地滚动到页面底部

Based on this I can conclude that the presence of a scroll bar in the left panel causes the main content to indefinitely scroll past page bottom whenever the left scroll bar is NOT at the bottom. 基于此,我可以得出结论,只要左滚动条不在底部,则左侧面板中会出现滚动条,导致主要内容无限期地滚动到页面底部。 When the left scroll bar is at bottom, then the main content scrolls normally. 当左滚动条位于底部时,主要内容将正常滚动。 Also, anytime a tab from left is chosen that does not have a scroll bar in its panel, then the main content scrolls normally. 同样,只要选择从左侧开始的选项卡,该选项卡的面板中都没有滚动条,则主内容就会正常滚动。

If you scroll the navigation sidebar when main content is towards bottom ,notice how right scroll bar moves a bit. 如果在主要内容移至底部时滚动导航侧栏,请注意右侧滚动条的移动方式。 Odd! 奇!

It seems that maybe various parts of the app that deal with scroll event are oddly interacting causing this indefinite scrolling in main body. 似乎应用程序中处理滚动事件的各个部分可能发生奇怪的交互,从而导致主体中的不确定滚动。 Just not sure how to solve this weird problem. 只是不确定如何解决这个奇怪的问题。

Here is link to the site under development: https://29b310b4.ngrok.io/lessons/an-introduction-to-triads-and-their-inversions 这是到正在开发的站点的链接: https : //29b310b4.ngrok.io/lessons/an-introduction-to-triads-and-their-inversions

Edit 编辑

So I found some jQuery code in the app that seems to be causing the problem (thanks to Chrome Tools event listener break function): 因此,我在应用程序中发现了一些似乎引起问题的jQuery代码(由于Chrome Tools事件监听器中断功能):

function initTooltips() {

        // Clear out old tooltips


        if( $('#learndash-tooltips').length ) {
            $('#learndash-tooltips').remove();
            $tooltips = $('*[data-ld-tooltip]');
        }

        if ($tooltips.length) {
            $('body').prepend('<div id="learndash-tooltips"></div>');
            var $ctr =1;
            $tooltips.each(function() {
                var anchor = $(this);
                if (anchor.hasClass('ld-item-list-item')) {
                    anchor = anchor.find('.ld-item-title');
                }
                var elementOffsets = {
                    top: anchor.offset().top,
                    left: anchor.offset().left + (anchor.outerWidth() / 2)
                };
                var $content = $(this).attr('data-ld-tooltip');
                var $rel_id = Math.floor((Math.random() * 99999));
                var $tooltip = '<span id="ld-tooltip-' + $rel_id + '" class="ld-tooltip" style="top:' + elementOffsets.top + 'px; left:' + elementOffsets.left + 'px;">' + $content + '</span>';
                $(this).attr('data-ld-tooltip-id', $rel_id);
                $('#learndash-tooltips').append($tooltip);
                $ctr++;
                var $tooltip = $('#ld-tooltip-' + $rel_id);
                $(this).hover(
                    function() {
                        $tooltip.addClass('ld-visible');
                    },
                    function() {
                        $tooltip.removeClass('ld-visible');
                    }
                );
            });

            $(window).on('resize', function() {
                // Reposition tooltips after resizing
                positionTooltips();
            });

            $(window).add('.ld-focus-sidebar-wrapper').on('scroll', function() {
                // Hide tooltips so they don't persist while scrolling
                $('.ld-visible.ld-tooltip').removeClass('ld-visible');
                // Reposition tooltips after scrolling
                positionTooltips();
            });

            positionTooltips();
        }
    }

function positionTooltips() {
        console.log('positiontooltips');
        if ( typeof $tooltips !== 'undefined' ) {
            setTimeout(function() {

                $tooltips.each(function() {
                    var anchor = $(this);
                    var $rel_id = anchor.attr('data-ld-tooltip-id');
                    $tooltip = $('#ld-tooltip-' + $rel_id);

                    if (anchor.hasClass('ld-item-list-item')) {
                        anchor = anchor.find('.ld-item-title');
                    }
                    $tooltip.css({
                        'top' : anchor.offset().top,
                        'left' : anchor.offset().left + (anchor.outerWidth() / 2),
                        'margin-left' : 0,
                        'margin-right' : 0
                    }).removeClass('ld-shifted-left ld-shifted-right');
                    if ($tooltip.offset().left <= 0) {
                        $tooltip.css({ 'margin-left' : Math.abs($tooltip.offset().left) }).addClass('ld-shifted-left');
                    }
                    var $tooltipRight = $(window).width() - ($tooltip.offset().left + $tooltip.outerWidth());
                    if ($tooltipRight <= 0) {
                        $tooltip.css({ 'margin-right' : Math.abs($tooltipRight) }).addClass('ld-shifted-right');
                    }

                });
            }, 500);
        }
    }

What's odd is that this code didn't cause a problem previously. 奇怪的是这段代码以前没有引起问题。 I then made some layout changes, ands the new layout seems to be such that this part 然后,我对布局进行了一些更改,并且新的布局似乎是这样的,

positionTooltips();

called in 呼入

$(window).add('.ld-focus-sidebar-wrapper').on('scroll', function() {

is causing a continuous loop. 导致连续循环。 I commented out positionTooltips(); 我注释掉positionTooltips(); to get rid of the weird scrolling behavior (as opposed to a CSS fix that I was hoping for), but my question is: 摆脱奇怪的滚动行为(与我希望的CSS修复相对),但是我的问题是:

"Why does the function call positionTooltips(); cause infinite scrolling?" “为什么函数调用positionTooltips();导致无限滚动?”

The Solution 解决方案

Ok, this was quite the trip! 好吧,这真是一次旅行! First I thought there was some CSS problem. 首先,我认为存在CSS问题。 Then I thought maybe presence of two scroll bars and improper event binding was causing weird behavior. 然后,我认为可能存在两个滚动条和不正确的事件绑定导致了奇怪的行为。 Kept looking at various possible issues. 继续研究各种可能的问题。

But here is what caused the problem: 但是,这是导致问题的原因:

$tooltip.css({
             'top' : anchor.offset().top,

This is part of the code I pasted above, and it deals with some tooltips that the app is using. 这是我上面粘贴的代码的一部分,它处理了应用程序正在使用的一些工具提示。 Namely, it hides tooltips when scrolling. 即,它在滚动时隐藏工具提示。

Thinking about it, it makes sense. 考虑一下,这是有道理的。 When top of the tooltip anchor is set to anchor.offset().top and that value is greater than the content height, then the browser sees an element with a height greater than current content height and sets that as new content height, therefore on next scroll event it thinks there is still room to scroll, and so on... 当工具提示锚的top设置为anchor.offset().top并且该值大于内容高度时,浏览器将看到一个高度大于当前内容高度的元素,并将其设置为新的内容高度,因此在下一次滚动事件时,它认为仍有滚动空间,依此类推...

So the solution is to cap the top value of one of these tooltips as 因此,解决方案是将这些工具提示之一的top价值限制为

var pageheight = $( 'div.overall-wrapper' ).height();
var currentanchorheight = anchor.offset().top;
var anchortop = currentanchorheight <  pageheight ? currentanchorheight : pageheight;

$tooltip.css({
            'top' : anchortop,

Anyway, probably obvious to experienced developers, but took me all day to figure this out! 无论如何,对于经验丰富的开发人员来说可能很明显,但是我却整日花了很多时间来解决这个问题! Never thought that tooltip manipulations were somehow causing this! 从未想到工具提示操作会以某种方式导致这种情况!

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

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