如何设计一个建议框,因此它不推开下面的元素?
我有此位置输入字段,使用MAPBOX的位置自动完成。发生的事情是,每次建议出现时,都会推动下面的其他项目。就像在这中一样 ScreenShot 。
<input className="w-full bg-gray-100 px-4 py-2 rounded-lg focus:outline-none"
{...location}
type="text"
></input>
{location.suggestions?.length > 0 && (
<div className="relative px-2 py-4">
{location.suggestions.map((suggestion, index) => {
return (
<p className="cursor-pointer"
key={index}
onClick={() => {
location.setValue(suggestion.place_name);
location.setSuggestions([]);
setValue("location", suggestion.place_name);
}}
>
{suggestion.place_name}
</p>
);
})}
</div>
)}
但是,它可以通过相对定位来期待下方的元素,但是,当我使用绝对定位时,它只是进入页面顶部,我猜是因为默认情况下,绝对是用top设置的, 0px
。我还有其他输入元素,因此我不确定如何估计我应该将顶部设置为顶部,或者甚至是对其进行硬编码的好习惯。您可以看到在此中使用绝对定位的外观 ScreenShot 。
I have this location input field with location autocomplete using mapbox. What's happening is that every time the suggestions appears, it pushes the other items below it. As in this
screenshot.
<input className="w-full bg-gray-100 px-4 py-2 rounded-lg focus:outline-none"
{...location}
type="text"
></input>
{location.suggestions?.length > 0 && (
<div className="relative px-2 py-4">
{location.suggestions.map((suggestion, index) => {
return (
<p className="cursor-pointer"
key={index}
onClick={() => {
location.setValue(suggestion.place_name);
location.setSuggestions([]);
setValue("location", suggestion.place_name);
}}
>
{suggestion.place_name}
</p>
);
})}
</div>
)}
It pushing the elements below it is to be expected with relative positioning, however, when I use absolute positioning, it just goes to the top of the page which I'm guessing is because by default, absolute is set with top: 0px
. I have other input elements on top of it so I'm not sure how to estimate how many pixels I should set the top to or if its even a good practice to hard-code it. You can see what it looks like using absolute positioning in this
screenshot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将容器的
position
设置为relative
,然后向下拉列表添加background-color
:您可以在这里找到更多信息:https:// css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
You need to set the
position
torelative
for the container and then add abackground-color
to the dropdown:You can find more infos here: https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/