2014-06-05 229 views
1

基本上我试图将两个垂直按钮对齐我的网站的右侧。当我点击时,它会显示另一个帮助我jQuery动画的div。这里是我的html代码:垂直对齐按钮

<div id="rightBar"> 
     <input type='button' id='hideshow' value='Show/Hide' /> 
     <input type='button' id='hideshowBookmark' value='Bookmark' /> 
     <div id='rightContent'> 
      <table style="width:100%; text-align: center;padding: 2px;"> 
       <th colspan="2"><b>Get Directions</b></th> 
       <tr><td>&nbsp;</td></tr> 
       <tr> 
        <td width="120px"><label class="title">Get Location: </label></td> 
        <td><input id="txtFrom" type="text" class="textInput" placeholder="Outram" />&nbsp;<img id="AutoFrom" src="img/map.jpg" class="button_search" onclick="GetDataFrom();"/>&nbsp;<label class="title">To</label>&nbsp; <input id="txtTo" type="text" class="textInput" placeholder="Hougang mall" />&nbsp;<img id="AutoTo" src="img/map.jpg" class="button_search" onclick="GetDataTo();" /></td> 
       </tr> 
      </table> 
      <div id="divDirectionResults" style="display: none; border:1px solid black; opacity: 0.8; filter: progid:DXImageTransform.Microsoft.Alpha(opacity=80); background:#000000; width:auto; height:190px;"></div> 
      <table style="width:100%; text-align: center;padding: 2px;"> 
       <tr> 
        <td colspan="2" style="padding-left:45px;"><input id="CbAvoid" type="checkbox" checked="checked" />&nbsp; Avoid ERP</td> 
       </tr> 
       <tr><td>&nbsp;</td></tr> 
       <tr> 
        <td style="width: 35%;">&nbsp;</td> 
        <td><input value="Get Routing" type="button" onClick="getDirections();" style="width: 109px; height: 23px;font-size:7pt;font-weight:bold;"/><input type="button" onclick="resetSearchField();" style="width: 58px; height: 23px;font-size: 7pt;font-weight:bold;" value="Reset"/></td> 
       </tr> 
      </table> 
      <div id="divComputedDirection"> 
      </div> 
     </div> 
    </div> 

而且我的JavaScript:

 $(document).ready(function() { 
    $('#hideshow').click(function() { 
     $('#rightContent').animate({ 
      width: 'toggle' 
     }, 1000 
       ); 
    }); 

    $('#hideshow').val('H\ni\nd\ne\n/\nS\nh\no\nw'); 
    $('#hideshowBookmark').val('B\no\no\no\nk\nm\na\nr\nk'); 
}); 

而CSS:

#rightBar 
{ 
    float: right; 
} 

#hideshow 
{ 
    float: right; 
    padding: 10px; 
    background: linear-gradient(#81BEF7,#2E9AFE); 
    border-radius: 3px; 
    font-weight: bold; 
    border: 0; 
} 
#hideshow:active 
{ 
    outline:none; 
} 
#hideshowBookmark 
{ 
    float: right; 
    padding: 10px; 
    background: linear-gradient(#81BEF7,#2E9AFE); 
    border-radius: 3px; 
    font-weight: bold; 
    border: 0; 
    margin-top: 170px; 
} 
#hideshowBookmark:active 
{ 
    outline:none; 
} 

#rightContent 
{ 
    width: 320px; 
    height: 440px; 
    background-color: white; 
    display: none; 
    margin-right: 40px; 
    border-radius: 5px; 
    border: 1px solid grey; 
} 

不知何故,DIV中的元素向下移动,当我点击第一个按钮。而我的第二个按钮没有对齐到右侧。

这里是我的小提琴:Fiddle Link

我不知道为什么会这样呢?是因为我的CSS还是我放置html组件的方式?

在此先感谢。

回答

1

只需删除顶部边距并为元素添加清除。 Clear做它所说的,它从流中清除漂浮的元素。

只要改变你的CSS这样的:

#hideshowBookmark 
{ 
    float: right; 
    padding: 10px; 
    background: linear-gradient(#81BEF7,#2E9AFE); 
    border-radius: 3px; 
    font-weight: bold; 
    border: 0; 
    clear:both; 
} 

下面是更新Fiddle

这里有一些关于Clear的好文档。

+0

它的工作原理!谢谢您的帮助! –

+0

只是想知道为什么当我滑出div时,所有组件都在移动?你知道如何让自己的位置留下吗? –

+0

这不完全是幻灯片。你正在使用'width'切换,所以里面的内容适应div扩展时的当前宽度。在StackOverflow中搜索解决方案,如果你没有找到它,你应该打开一个新的问题。 – Cthulhu

1

更改CSS这个类以下内容:

#hideshowBookmark 
{ 
    float: right; 
    padding: 10px; 
    background: linear-gradient(#81BEF7,#2E9AFE); 
    border-radius: 3px; 
    font-weight: bold; 
    border: 0; 
    margin-top: 170px; 
    position: absolute; 
    right: 7px; 
} 

fiddle

+0

它的工作原理!谢谢。顺便说一下,你添加了什么?你能向我解释一下吗? –

+0

嗯,我不是很擅长解释,但我会试一试。绝对位置可以将元素精确放置在您想要的位置。所以我改变它的位置为绝对,并与权利到7像素我把它放在右侧:) –

+0

然而,与'位置:绝对;'你完全从流中删除你的元素('浮动'是什么都不做) ,所以创建布局可能会有点棘手。元素的位置对于父元素是绝对的,所以如果你改变了父元素,你也会改变这个孩子。另外,如果你有20个元素,把所有东西放在绝对位置不是一个好习惯,最好让它们保持漂浮状态。 – Cthulhu

2

检查出这个fiddle

您需要的CSS添加到div rightBar这个CSS

#rightBar 
{ 
    float: right; 
    position:relative; 
} 

#hideshowBookmark

#hideshowBookmark 
{ 
    float: right; 
    padding: 10px; 
    background: linear-gradient(#81BEF7,#2E9AFE); 
    border-radius: 3px; 
    font-weight: bold; 
    border: 0; 
    margin-top: 170px; 
    position:absolute; 
    right:0; 
} 
+0

当然,谢谢。有用! –

+0

只是想知道为什么当我滑出div时,所有组件都在移动?你知道如何让自己的位置留下吗? –

1

我通过将rightContent div移出rightBar div来修复它,从按钮中删除浮动属性并将浮动属性添加到rightContent div。

另请参见JsFiddle

+0

只是想知道为什么当我滑出div时,所有组件都在移动?你知道如何让自己的位置留下吗? –

+0

这是因为宽度在变化,组件使用的空间已经有了。用这种方法你不能让他们留在这个方法中。 – frieder

+0

你有什么想法解决这个问题吗? –