[英]What is the best way to show icon/button only when hover on an object ?
I have a cover-image like this 我有这样的封面图片
When the user hover on my image, I want to : 当用户将鼠标悬停在我的图片上时,我想:
CSS CSS
<style type="text/css">
#cover-img:hover{
opacity: .9;
}
#nav-upload-icon{
top: 10px;
left: 10px;
color: red;
z-index: 1000;
}
</style>
HTML HTML
<img id="cover-img" src="/material/img/profile-menu.png" height="130px">
<i id="nav-upload-icon" class="md md-camera hidden"></i>
JS JS
$("#cover-img").hover(function() {
$("#nav-upload-icon").removeClass( "hidden" );
});
I couldn't get it to behave what I expected to see. 我无法让它表现出我期望看到的。 What is the best way to implement something like that ? 实现类似的东西的最佳方法是什么?
There is no reason to use JavaScript if that is the actual html code, you can use the next sibling selector with hover. 如果这是实际的html代码,没有理由使用JavaScript,你可以使用悬停的下一个兄弟选择器。
#cover-img:hover + #nav-upload-icon,
#nav-upload-icon:hover {
visibility: visible;
}
#nav-upload-icon {
visibility : hidden;
}
bind mouseout event to remove add the hidden
class again 绑定mouseout事件以删除再次添加hidden
类
$("#cover-img").hover(function() {
$("#nav-upload-icon").removeClass("hidden");
});
$("#cover-img").mouseout(function() {
$("#nav-upload-icon").addClass("hidden");
});
Give position absolute
to place it over the image 给位置absolute
放置在图像上
Go for @epascarello solution. 去找@epascarello解决方案。 It is the best. 这是最好的。
Your almost there. 你快到了。 Add a second anonymous function to add the class for mouseleave
添加第二个匿名函数以添加mouseleave
的类
$("#cover-img").hover(function() {
$("#nav-upload-icon").removeClass("hidden");
}, function() {
$("#nav-upload-icon").addClass("hidden");
});
According to hover() , you can pass in handlerIn/handlerOut
which are synonymous with mouseenter/mouseleave
根据hover() ,您可以传入handlerIn/handlerOut
,它们与handlerIn/handlerOut
mouseenter/mouseleave
同义
If you don't want to use javascript, wrap a div around the image. 如果您不想使用javascript,请在图像周围包裹div。
<div class="image-wrap">
<img > <-- your super cool large image
<img class="upload"> <- your super cool icon and stuff absolutely positioned with 0 transparency
</div>
Then in the css you go something like this 然后在CSS中你会发生这样的事情
div.image-wrap:hover img.upload {
opacity:0.9
}
Don't bother with javascript, it's 2015 不要打扰javascript,这是2015年
This can be achieved without any JS. 这可以在没有任何JS的情况下实现。 Using the adjacent selector you can show the icon when #cover-img
is hovered on. 使用相邻的选择器,您可以在#cover-img
悬停时显示图标。
#cover-img:hover + img {
opacity: 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.