简体   繁体   English

防止滚动溢出:单击指向锚点的链接时隐藏的元素

[英]prevent scrolling of overflow:hidden elements when link to anchors are clicked

clicking the link the anchor is reached, but even if the parent element has overflow:hidden it scrolls unnecessarily hiding the contents 单击链接可到达锚点,但是即使父元素已溢出:隐藏,它也会不必要地滚动以隐藏内容

<style>
div#x
    {overflow:hidden;border-bottom:1px red solid;}

div#x > div
    {border:1px red solid;padding:10px;float:left;width:33%;box-sizing:border-box;padding-bottom:10000px;margin-bottom:-10000px;}
</style>

<div id="x">
    <div><a href="#test">go to</a><br><br>a</div>
    <div>a<br><br><br><br>a</div>
    <div>a<br><br><br><br><br><br><span id="test">go here</span></div>
</div>

demo: http://jsfiddle.net/aj8cX/5/ 演示: http : //jsfiddle.net/aj8cX/5/

is there a way to fix this behavior? 有没有办法解决此问题?

This is as close as I could get, using table > table-cell : 使用table > table-cell ,这与我所能达到的尽可能接近:

http://jsfiddle.net/mikedidthis/AmNxf/ http://jsfiddle.net/mikedidthis/AmNxf/

Would work well for static content, but for dynamic content it may cause some issues. 对于静态内容,效果很好,但是对于动态内容,可能会引起一些问题。

You could use some javascript trickery. 您可以使用一些JavaScript技巧。

I am using jQuery, but you can use anything you want. 我正在使用jQuery,但是您可以使用任何您想要的东西。

$('a[href="#test"]').on('click', function(){
    var test_element = $('#test');
    var scroll_top = test_element.scrollTop();
    var max_height = test_element.parent().height();
    if (scroll_top < max_height) { return false; }
    else { return true; } //only scroll if the item is in view
});

You can use javascript to get elements of equal height: 您可以使用javascript获取高度相等的元素:

var height = 0;
$.each($('#x div'),function(k,v){
    if($(this).height() > height){
        height = $(this).height();
    }
});
$.each($('#x div'),function(){
    $(this).height(height);
});

Fix your css like so: 像这样修复您的CSS:

div#x
{
    overflow:visible;
    height:10000px;
}

div#x  div
{
    border:1px red solid;
    float:left;
    width:33%;
    box-sizing:border-box;
    margin-right:-1px;
}

Working example: http://jsfiddle.net/aj8cX/6/ 工作示例: http : //jsfiddle.net/aj8cX/6/

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

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