如何自动对齐父div中存在的两个div

时间:2017-08-22 09:39:04

标签: jquery html css

我在父div中有两个div。 div 1设置为75%宽度,第二个div设置为25%。用户可以选择在我们的应用程序中使用主题来切换div 1和div 2的位置。



.docTOCDiv li.iconPage:hover {
    text-decoration: none;
}
div.docPage {
    background-color: #ffffff;
    background-image: none;
    border-color: #ffffff;
    border-style: solid;
    border-width: 0;
}
.docToggleFull {
    background-color: #002a5c;
    background-image: url("/site_assets/images/doc_style/murraystate/murraystate_banner.png");
    background-position: right top;
    background-repeat: no-repeat;
    padding-left: 0.5em;
}
div.hideTOC {
    margin-top: 1em;
}
.docTOCDiv {
    background: transparent none repeat scroll 0 0;
    border: medium none;
    left: 0.2em;
    margin: 8em 0 0 0.2em;
    padding: 0.5em;
    position: absolute;
    top: 0;
}
h3.docPageH3 {
    border-bottom: medium none;
    color: #002a5c;
    font-size: 18px;
    font-weight: normal;
    line-height: 2em;
    margin: 0;
    padding-left: 0.5em;
}
div.docPageCorner {
    line-height: 0;
}
.docTOCDiv li.iconPage a:link, .docTOCDiv li.iconPage a:active, .docTOCDiv li.iconPage a:visited {
    color: #ffffff;
}
.docToggle {
    background-color: #002a5c;
    background-image: url("/site_assets/images/doc_style/murraystate/murraystate_banner.png");
    background-position: right top;
    background-repeat: no-repeat;
    padding-left: 18em;
}
div.docSection {
    background-color: #ffffff;
    border-color: #1e5127;
    border-style: solid;
    border-width: 0;
    margin-bottom: 1.2em;
    margin-top: 0.2em;
    padding: 0.5em;
}
.docTOCDiv li.iconSection a:link, .docTOCDiv li.iconSection a:active, .docTOCDiv li.iconSection a:visited {
    color: #ffffff;
}
div.docPageBottom {
    background-image: none;
}
.docTOCDiv ul, .docTOCDiv ul:hover {
    background: #6699cc none repeat scroll 0 0;
}
.docTOCDiv li.iconSection:hover, .docTOCDiv li.iconSection a:hover {
    color: #93b1d5;
    text-decoration: none;
}
div.docDiv {
    background-color: #002a5c;
    background-image: none;
}
div.docPageBottomCorner {
    background-image: none;
}
.docTOCDiv ul:hover {
    background-image: none;
}
div.editPage {
    margin: 1em;
}
.docTOCDiv ul.sectionLevel, .docTOCDiv ul.sectionLevel:hover {
    background: #6699cc none repeat scroll 0 0;
    color: #93b1d5;
}
div.docSection p {
    color: #000000;
    font-size: 11px;
}
div.pagination {
    padding: 1em;
}
h2.docDivH2 {
    background-color: #ffcc00;
    border: 3px solid #fff;
    color: #000033;
    font-size: 21px;
    font-weight: bold;
    line-height: 120%;
    margin: 0;
    padding: 0.5em;
}
.docTOCDiv div.pageTOCBottom {
    border-color: #3b6289;
    line-height: 0;
}
div.submitDivLeft {
    border: medium none;
    margin-top: 6em;
}
div.ifLeftTOC {
    float: left;
    margin-top: -0.4em;
}
.docTOCDiv h5 {
    background-color: #6699cc;
    border: medium none;
    color: #fff;
    font-size: 13px;
    font-weight: bold;
    margin: 1.1em 0 0;
    padding: 0.5em;
}

