简体   繁体   English

jQuery模态弹出导致页面滚动

[英]Jquery modal pop up causing page to scroll

So I have a modal window that is causing the page to scroll down. 所以我有一个模式窗口,导致页面向下滚动。 It adds #login-box to the url but that div id is not a set place. 它将#login-box添加到url中,但该div id不是一个设置位置。 I thought about adding a div to the html but that wouldn't work because my menu is sticky and it would cause them to scroll to wherever that div is. 我曾考虑过将div添加到html中,但是由于我的菜单粘滞,将导致它们滚动到该div所在的位置,因此无法使用。

http://onecraftyshop.com and click "login" in the nav menu. http://onecraftyshop.com ,然后在导航菜单中单击“登录”。 You will see what I'm talking about. 您会看到我在说什么。 Then scroll down and click it again and oyu will see that it scrolls down no matter where you are on the page. 然后向下滚动并再次单击它,无论您在页面上的什么位置,oyu都会看到它向下滚动。

Here is the relevant JS of the modal window: 这是模态窗口的相关JS:

// Load the modal window
$('a.login-window').click(function() {

    // Get the value in the href of our button.
    var login_id = $(this).attr('href');

    // Add our overlay to the body and fade it in.
    $('body').append('<div id="overlay"></div>');
    $('#overlay').fadeIn(300);

    // Fade in the modal window.
    $(login_id).fadeIn(300);

    // center our modal window with the browsers.
    var margin_left = ($(login_id).width() + 24) / 2;
    var margin_top = ($(login_id).height() + 24) / 2;

    $(login_id).css({
        'margin-left' : -margin_left,
        'margin-top' : -margin_top
    });

    return false;
});

I tried changing 我尝试改变

var margin_top = ($(login_id).height() + 24) / 2; to

var margin_top = ($(login_id).height() + 24) / .7; and that stopped the scrolling but the box wasn't centered (it was actually cut off at the top of the page) I then thought "oh easy just change the positioning with css", but then that caused it to start scrolling again! 然后停止了滚动,但是框没有居中(实际上是在页面顶部被切除了),然后我想到“哦,很简单,只需使用css更改位置”即可,然后导致它再次开始滚动!

Visit http://onecraftyshop.com and click "login" in the nav menu. 访问http://onecraftyshop.com ,然后在导航菜单中单击“登录”。 The modal window should pop up and the page will scroll. 模态窗口应弹出,页面将滚动。 CSS through firebug or dev tool in chrome. 通过Chrome中的Firebug或dev工具的CSS。

Let me know if you need anything else. 需要帮助请叫我。

Thanks for helping me sort this one out! 感谢您帮助我解决这一问题!

------------CSS for #overlay as requested-------------------------- ------------ CSS用于#overlay的要求--------------------------

background: none repeat scroll 0 0 #000000;
height: 100%;
left: 0;
opacity: 0.8;
position: fixed;
top: 0;
width: 100%;
z-index: 9999999;

To stop scrolling on the page, create this javascript function (written in plain javascript): 要停止在页面上滚动,请创建以下javascript函数(用普通的javascript编写):

scrollingToggleIterates = 0;
function toggleScrolling(event){
    if(scrollingToggleIterates%2 == 0){
        var scrollPosition = document.documentElement.scrollTop;
        document.body.className = 'stopScrolling';
        document.documentElement.scrollTop = scrollPosition;
    }
    else{
        var scrollPosition = document.documentElement.scrollTop;
        document.body.className = '';
        document.documentElement.scrollTop = scrollPosition;
    }
    scrollingToggleIterates++;
}

Then add this class to your css: 然后将此类添加到您的CSS:

.stopScrolling{
  height: 100%;
  overflow:hidden;
}

Call the toggleScrolling function when you want to prevent scrolling, then call the function again when you want to restart it. 如果要防止滚动,请调用toggleScrolling函数,然后在要重新启动它时再次调用该函数。 I know you're working in JQuery, but you should be able to intermix it with plain JS. 我知道您正在使用JQuery,但是您应该可以将其与纯JS混合。

The plugin that scrolls the window down on fragment links is causing this. 在片段链接上向下滚动窗口的插件导致了这种情况。 If you unhide the login menu you can see the window is scrolling down to it's original location. 如果取消隐藏登录菜单,则可以看到该窗口向下滚动到其原始位置。 Since this isn't the default browser behavior, return false or event.preventDefault() will not stop this behavior. 由于这不是默认的浏览器行为,因此return falseevent.preventDefault()不会停止此行为。 You will need to see if there's a class you can add to this link that will stop the plugin from scrolling when this link is clicked. 您将需要查看是否有一个可以添加到该链接的类,当单击该链接时,该类将阻止插件滚动。

Another option is to not use the href property for the id of the login window. 另一种选择是不使用href属性作为登录窗口的ID。 Make it either href="javascript:void(0)" or just href="#" . 将其设置为href="javascript:void(0)"或仅href="#" Then use a data attribute like: data-target="loginMenu" and update your javascript to grab the id from this attribute: 然后使用数据属性,例如: data-target="loginMenu"并更新您的JavaScript以从此属性获取ID:

var login_id = $(this).attr('data-target');

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

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