javascript - 如何使 DIV 扩展以适应绝对定位的一个元素和固定定位的另一个元素

标签 javascript jquery html css

我的页面顶部有一个 UL/LI,LI 位于“相对”位置,包含菜单按钮。

底部的另一个 div 定位为“固定”,包含帮助、版权、使用条款等链接。

我想在这两者之间拉伸(stretch)以适应这两者之间的一个 div,以便它随浏览器窗口调整大小..

CSS:

.content {
    margin-left:1%;
}

ul.sdt_menu {
    margin:0;
    padding:0;
    list-style: none;
    font-family:"Myriad Pro", "Trebuchet MS", sans-serif;
    font-size:14px;
    width:1020px;
}
ul.sdt_menu a {
    text-decoration:none;
    outline:none;
}
ul.sdt_menu li{
    float:left;
    width:170px;
    height:85px;
    position:relative;
    cursor:pointer;
}
ul.sdt_menu li > a {
    position:absolute;
    top:0px;
    left:0px;
    width:170px;
    height:85px;
    z-index:12;
    background:transparent url(../images/overlay.png) no-repeat bottom right;
    -moz-box-shadow:0px 0px 2px #000 inset;
    -webkit-box-shadow:0px 0px 2px #000 inset;
    box-shadow:0px 0px 2px #000 inset;
}
ul.sdt_menu li a img{
    border:none;
    position:absolute;
    width:0px;
    height:0px;
    bottom:0px;
    left:85px;
    z-index:100;
    -moz-box-shadow:0px 0px 4px #000;
    -webkit-box-shadow:0px 0px 4px #000;
    box-shadow:0px 0px 4px #000;
}
ul.sdt_menu li span.sdt_wrap{
    position:absolute;
    top:25px;
    left:0px;
    width:170px;
    height:60px;
    z-index:15;
}
ul.sdt_menu li span.sdt_active{
    position:absolute;
    background:#111;
    top:85px;
    width:170px;
    height:0px;
    left:0px;
    z-index:14;
    -moz-box-shadow:0px 0px 4px #000 inset;
    -webkit-box-shadow:0px 0px 4px #000 inset;
    box-shadow:0px 0px 4px #000 inset;
}
ul.sdt_menu li span span.sdt_link,
ul.sdt_menu li span span.sdt_descr,
ul.sdt_menu li div.sdt_box a{
    margin-left:15px;
    text-transform:uppercase;
    text-shadow:1px 1px 1px #000;
}
ul.sdt_menu li span span.sdt_link{
    color:#fff;
    font-size:24px;
    float:left;
    clear:both;
}
ul.sdt_menu li span span.sdt_descr{
    color:#0B75AF;
    float:left;
    clear:both;
    width:155px; /*For dumbass IE7*/
    font-size:10px;
    letter-spacing:1px;
}
ul.sdt_menu li div.sdt_box{
    display:block;
    position:absolute;
    width:340px;
    xoverflow:hidden;
    height:340px;
    top:85px;
    left:0px;
    display:none;
    background:#000;
}
ul.sdt_menu li div.sdt_box a{
    float:left;
    clear:both;
    line-height:30px;
    color:#0B75AF;
}
ul.sdt_menu li div.sdt_box a:first-child{
    margin-top:15px;
}
ul.sdt_menu li div.sdt_box a:hover{
    color:#fff;
}

HTML:

