简体   繁体   English

如何实现将鼠标悬停在图像效果上?

[英]How do I achieve a hover over image effect?

I'm trying to achieve the same mouse over image effect seen on wookmark and I'm assuming this is done with some jquery and css magic. 我正在尝试实现在wookmark上看到的相同的鼠标悬停图像效果,并且我假设这是通过一些jquery和CSS魔术完成的。 Are there any good tutorials that show how this is done? 有没有很好的教程来说明如何做到这一点?

Looking through the jquery docs it seems I would need to use hover like so: 浏览jQuery文档,看来我需要像这样使用悬停:

$("li.preview").hover(function(e){  
    // show and hide come css magic
});

Not sure where to go with the CSS portion of it... 不确定CSS的位置在哪里...

You can create a wrapper around your image(s) and inside that wrapper you can have the buttons you want to appear/disappear on hover. 您可以在图像周围创建包装器,并且在该包装器中可以具有要在悬停时显示/消失的按钮。 Then just show/hide those buttons when the image is hovered. 然后,当图像悬停时,只需显示/隐藏这些按钮即可。

HTML -- HTML-

<span class="image-wrapper">
    <span class="image-options"></span>
    <img src="..." />
</span>

CSS -- CSS-

.image-wrapper {
    position : relative;
    display  : inline-block;
}
.image-wrapper .image-options {
    position : absolute;
    top      : 0;
    right    : 0;
    left     : 0;
    height   : 25px;
    z-index  : 2;
    display  : none;
}
.image-wrapper:hover .image-options {
    display : block;
}

You can also use CSS3 to fade-in/out the options element: 您还可以使用CSS3淡入/淡出options元素:

.image-wrapper {
    position : relative;
    display  : inline-block;
}
.image-wrapper img {
    position : relative;
}
.image-wrapper .image-options {
    position : absolute;
    top      : 0;
    right    : 0;
    left     : 0;
    height   : 25px;
    z-index  : 2;
    background         : grey;
    border             : 1px solid black;
    opacity            : 0;
    filter             : alpha(opacity=0);
    -webkit-transition : opacity 0.25s linear;
    -moz-transition    : opacity 0.25s linear;
    -ms-transition     : opacity 0.25s linear;
    -o-transition      : opacity 0.25s linear;
    transition         : opacity 0.25s linear;
}
.image-wrapper:hover .image-options {
    opacity            : 1;
    filter             : alpha(opacity=100);
}​

Here is a demo using only CSS: http://jsfiddle.net/fJsJb/ 这是仅使用CSS的演示: http : //jsfiddle.net/fJsJb/

And of-course you can make the fade happen with JS: 当然,您可以使用JS进行淡入淡出:

$('.image-wrapper').on('mouseenter', function () {
    $(this).children('.image-options').stop().fadeIn(250);
}).on('mouseleave', function () {
    $(this).children('.image-options').stop().fadeOut(250);
});

Here is a demo using JS: http://jsfiddle.net/fJsJb/1/ 这是使用JS的演示: http : //jsfiddle.net/fJsJb/1/

UPDATE UPDATE

You can also create a slide animation by animating the top property like this: 您还可以通过动画top属性来创建幻灯片动画,如下所示:

.image-wrapper {
    position : relative;
    display  : inline-block;
    overflow : hidden;
}
.image-wrapper img {
    position : relative;
}
.image-wrapper .image-options {
    position : absolute;
    top      : -25px;
    right    : 0;
    left     : 0;
    height   : 25px;
    z-index  : 2;
    background         : grey;
    border             : 1px solid black;
    -webkit-transition : top 0.25s linear;
    -moz-transition    : top 0.25s linear;
    -ms-transition     : top 0.25s linear;
    -o-transition      : top 0.25s linear;
    transition         : top 0.25s linear;
}
.image-wrapper:hover .image-options {
    top : 0;
}​

Here is a demo: http://jsfiddle.net/fJsJb/2/ 这是一个演示: http : //jsfiddle.net/fJsJb/2/

You can do with css like this. 您可以像这样使用CSS。

<style>
.imageBox { float:left; width:100px; height:100px; position:relative; background:#000; overflow:hidden; }
.imageBox .imageActions { position:absolute; top:0; left:0; width:100%; height:40px; background:#999; display:none; }
.imageBox img { width:100%; }
.imageBox:hover .imageActions { display:block; }
</style>

<div class="imageBox">
    <div class="imageActions "></div>
    <img src=".." />
</div>

Demo 演示

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

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