javascript - CSS "Suggestions search box"位置不当

标签 javascript html css

我为一个网站创建了一个搜索功能,此外我还制作了一个“建议框”,可以根据他们输入的内容下拉。 (就像 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/

相关文章:

html - 具有静态宽度的内联 div

jquery - SB Admin 2 侧边栏菜单在加载时扩展

CSS 背景颜色和 png 图像

javascript - 使用 NVD3.js 的 <circle> 属性 cx ="NaN"的值无效

javascript - 在 MongoDB 中将字符串转换为 ISODate

html - 将两个按钮对齐到一行的中心

javascript - 为什么我必须将所有脚本放入 jquery mobile 中的 index.html

javascript从aws lambda查询dynamodb中的最后一项

javascript - 如何在javascript中获取进度条的x坐标

HTML 正文背景图片在 iOS 5 中不起作用