<html>
    <head>

        <title>Employment Application</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon"/>
        <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>


        <!-- The JavaScript -->
        <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
        <script type="text/javascript" src="js/jquery.easing.1.3.js"></script>

        <style>
            body{
                background:#333 url(images/bg.jpg) repeat top left;
                font-family:Arial;
            }

            span.reference {
                position:fixed;
                left:10px;
                bottom:10px;
                font-size:12px;
            }

            span.reference a {
                color:#aaa;
                text-transform:uppercase;
                text-decoration:none;
                text-shadow:1px 1px 1px #000;
                margin-right:30px;
            }

            span.reference a:hover {
                color:#ff2222;
            }

            ul.sdt_menu {
                margin-top:80px;
            }

            h1.title {
                text-indent:-9000px;
                background:transparent url(images/title.png) no-repeat top right;
                width:633px;
                height:69px;
            }
        </style>


        <script type="text/javascript">
            $(function() {
                /**
                * for each menu element, on mouseenter, 
                * we enlarge the image, and show both sdt_active span and 
                * sdt_wrap span. If the element has a sub menu (sdt_box),
                * then we slide it - if the element is the last one in the menu
                * we slide it to the left, otherwise to the right
                */

                $('#sdt_menu > li').bind('mouseenter',function(){
                    var $elem = $(this);
                    $elem.find('img')
                         .stop(true)
                         .animate({
                            'width':'170px',
                            'height':'170px',
                            'left':'0px'
                         },400,'easeOutBack')
                         .andSelf()
                         .find('.sdt_wrap')
                         .stop(true)
                         .animate({'top':'140px'},500,'easeOutBack')
                         .andSelf()
                         .find('.sdt_active')
                         .stop(true)
                         .animate({'height':'170px'},300,function() {
                        var $sub_menu = $elem.find('.sdt_box');
                        if($sub_menu.length){
                            var left = '170px';
                            if($elem.parent().children().length == $elem.index()+1)
                                left = '-170px';
                            $sub_menu.show().animate({'left':left},300);
                        }   
                    });
                }).bind('mouseleave',function(){
                    var $elem = $(this);
                    var $sub_menu = $elem.find('.sdt_box');
                    if($sub_menu.length)
                        $sub_menu.hide().css('left','0px');

                    $elem.find('.sdt_active')
                         .stop(true)
                         .animate({'height':'0px'},300)
                         .andSelf().find('img')
                         .stop(true)
                         .animate({
                            'width':'0px',
                            'height':'0px',
                            'left':'85px'},400)
                         .andSelf()
                         .find('.sdt_wrap')
                         .stop(true)
                         .animate({'top':'25px'},500);
                });
            });
        </script>

    </head>

    <body>
        <div class="content">
            <h1 class="title">Employment Application</h1>
            <ul id="sdt_menu" class="sdt_menu">
                <li>
                    <a href="#">
                        <img src="images/AboutUs.jpg" alt=""/>
                        <span class="sdt_active"></span>
                        <span class="sdt_wrap">
                            <span class="sdt_link">About Us</span>
                            <span class="sdt_descr">Let's Get Started</span>
                        </span>
                    </a>
                    <div class="sdt_box">
                            <a href="#">About US</a>
                            <a href="#">E-Verify</a>
                            <a href="#">Self Identification (Optional)</a>
                            <a href="#">How'd You Hear About Us? (Optional)</a>
                    </div>
                </li>
                <li>
                    <a href="#">
                        <img src="images/AboutYou.jpg" alt=""/>
                        <span class="sdt_active"></span>
                        <span class="sdt_wrap">
                            <span class="sdt_link">About You</span>
                            <span class="sdt_descr">Just The basics</span>
                        </span>
                    </a>
                    <div class="sdt_box">
                            <a href="#">1. About You</a>
                            <a href="#">2. Education</a>
                            <a href="#">3. Military Service</a>
                            <a href="#">4. Organisations</a>
                    </div>
                </li>
                <li>
                    <a href="#">
                        <img src="images/3.jpg" alt=""/>
                        <span class="sdt_active"></span>
                        <span class="sdt_wrap">
                            <span class="sdt_link">History</span>
                            <span class="sdt_descr">Where You've Been</span>
                        </span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="images/4.jpg" alt=""/>
                        <span class="sdt_active"></span>
                        <span class="sdt_wrap">
                            <span class="sdt_link">References</span>
                            <span class="sdt_descr">Applicant Testimonials</span>
                        </span>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <img src="images/5.jpg" alt=""/>
                        <span class="sdt_active"></span>
                        <span class="sdt_wrap">
                            <span class="sdt_link">Finish</span>
                            <span class="sdt_descr">Few last things and we're done</span>
                        </span>
                    </a>
                </li>
            </ul>
        </div>
        <div class="content" style="color:white; margin-top:8px; float:left; width:70%; height:400px; overflow-y:auto; border:1px solid red; ">
            <br>
            <br>
            alskd fjlaksdj flaksdj flaksdj flkasdj flaksdjf lkasjdflkajsdlfk jalskdfj <br>
            alskd fjlaksdj flaksdj flaksdj flkasdj flaksdjf lkasjdflkajsdlfk jalskdfj <br>
        </div>

        <div>
            <span class="reference" style="border:1px solid green;">
                <a href="https://www.asdfasdf/apply/getHelp.html">Need Help?</a>
                <a href="https://www.asdfasdf/apply/termsOfUse.html">Terms of Use</a>
                <a href="">Site Design Copyright &copy; XYZ Company - All Rights Reserved</a>
            </span>
        </div>

    </body>
</html>

这能做到吗?

CSS 解决方案或 javascript 窗口大小调整事件都可以,只要可以完成即可。

编辑:这是当前结果的图片:

Current state

编辑:这是对我在这里找到的一个优秀片段的修改,并向http://tympanus.net/codrops/2010/07/16/slide-down-box-menu/ 致敬

最佳答案

确保主体不可滚动

body {
    overflow: hidden;
}

您的页脚是:

bottom: 0px;

制作你的内容div

bottom: 20px; // or whatever is perfect
overflow: auto;

页脚将固定,因为正文不可滚动而内容可滚动,所以位置应始终正确。

你可以改为让你的 body 滚动有边距:

margin-bottom: 20px;

还有你的页脚:

position: fixed;
bottom: 0px;

这样 body div 滚动而页脚是静态的。

关于javascript - 如何使 DIV 扩展以适应绝对定位的一个元素和固定定位的另一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538956/

相关文章:

javascript - 添加 InnerHTML 时 Jquery DatePicker 不起作用

html - 如何禁用 SVG 滚动?

javascript - 替换 JavaScript 中的字符

jquery - 如何根据给定的结构构建 HTML

javascript - 获取文本的 JavaScript 变量并将它们用作隐藏输入

javascript - 使用 jQuery 添加位于我的项目中的图像

javascript - HTML 样式表更改错误

javascript - 错误 : "angular was used before it was defined" but online editors able to output the result

javascript .on() 函数不工作

javascript - IF 语句的多个 OR 条件