简体   繁体   English

动态设置div的位置

[英]Set position of div dynamically

I have to create a search result box. 我必须创建一个搜索结果框。 Whenever a user type something in the search text box the list of result will be populated in the search result box just underneath the textbox. 每当用户在搜索文本框中键入内容时,结果列表将填充在文本框下方的搜索结果框中。 I am able to do this where there is only one instance of the search box(the caller is a single textbox). 我可以在只有一个搜索框实例的情况下执行此操作(调用方是单个文本框)。 The problem occurs when the same searchbox is used for multiple text boxes. 当同一搜索框用于多个文本框时,会发生此问题。 I want it to adjust its position according to the caller textbox. 我希望它根据呼叫者文本框调整其位置。 Here is the code: 这是代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .searchDiv {
            border: 1px solid red;
            width:150px;
            height:100px;
            visibility: hidden;
        }
    </style>
    <script>
        showSearchBox = function (_element) {
            var _div = document.getElementById('divSearch');
            _div.style.visibility = 'visible';
        }
        hideSearchBox = function () {           
            var _div = document.getElementById('divSearch');
            _div.style.visibility = 'hidden';
        }
    </script>
</head>
<body>
    <table border="0" style="width: 50%">
        <tbody>
            <tr>
                <td style="width: 50%">
                    <input type="text" id="txtFirstName" name="txtFirstName" onfocus="showSearchBox(this)" onblur="hideSearchBox()" />
                </td>
                <td style="width: 50%">
                    <input type="text" id="txtLastName" name="txtLastName" onfocus="showSearchBox(this)" onblur="hideSearchBox()"/>
                </td>
            </tr><tr>
                <td colspan="2" style="width: 100%">
                    This is just a placeholder text.
                </td>
            </tr>
            <tr>
                <td colspan="2" style="width: 100%">
                    <div id="divSearch" class="searchDiv"></div>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

What changes do I need to make in my code? 我需要在代码中进行哪些更改? You can also check out the jsfiddle 您也可以查看jsfiddle

add this to your show div method. 将此添加到您的show div方法。 fiddle . 摆弄 to make it more general you can use the top and left values from parent. 为了使其更通用,可以使用父级的顶部和左侧值。 Also add position:relative to your div 还要添加position:relative对于您的div

 var parentPositionLeft = _element.getBoundingClientRect().left;
 var parentPositionTop = _element.getBoundingClientRect().top;


            _div.style.left = parentPositionLeft+'px';
            _div.style.top = parentPositionTop+'px';
            console.log(_div.style.left);

I guess you have plain javascript, so in this case you gonna need to change your function showSearchBox function like this: 我想您有简单的javascript,因此在这种情况下,您需要像下面这样更改函数showSearchBox函数:

showSearchBox = function (_element) {
    var _div = document.getElementById('divSearch');

    var input = _element.getBoundingClientRect();

    _div.style.left = input.left + 'px';
    _div.style.top = input.top + 'px';
    _div.style.visibility = 'visible';
}

and add this rule to you css class .searchDiv : 并将此规则添加到您的CSS类.searchDiv

position: relative;

Here is a fiddle 这是一个小提琴

You can use jQuery to modify the position of .searchDiv. 您可以使用jQuery修改.searchDiv的位置。

$(".searchDiv").css({top: 55, left: 220, position:'absolute'});

Try this: https://jsfiddle.net/ybdesire/yy751fn6/ 试试这个: https : //jsfiddle.net/ybdesire/yy751fn6/

首先,您必须找出放置它的文本框的确切位置,然后将该Div绝对正确地放置在该位置的上下位置。

