我为一个网站创建了一个搜索功能,此外我还制作了一个“建议框”,可以根据他们输入的内容下拉。 (就像 Google 所做的那样!)
所以我的问题是,盒子根据窗口大小 float 。如何将其固定在搜索字段的底部?
此处找到的实例:http://swissinstruments.com/searchresults.php (请参阅顶部搜索字段,输入“a”或“c”以获取一些建议。
现在我正在使用一些代码:
建议框风格
.suggestion_list
{
z-index:500;
background: white;
border: 1px solid #80B6E1;
padding: 4px;
float:none;
margin-top:5px;
width:191px;
position:fixed;
}
.suggestion_list ul
{
padding: 0;
margin: 0;
list-style-type: none;
}
.suggestion_list a
{
text-decoration: none;
color: navy;
}
.suggestion_list .selected
{
background: navy;
color: white;
}
.suggestion_list .selected a
{
color: white;
}
#autosuggest
{
display: none;
}
The SEARCH BOX Style
#search-form { float:right; padding:9px 0 0 0;}
#search-form fieldset {
border:none;
border:1px solid #0f58a2;
border-top:none;
background:#126dbe;
padding:0 12px 10px 13px;
float:right
}
#search-form input.text { width:190px; border:1px solid #d0e0e6; margin-right:3px; padding:2px 2px 3px 7px;}
#search-form input.submit { background:#007cff; width:59px; text-align:center; height:23px; line-height:23px; color:#fff; font-size:.77em; text-transform:uppercase; border:none; cursor:pointer;}
最后是用于定位 div 的 JavaScript 部分。我已将 x 设置为 -164,因为它似乎适用于大多数用户使用的分辨率。但一旦我收缩或拉伸(stretch)它,盒子就会错位。
/********************************************************
Position the dropdown div below the input text field.
********************************************************/
this.positionDiv = function()
{
var el = this.elem;
var x = -164;
var y = el.offsetHeight;
//Walk up the DOM and add up all of the offset positions.
while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
{
x += el.offsetLeft;
y += el.offsetTop;
el = el.offsetParent;
}
x += el.offsetLeft;
y += el.offsetTop;
this.div.style.left = x + 'px';
this.div.style.top = y + 'px';
};
有没有办法使用百分比而不是像素?
最佳答案
您可以使用 CSS 中的嵌套 div 和相对/绝对定位来完成此操作,您实际上不需要 javascript 来定位下拉列表。
假设搜索表单没有填充/边距,这样的东西应该是齐平的
#suggest_container { position:relative; overflow:visible; }
#autosuggest { position:absolute; top:<height-of-search-form>; left:0px; }
<div id="suggest_container">
<form id="search-form"></form>
<div id="autosuggest"></div>
</div>
您也可以始终使用 jquery UI 自动完成小部件,我之前已经使用过它很多次,而且它相当强大
关于javascript - CSS "Suggestions search box"位置不当,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10048818/