简体   繁体   English

鼠标悬停和鼠标移动时显示隐藏div

[英]show hide div on mouseover and mouseout

<script type="text/javascript">
function show_sidebar()
{
document.getElementById('sidebar').style.visibility="visible";
}

function hide_sidebar()
{
document.getElementById('sidebar').style.visibility="hidden";
}
</script>

<img src="images/cart.jpg" width="80px" height="30px" onMouseOver="show_sidebar()"     onMouseOut="hide_sidebar()">

<div id="sidebar">some thing</div>

This is my code for showing and hiding sidebar div on mouseover and mouseout. 这是我在mouseover和mouseout上显示和隐藏侧栏div的代码。 Its working but i want is when i have mouseover on image, sidebar div is shown and i want sidebar div to be shown also when mouse is over sidebar. 它的工作,但我想要的是当我有鼠标悬停在图像上,侧边栏div显示,我希望侧栏div也显示当鼠标在侧边栏上。 How can u do it. 你怎么能这样做

If you can wrap them in a common parent it is as simple with just CSS, no JavaScript is needed. 如果你可以将它们包装在一个共同的父级中,那么只需要CSS就可以了,不需要JavaScript。

CSS: CSS:

#sidebar {
    display: none;
}
.cart:hover #sidebar {
    display:block;
}

HTML: HTML:

<div class="cart">
    <img src="images/cart.jpg" width="80px" height="30px" />
    <div id="sidebar">some thing</div>
</div>

Example: 例:

JSFiddle 的jsfiddle

It can be done without wrapping, but you have to make sure that the image and div overlap so the cursor can move into the div. 它可以在不包装的情况下完成,但您必须确保图像和div重叠,以便光标可以移动到div中。 Wrapping it avoids that problem. 包装它避免了这个问题。

Also note, not everyone uses a mouse. 另请注意,并非每个人都使用鼠标。

Move the eventhandlers to a wrapper div to accomplish what you want. 将事件处理程序移动到包装器div以完成您想要的操作。

<div id="wrapper" onMouseOver="show_sidebar()" onMouseOut="hide_sidebar()">
  <img src="images/cart.jpg" width="80px" height="30px">
  <div id="sidebar">some thing</div>
</div>

use it with css, it simplier 使用它与CSS,它更简单

 <style>
   #sidebar{
       display : none;
   }
   img:hover ~ #sidebar {
       display : block;
   }
    #sidebar:hover {
       display : block;
   }
</style>
<img src="images/cart.jpg" width="80px" height="30px" >

<div id="sidebar">some thing</div>

With delay : 延迟:

<style>
#sidebar{

    opacity : 0;
}
img:hover ~ #sidebar {
    opacity : 1;
}
 #sidebar:hover {
    opacity : 1;
}
 #sidebar:not(hover) {
    animation-delay:2s;
    -webkit-animation-delay:2s;
    -webkit-transition: opacity 1s ease-out;
    opacity : 0;
}
</style>

Here it is: 这里是:

http://jsfiddle.net/luckmattos/CV9uX/ http://jsfiddle.net/luckmattos/CV9uX/

with Jquery 与Jquery

$('#img').hide(); 

$('#sidebar').mouseover(function () {
      $('#img').show();      
});
$('#sidebar').mouseout(function () {
      $('#img').hide();      
});

HTML HTML

<a id="sidebar">Show on Over</a>

<div class="img"><img src="http://www.highsnobiety.com/files/2013/05/lamborghini-egoista-concept-car-9.jpg" width="250px" id="img">
    </div>

Invisible elements don't have mouse events. 不可见元素没有鼠标事件。 Try this: 试试这个:

<script type="text/javascript">
function show_sidebar()
{
document.getElementById('sidebar').style.opacity = 1;
}

function hide_sidebar()
{
document.getElementById('sidebar').style.opacity = 0;
}
</script>

<img src="images/cart.jpg" width="80px" height="30px" onMouseOver="show_sidebar()" onMouseOut="hide_sidebar()">
<div id="sidebar" style="opacity: 0;" onMouseOver="show_sidebar()">some thing</div>

I think this is the best and easy method working with latest jquery version. 我认为这是使用最新jquery版本的最好和最简单的方法。

<script>
$(document).ready(function(){
$("#div2").hide();
$("#div1").mouseover(function(){
$("#div2").fadeIn();
});
$("#div1").mouseout(function(){
$("#div2").fadeOut();
});
});

</script>

<div id="div1">Move the mouse pointer over this paragraph.</div>
<div id="div2" >div2.</div>

try 尝试

<script type="text/javascript">
function show_sidebar()
{
document.getElementById('sidebar').style.display="block";
}

function hide_sidebar()
{
document.getElementById('sidebar').style.display="none";
}
</script>

<img src="images/cart.jpg" width="80px" height="30px" onMouseOver="show_sidebar()"     onMouseOut="hide_sidebar()">

<div id="sidebar">some thing</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM