简体   繁体   English

jquery-steps:如何防止页面滚动到顶部?

[英]jquery-steps: how to prevent page scrolling to top?

I'm testing the jquery-steps plugin, and if I click on the Next or Previous button (on the bottom) and the window top is below the steps' div top (ie this happens if my browser window is just too short in height), the scroll jumps to the body top.我正在测试 jquery-steps 插件,如果我单击“下一步”或“一步”按钮(底部)并且窗口顶部低于步骤的 div 顶部(即,如果我的浏览器窗口高度太短,则会发生这种情况),滚动跳转到正文顶部。

Apparently there's no way to prevent this, I tried everything including editing the plugin code.显然没有办法阻止这种情况,我尝试了一切,包括编辑插件代码。 The only thing I could do was setting a different scroll position by adding some code to the onStepChanging event:我唯一能做的就是通过向onStepChanging事件添加一些代码来设置不同的滚动位置:

$("#steps-container").steps({
        /* ... */
        onStepChanging: function(event, currentIndex, priorIndex)
        {
            var top = 300;
            var pos = $(window).scrollTop();
            if (pos > top + 48)
            {
                $('body').scrollTop(top);
            }
            return true;
        },
        labels:
        {
            /* ... */
        },
        onFinishing: function (event, currentIndex) { submitOrderForm(); return true; }
    });

Can somebody help me sorting this out?有人可以帮我解决这个问题吗? Thanks!谢谢!

Fortunately, I think I've found the solution.幸运的是,我想我已经找到了解决方案。

Unfortunately, it involves making your own edited version of the plugin, because the developer does not provide an option for this, or expose this function for overriding.不幸的是,它涉及制作您自己编辑的插件版本,因为开发人员没有为此提供选项,也没有公开此功能以进行覆盖。

In the function refreshStepNavigation , comment out or remove the following code (at line 870 in the unminified version :在函数refreshStepNavigation ,注释掉或删除以下代码( 在未缩小版本的第 870 行

currentOrNewStepAnchor.focus();

I couldn't see anything to do with modifying scroll position or finding the top offset of an element in the source, so twigged it might be trying to focus some element which causes the browser to jump to it.我看不出与修改滚动位置或在源中查找元素的顶部偏移有任何关系,因此 twigged 它可能试图聚焦某些导致浏览器跳转到它的元素。 Tried this in a quick jsfiddle and it seemed to work...在一个快速的 jsfiddle 中尝试了这个,它似乎有效......

So when you encounter this and you're also using a validation like the jQuery Validate plugin, you have to comment out or remove a different line in some situations.因此,当您遇到这种情况并且您还使用了像 jQuery Validate 插件这样的验证时,您必须在某些情况下注释掉或删除不同的行。

In my case, if form validation fails the step stays the same and the browser view pops to the top.在我的情况下,如果表单验证失败,步骤保持不变,浏览器视图会弹出到顶部。 Comment out or remove line 1273 to remove this behaviour (see here ):注释掉或删除第 1273 行以删除此行为(请参阅此处):

getStepAnchor(wizard, oldIndex).focus();

I had same problem with validation in minified version (v1.1.0), thought it might be helpful for somebody.我在缩小版本(v1.1.0)中遇到了同样的验证问题,认为它可能对某人有帮助。

Just find this code:只需找到此代码:

return g===f.currentIndex?(j(d,g).focus(),!1):void 0

And change it to this并改成这样

return g===f.currentIndex?(true,!1):void 0

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

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