<div id="doc" class="contentDiv" style="width: 105.4%;">
    <!-- Document Section starts here -->

    <div class="sectionDiv docToggle" id="docToggleDiv">
        <!-- Doc Table of Content starts -->

        <div id="docTOC" class="docTOCDiv" style="height: 440px;">
            <div class="submitDivLeftFloat clearfix">
                <div id="toggle-toc" style="margin-top: 0px;">
                    <a title="Hide Page List" class="iconButton hide">Hide</a>
                </div>
            </div>

            <h5>Page List</h5>
            <div id="allPages" class="allPages" style="height: 315px; overflow-y: auto; overflow-x: hidden;">
            
            <div class="">
            <ul id="sections-toc-ul-47631849">
              <li class="iconPage">
                <a href="/doc/10672558/47631849" class="pageTitle_47631849">DYO Page</a></li>
              
            </ul>
            </div>
            
        </div>
         
        <table><tbody><tr>
        <td><div class="button" id="add_page" style="margin-top: 10px;"><a tabindex="0" title="Add new page" class="iconOnlyButton add">&nbsp;</a></div></td>
        <td><div class="button" id="manage_pages" style="margin-left: 5px; margin-top: 10px;"><a tabindex="0" class="iconButton edit">Manage Pages</a></div></td>
        </tr></tbody></table>
          
        </div>
        <!-- Doc Table of Content ends -->

        <div id="pagesArea">

            <div class="button pull-right" id="manage_content_button_47631849">
                <a tabindex="0" class="iconButton edit">Manage Content</a>
            </div>
            <div class="floatLeft">
            <h2 class="docDivH2 pageTitle_47631849">DYO Page</h2>
            </div>

            <a tabindex="0" id="pageTitleEdit_47631849">edit title</a>
            <div id="page_body_47631849" class="docPage clear">
                <div class="docPageSider">
                    <div class="grayMessageDiv">
                        <div class="noSectionsMessage">
                        Currently, there is no content on this page.To add content to this page, select a content type:<div class="newSectionDropdown_47631849 button dropdown" id=""></div>							<a class="iconOnlyButton add" title="Add new content">&nbsp;</a>
                    </div>
                    </div>
                </div>
            </div>
    </div>
    <!-- Document Section ends here -->
    </div>
&#13;
&#13;
&#13;

由于每个主题都有很多css文件,我们无法更改或更新每个css属性。我们无法完全垂直对齐div。所以想到使用Jquery并设置div的高度。 #docToggleDiv 是主div。 #docTOC 应始终为 #docToggleDiv 的高度。

以下是我到目前为止使用Jquery尝试的内容:

&#13;
&#13;
<script>
jQuery.noConflict();
jQuery(document).ready(function() {

    var sectionDivHeight = jQuery('#docToggleDiv').height() ;
    var docTOCDivHeight = jQuery('#docTOC').height() ;

    // since doc view page doesn't has the right side quick links, we can increase the size of the container a little more.
    // The right side edge of the container is inline with the author or creator name displayed on the right top corner
    jQuery('.contentDiv').css("width", "105.4%");

    if ( sectionDivHeight < docTOCDivHeight ) {

        // in rest all test cases, the table of contents list height should adhere to the height of the section div
        // This is required when the TOC is on the left side and the hide button is way off the top border
        jQuery('#docTOC').css("height", sectionDivHeight );
        jQuery('#toggle-toc').css("margin-top", "0px");

        // this is to make the TOC plus signs appear within the viewable area in the TOC
        //jQuery('#docTOC').find('li.iconPage').css("margin", "0 0.7em 0 0");

        // The 125 which I have set is an average value after checking all the docs in the ticket RT119492
        jQuery('#allPages').css("height", sectionDivHeight - 125 );
        jQuery('#allPages').css("overflow-y", "auto");
        jQuery('#allPages').css("overflow-x", "hidden");

        // The add page and Manage Pages button are touching the Scrollbar
        jQuery('#add_page').css("margin-top", "10px");
        jQuery('#manage_pages').css("margin-left", "5px");
        jQuery('#manage_pages').css("margin-top", "10px");

        // this is to make the TOC plus signs appear within the viewable area in the TOC
        jQuery('#allPages').find('div.TOCcollapsed').css("width", "14em");

    }
    }); // END of Jquery Ready Block
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

在这里添加一些图片。

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您只需要获取左侧元素的顶部位置,然后将其作为顶部值应用于右侧元素:

jQuery(document).ready(function() {

  var docTOCtop = $('#docTOC').position().top;
  
  $('#docToggleDiv').css({"top": docTOCtop});

 });
.container {
  height: 600px;
  background-color: Tomato;
  position: relative;
  
}

#docTOC {
  position: absolute;
  top: 100px;
  left: 0;
  width: 25%;
  height: 500px;
  background-color: wheat;
}

#docToggleDiv {
  position: absolute;
  right: 0;
  width: 75%;
  height: 500px;
  background-color: aquamarine;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div id="docTOC"></div>
  <div id="docToggleDiv"></div>
</div>