简体   繁体   English

如何通过单击或悬停在第一个 div 元素旁边水平显示另一个 div 元素? 在第一个 div

[英]how to display another div element next to the first div element horizontally by clicking or hovering? over the first div

单击或悬停在 div 上时,它应该显示旁边的另一个 div,就像选项一样,它不能像下拉一样垂直,它必须水平继续

There are multiple ways to do it, but aligning div horizontally is done using css, it has nothing to do with hover events.有多种方法可以做到,但是水平对齐 div 是使用 css 完成的,它与悬停事件无关。 Try to understand the below code.试着理解下面的代码。

I am showing in 2 ways here我在这里以两种方式展示

  • (div1,div2) using css (div1,div2) 使用 css
  • (div3,div4) using javascript (div3,div4) 使用 javascript

Link: https://github.com/helloritesh000/how-to-display-another-div-element-next-to-the-first-div-element-horizontally-by链接: https : //github.com/helloritesh000/how-to-display-another-div-element-next-to-the-first-div-element-horizo​​ntally-by

<!doctype html>
<html>
<head>
<style type="text/css">
.div{
    width: 200px;
    height: 30px;
    float: left;
}

.parent1 .div1{
    background-color: red;
}

.div2{
    background-color: cyan;
    display: none;
}

.parent1 .div:hover + div{
    display: block;
}


</style>
</head>
<body>

<div class="parent1">
    <div class="div div1">
    </div>
    <div class="div div2">
    </div>
</div>
<div style="clear: both;"></div>
<div class="parent2">
    <div class="div div3" id="div3" onmouseover="div4.style.visibility='visible'" onmouseout="div4.style.visibility='hidden'" style="background-color: blue;">
    </div>
    <div class="div div4" id="div4" style="background-color: #ccc;">
    </div>
</div>
<script>
div4.style.visibility='hidden'
</script>
</body>
</html>

展示<div>当悬停在另一个 div 及其子项上时</div><div id="text_translate"><p>我有一个 HTML div,里面有孩子。 我有一个附加到 div 的 jQuery 鼠标悬停事件。 鼠标悬停时,我显示另一个 div,鼠标悬停时,我隐藏它。</p><p> 但是,当我将鼠标悬停在“premiumlink”div 上时,一切正常,但是当我将鼠标移到 div 的一个子项上时,有条件显示的 div 会隐藏,但随后 jQuery 发现父 div 仍在悬停,所以它再次显示它。 然后,如果我将 cursor 移回父 div,则该 div 被隐藏然后再次显示。</p><p> 我怎样才能让鼠标悬停和鼠标悬停适用于所有孩子,而不是这个跳跃的 state?</p><p> 这是我的 HTML</p><pre> &lt;div class="platinumlevel" id="premiumlink"&gt; &lt;h1&gt; &lt;img src="~/Content/Images/colorworld.png" alt="Logo" class="eventimage" /&gt; &lt;a href="@Url.Action("Exhibitor1Attendee", "Home")" class="landing"&gt;Company Name&lt;/a&gt; &lt;/h1&gt; &lt;div id="demopreview" style="display: none;"&gt; I should be displayed when "premiumlink" and all it's children are mouseovered &lt;/div&gt; &lt;/div&gt;</pre><p> JS</p><pre> $("#premiumlink").mouseover(function () { $('#demopreview').show(1000); }).mouseout(function () { $('#demopreview').hide(1000); });</pre></div> - Display <div> when hovering over another div and its children

暂无
暂无

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

相关问题 如何通过将鼠标悬停在另一个元素上来更改div的类 - How to change class of a div by hovering on another element 如何根据另一个“ div”元素水平对齐一个“ div”元素 - How to align a 'div' element horizontally according to another 'div' element 将鼠标悬停在另一个div上时如何显示div - How to display a div when hovering over another div 单击后如何将div中的元素移动到第一个位置 - How to move the element in div to first position after clicking to it 展示<div>当悬停在另一个 div 及其子项上时</div><div id="text_translate"><p>我有一个 HTML div,里面有孩子。 我有一个附加到 div 的 jQuery 鼠标悬停事件。 鼠标悬停时,我显示另一个 div,鼠标悬停时,我隐藏它。</p><p> 但是,当我将鼠标悬停在“premiumlink”div 上时,一切正常,但是当我将鼠标移到 div 的一个子项上时,有条件显示的 div 会隐藏,但随后 jQuery 发现父 div 仍在悬停,所以它再次显示它。 然后,如果我将 cursor 移回父 div,则该 div 被隐藏然后再次显示。</p><p> 我怎样才能让鼠标悬停和鼠标悬停适用于所有孩子,而不是这个跳跃的 state?</p><p> 这是我的 HTML</p><pre> &lt;div class="platinumlevel" id="premiumlink"&gt; &lt;h1&gt; &lt;img src="~/Content/Images/colorworld.png" alt="Logo" class="eventimage" /&gt; &lt;a href="@Url.Action("Exhibitor1Attendee", "Home")" class="landing"&gt;Company Name&lt;/a&gt; &lt;/h1&gt; &lt;div id="demopreview" style="display: none;"&gt; I should be displayed when "premiumlink" and all it's children are mouseovered &lt;/div&gt; &lt;/div&gt;</pre><p> JS</p><pre> $("#premiumlink").mouseover(function () { $('#demopreview').show(1000); }).mouseout(function () { $('#demopreview').hide(1000); });</pre></div> - Display <div> when hovering over another div and its children 如何用div包装除第一个元素之外的所有元素 - How to wrap all but first element with div 如何使用 JavaScript 将 div 作为第一个元素插入容器中 - How to insert a div as the first element in a container with JavaScript 切换div的第一个元素的类 - Toggle class of first element of a div 在div之前,但在第一个元素之后 - Prepend to div, but after first element 如何使用jQuery删除除第一个div以外的克隆元素div - How to remove cloned element div except first div using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM