简体   繁体   English

容器内的固定div

[英]A fixed div within a container

So I have the following: 所以我有以下内容:

<div id="TwoColumns">

    <div id="LeftColumn">
        <div id="navigation">
            /*This is a fixed navigation*/
            Anchor link here to PointOne
            Anchor link here to PointTwo
            Anchor link here to PointThree
        </div>
    </div>

    <div id="RightColumn>
        <div id="PointOne">
            Point One
        </div> 
        <div id="PointTwo">
            Point Two
        </div> 
        <div id="PointThree">
            Point Three
        </div> 
    </div>

</div>

1) What I want to do is when a user scrolls the navigation moves within the LeftColumn and follows the user down as a fixed element usually would but strictly within container only. 1)我想要做的是当用户在LeftColumn中滚动导航移动并作为固定元素跟随用户时,通常只在容器内。

2) When a anchor link is clicked reposition navigation to be inline with relevant Point. 2)单击锚链接时,重新定位导航以与相关点内联。

So what I am doing is setting top:0; 所以我正在做的是设置top:0; for navigation when anchor link is clicked, the issue with doing this is that when I scroll to the top the fixed div now leaves it's container which is LeftColumn . 对于单击锚链接时的navigation ,这样做的问题是,当我滚动到顶部时,固定div现在离开它的容器,即LeftColumn

I don't mind using javascript and jquery. 我不介意使用javascript和jquery。

UPDATE UPDATE

Ok so Oswaldo Acauan html/css solution gets my 1st point ticked off. 好的Oswaldo Acauan html / css解决方案获得了我的第一点。

The second issue still is an issue. 第二个问题仍然是一个问题。 When I click on link the navigation is not in-line with content on the right hand side. 当我点击链接时,导航与右侧的内容不一致。

在此输入图像描述

I am getting WRONG at the moment and would like the CORRECT vision. 我现在变得WRONG了,并且想要CORRECT愿景。 I can't for the life of me figure it out. 我不能为我的生活弄清楚。

http://jsfiddle.net/BbAck/1/ http://jsfiddle.net/BbAck/1/

You can try Scrollspy by TwitterBootstrap, or do it with CSS/HTML and a little bit of Javascript/jQuery. 您可以通过TwitterBootstrap尝试Scrollspy ,或者使用CSS / HTML和一些Javascript / jQuery。

Demo HERE 这里演示

HTML: HTML:

<div id="Container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<div id="TwoColumns">

    <div id="LeftColumn">
        <div id="navigation">
            This should be in line with the top of the point
            <a href="#PointOne">Anchor link here to PointOne</a>
            <a href="#PointTwo">Anchor link here to PointTwo</a>
            <a href="#PointThree">Anchor link here to PointThree</a>
        </div>
    </div>

    <div id="RightColumn">
        <div id="PointOne">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </div> 
        <div id="PointTwo">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </div> 
        <div id="PointThree">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </div> 
    </div>

</div>

​ CSS: CSS:

html,body { height: 100%; }

#Container { overflow: hidden; }

#LeftColumn {
    float: left;
    width: 50%;
}

#navigation.fix {
    position: fixed;
    top: 0;
}

#navigation a {
    display: block; 
}

#RightColumn {
    width: 50%;
    float: right;
}

#PointOne { 
    background-color: red;
    height: 159px;
}

#PointTwo { 
    background-color: green;
    height: 400px;
}

#PointThree { 
    background-color: purple;
    height: 650px;
}

​ JS: JS:

$(window).scroll(function() {
    yOffset = window.pageYOffset;
    yContainer = $('#Container').height() - $('#RightColumn').height();
    if (yOffset >= yContainer) {
        $('#navigation').addClass('fix');
    } else {
        $('#navigation').removeClass('fix');
    }
});​

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

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