简体   繁体   English

更改固定元素的内容时不希望的页面滚动

[英]Undesired page scroll when changing content of fixed element

So I've made a menu sort of thing, affixed to the bottom of the screen via CSS fixed position, and it works fine. 因此,我做了一个菜单之类的东西,它通过CSS固定位置固定在屏幕底部,并且工作正常。 Only problem is that when the page is scrolled down somewhat, and then the menu activated (slide up animated with jQuery), the page jumps to the top again. 唯一的问题是,当页面稍微向下滚动,然后激活菜单(使用jQuery向上滑动动画)时,页面再次跳至顶部。 It's pretty irritating. 很烦人。 Here's some pertinent code: 这里是一些相关的代码:

//handle click on stats tab
$('a.stats').click(function(e) {
    if ($('#menuWrapper').hasClass("stats")) {
        $('#menuWrapper').removeClass("stats")
        .removeClass("open")
        .animate({ bottom: '-100px'}, 300);
    } else if (!$('#menuWrapper').hasClass("open")) {
        $('#sponsors').hide();
        $('#feedback').hide();
        $('#stats').show();
        $('#menuWrapper').animate({ bottom: '0px'}, 300)
        .addClass("stats")
        .addClass("open");
    } else {
        $('#menuWrapper').addClass("stats");
        $('#sponsors').fadeOut();
        $('#feedback').fadeOut();
        $('#stats').fadeIn();
    }
    $('#menuWrapper').removeClass("sponsors")
    .removeClass("feedback");
});

(this is an example of how I change the content. I have similar functions which change the content or minimize the menu depending on its current state.) (这是我如何更改内容的示例。我具有类似的功能,它们可以根据当前状态更改内容或最小化菜单。)

<div id="menuWrapper">

    <div id="menuTop">
        <a href="#" class="sponsors">Sponsors</a> | <a href="#" class="feedback">Feedback</a> | <a href="#" class="stats">Stats</a>
    </div>

    <div id="menuContent">
        <div id="sponsors"></div>
        <div id="feedback"></div>
        <div id="stats"></div>
    </div>

</div>

(and this is what the HTML looks like, before the content is dynamically loaded into the sponsors, feedback, and stats divs.) (这是HTML的外观,然后将内容动态加载到赞助者,反馈和统计数据div中。)

To see an example of the funky functionality if necessary, see http://www.crowdsplat.com/alpha . 要在必要时查看时髦功能的示例,请参见http://www.crowdsplat.com/alpha If you have any insight into what may be causing this and/or how I can fix it, I'd greatly appreciate it. 如果您对造成此问题的原因和/或如何解决有任何了解,我将不胜感激。 Thanks for your time. 谢谢你的时间。

Try return false from your click function or use event.preventDefault() to avoid the browser trying to follow the link which I assume is causing the jump to the top if you have an invalid bookmark eg href="#" 尝试从点击函数返回false或使用event.preventDefault()来避免浏览器尝试跟踪链接,如果您使用的书签无效,我认为该链接会导致跳转到顶部,例如href =“#”

eg 例如

$('a.stats').click(function(e) {
    e.preventDefault();
    if ($('#menuWrapper').hasClass("stats")) {
        $('#menuWrapper').removeClass("stats")
        .removeClass("open")
        .animate({ bottom: '-100px'}, 300);
    } else if (!$('#menuWrapper').hasClass("open")) {
        $('#sponsors').hide();
        $('#feedback').hide();
        $('#stats').show();
        $('#menuWrapper').animate({ bottom: '0px'}, 300)
        .addClass("stats")
        .addClass("open");
    } else {
        $('#menuWrapper').addClass("stats");
        $('#sponsors').fadeOut();
        $('#feedback').fadeOut();
        $('#stats').fadeIn();
    }
    $('#menuWrapper').removeClass("sponsors")
    .removeClass("feedback");
});

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

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