[英]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.