设置 position<div> 在运行时</div><div id="text_translate"><p>在鼠标悬停时,我想将“imgbox”移动到特定的绝对 position(这可能会将其移动到“i”图像的顶部)。</p><p> 第二个图像(“newimg”)加载,因此该部分工作(包括再次将其隐藏在“onmouseout”中)但它显示在所有内容下方(如 HTML 代码中)。 似乎设置imgbox.style.left和imgbox.style.top没有做任何事情。 如果我改为设置“边距”,则图像将显示在右侧 200 像素处,并从原来的位置向下 200 像素(但仍低于其他所有内容)。</p><p> 我错过了什么? 如何在运行时使用常规 Javascript 移动“imgbox”(请不要 JQuery?)? </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> function onHoverIn(url) { var imgbox = document.getElementById("imgbox"); imgbox.style.visibility='visible'; var newimg = document.createElement("img"); newimg.src = url; var oldimg = document.getElementById("i"); /*if(oldimg.addEventListener){ //Removed so the snippet'll run oldimg.addEventListener('mouseout',onHoverOut,false); } else { oldimg.attachEvent('onmouseout',onHoverOut); }*/ imgbox.innerHTML=''; imgbox.appendChild(newimg); imgbox.style.left = '200px'; imgbox.style.top = '200px'; //imgbox.style.marginLeft = '200px'; //imgbox.style.marginTop = '200px'; }</pre><pre class="snippet-code-css lang-css prettyprint-override"> #imgbox { position: absolute; border: 1px solid #999; background: #FFFFFF; filter: Alpha(Opacity=100); visibility: hidden; z-index: 50; overflow: hidden; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;img id="i" src="https://i.pinimg.com/originals/53/02/a4/5302a4c318139bf5753c433b1f4c6aa8.jpg" alt="DP" onmouseover="onHoverIn('https://i.pinimg.com/originals/b2/1b/07/b21b0738ea390fc56a4d3efe76ab88de.jpg')"&gt; &lt;p&gt;Long Teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;END TEEEEEEEEEEEXT&lt;/p&gt; &lt;div id="imgbox"&gt;&lt;/div&gt;</pre></div></div><p></p><p> 抱歉,图片很大,所以不知道效果如何。 我用 Firefox 85.0、Chrome 88、IE 和 Edge 测试了代码,只需双击 .html 文件,定位在其中任何一个都不能正常工作。</p></div> - Set position of <div> at runtime

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态设置弹出div的位置 - Set the position of pop up div dynamically 如何设置动态定位的div的正确position() - How to set correct position() of a div that is dynamically positioned 根据窗口大小浏览器动态设置div位置 - set div position dynamically depending window size browser 动态计算div位置 - dynamically calculate div position in 动态更改div的位置 - Change the position of a div dynamically 使用jquery动态设置位置 - Set position dynamically with jquery 设置可拖动div的位置 - Set position of draggable div 设置 position<div> 在运行时</div><div id="text_translate"><p>在鼠标悬停时,我想将“imgbox”移动到特定的绝对 position(这可能会将其移动到“i”图像的顶部)。</p><p> 第二个图像(“newimg”)加载,因此该部分工作(包括再次将其隐藏在“onmouseout”中)但它显示在所有内容下方(如 HTML 代码中)。 似乎设置imgbox.style.left和imgbox.style.top没有做任何事情。 如果我改为设置“边距”,则图像将显示在右侧 200 像素处,并从原来的位置向下 200 像素(但仍低于其他所有内容)。</p><p> 我错过了什么? 如何在运行时使用常规 Javascript 移动“imgbox”(请不要 JQuery?)? </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> function onHoverIn(url) { var imgbox = document.getElementById("imgbox"); imgbox.style.visibility='visible'; var newimg = document.createElement("img"); newimg.src = url; var oldimg = document.getElementById("i"); /*if(oldimg.addEventListener){ //Removed so the snippet'll run oldimg.addEventListener('mouseout',onHoverOut,false); } else { oldimg.attachEvent('onmouseout',onHoverOut); }*/ imgbox.innerHTML=''; imgbox.appendChild(newimg); imgbox.style.left = '200px'; imgbox.style.top = '200px'; //imgbox.style.marginLeft = '200px'; //imgbox.style.marginTop = '200px'; }</pre><pre class="snippet-code-css lang-css prettyprint-override"> #imgbox { position: absolute; border: 1px solid #999; background: #FFFFFF; filter: Alpha(Opacity=100); visibility: hidden; z-index: 50; overflow: hidden; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;img id="i" src="https://i.pinimg.com/originals/53/02/a4/5302a4c318139bf5753c433b1f4c6aa8.jpg" alt="DP" onmouseover="onHoverIn('https://i.pinimg.com/originals/b2/1b/07/b21b0738ea390fc56a4d3efe76ab88de.jpg')"&gt; &lt;p&gt;Long Teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;END TEEEEEEEEEEEXT&lt;/p&gt; &lt;div id="imgbox"&gt;&lt;/div&gt;</pre></div></div><p></p><p> 抱歉,图片很大,所以不知道效果如何。 我用 Firefox 85.0、Chrome 88、IE 和 Edge 测试了代码,只需双击 .html 文件,定位在其中任何一个都不能正常工作。</p></div> - Set position of <div> at runtime 动态取得最高div位置 - dynamically make top div position 动态设置div的高度 - Dynamically set height of div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM