简体   繁体   English

悬停在一个div上时如何显示更多div?

[英]How to make more divs appear when hovering over one div?

I am trying to work out how to show more divs when hovering over one div. 我正在尝试解决将鼠标悬停在一个div上时如何显示更多div的问题。

I know how to show changes of the same div when hovering over it but how can I show more divs when hovering over one div? 我知道将鼠标悬停在同一div上时如何显示其变化,但是将鼠标悬停在一个div上时如何显示更多div?

Take this as an example: 以此为例:

http://jsfiddle.net/j4LFD/ http://jsfiddle.net/j4LFD/

How can I make it so when you hover over the box another box appears next to it? 当您将鼠标悬停在该框上方时,我该如何做呢?

Im not sure if this can be done with css or needs javascript? 我不确定是否可以使用CSS或需要JavaScript?

Thanks! 谢谢!

James 詹姆士

You can do it with css like this: 您可以使用CSS来做到这一点:

.box , .appear{
 background: #151515;
 height: 100px;
 width: 100px;
    float:left;
}
.appear{
    background:red;
    display:none
}
.box:hover{
    background: #e6e6e6;
 height: 100px;
 width: 100px;  
}
.box:hover + .appear {
    display:block;  
}

Check this example http://jsfiddle.net/j4LFD/1/ 检查此示例http://jsfiddle.net/j4LFD/1/

To my knowledge it's not possible without Javascript, and I'd recommend using jQuery because it will make your life a lot easier. 据我所知,没有Java语言是不可能的,我建议使用jQuery,因为它会使您的生活变得更加轻松。 If you just want to show a div then you can do something similar to 如果您只想显示div,则可以执行以下操作

<div id="div1">First Div</div>
<div id="div2" style="display:none">Second Div</div>

Then in the javascript 然后在javascript中

$('#div1').mouseover(function(){
    $('#div2').show();
})
$('#div1').mouseout(function(){
    $('#div2').hide();
})

If you actually want to add it dive on hover then you can use the jquery .append() function (http://api.jquery.com/append/) 如果您确实想将其添加到悬停状态,则可以使用jquery .append()函数(http://api.jquery.com/append/)

Edit : Ok seeing sandeep's answer it clearly is possible in CSS, but still, this is how you'd do it in JS :-) 编辑 :好的,很清楚看到sandeep的答案在CSS中是可能的,但是,这仍然是您在JS中实现的方法:-)

CSS solution: Use a parent/container div. CSS解决方案:使用父/容器div。

http://jsfiddle.net/samliew/j4LFD/4/ http://jsfiddle.net/samliew/j4LFD/4/

You can also do this with vanilla Javascript (no JQuery) and without having your elements be adjacent siblings. 您也可以使用原始Javascript(不使用JQuery)来执行此操作,而不必让元素成为相邻的兄弟姐妹。 Example fiddle: http://jsfiddle.net/3nxfG/ 小提琴示例: http : //jsfiddle.net/3nxfG/

html/js: html / js:

<div id="box1" class="box"></div>
<div id="box2" class="box hidden"></div>
<script type="text/javascript">
var box1 = document.getElementById('box1');
box1.onmouseover = function() {
    var box2 = document.getElementById('box2');
    box2.style.visibility = 'visible';
};
box1.onmouseout = function() {
    var box2 = document.getElementById('box2');
    box2.style.visibility = 'hidden';
};
</script>

css: CSS:

.box {
 background: #151515;
 height: 100px;
 width: 100px;
 float: left;
}

.hidden {
    visibility: hidden;
}

您可以伪造它,使用该框隐藏第二个框,并使用边框作为框, http://jsfiddle.net/j4LFD/3/

暂无
暂无

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

相关问题 将鼠标悬停在SVG路径上时如何显示div? - How can I make a div appear when hovering over a SVG path? 如何在d3中为可视化制作一个函数而不是3个或更多函数,然后将鼠标悬停在节点上时添加突出显示 - How to make one function instead of 3 or more for a visualization in d3 to then add highlight when hovering over nodes 将鼠标悬停在元素上但不出现在鼠标指针上时如何显示div? - How to have a div show up when hovering over elements but not appear over the mouse pointer? 尝试将鼠标悬停在一个div上,以将类添加到多个div - Trying to add classes to multiple divs by hovering over one div 悬停在另一个元素上时,使一个元素出现并进行调整 - Make an element appear and make adjustments when hovering over another element 将鼠标悬停在 div 上时显示图像......但在 div 的 axa 网格中? - Show an image when hovering over a div... but in a x a grid of divs? 如何使用jQuery使多个div出现 - How to make more than one div appear using jQuery 将鼠标悬停在Javascript和/或CSS列表中的相应单词时,如何显示不同的图像? - How to make different images appear when hovering over corresponding words in list in Javascript and/or CSS? 将鼠标悬停在单个菜单项上时如何使整个菜单出现? - How to make the entire menu appear when hovering over a single menu item? 将鼠标悬停在图像上时,如何使信息文本出现并遵循 cursor? - How to make info text appear and follow the cursor when hovering over image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM