调整图片大小时显示颜色叠加层(悬停) [英] Color overlay (hover) on resizing image

查看:279
本文介绍了调整图片大小时显示颜色叠加层(悬停)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图片上放置颜色叠加层。现在这很简单,但考虑到图像的边框在悬停时变化的事实,它不是(对我来说,因为我是CSS的新手)。我不知道这是否甚至可能与CSS,或者你将不得不使用JavaScript(或JQuery)?

I want to have a color overlay on an image. Now this is pretty simple, but given the fact the image's border changes on hover, it isn't (for me, because I'm fairly new to CSS). I don't know if this is even possible with CSS, or would you have to use JavaScript (or JQuery)?

到目前为止,我试过这个:put < div> 中的图片,在悬停时动态调整大小。这工作相当精细,但因为我要有一个黑暗的背景,有一个小问题。

So far, I've tried this: put the image in a <div>, which dynamically resizes on hover. This works pretty fine, but as I'm going to have a dark background, there is one little problem. There is a minimal 'border' of less than one pixel always around the image:

它几乎看不到,但它使我失望。我已经尝试使外部div更小,但是,这留下了我的中心线不褪色的图像和白色边框。

It's barely visible, but it annoys me. I've already tried to make the outside div smaller, but that leaves me with out-centered lines of not faded image and white border.

看看我的代码这里(没有什么特别,但只是为了防止): JSFiddle

Take a look at my code here (nothing special, but just in case): JSFiddle

有某种方法修复这个问题吗?我还没有学到很多Javascript,所以我宁愿用css而不是js,但如果没有其他方法...

Is there some way of fixing this? I haven't learnt a lot of Javascript yet, so I would rather do it with css than with js, but if there's no other way...

推荐答案

我更新了一些代码;请参阅 http://jsfiddle.net/GR8Vq/

I've updated some code; see http://jsfiddle.net/GR8Vq/:

* {
    margin: 0;
    padding: 0;
}

body {
    background: #000;
}

.imgholder {
    position: relative; // added
    margin: 5px;
    // background: #fff;
    float: left;
    height: 200px;
    // border-radius: 200px / 110px;
    transition: border-radius 0.3s ease-out;
}

.imgholder img,
.imgholder:after {
    border-radius: 200px / 110px;
    transition: 0.3s ease-out;
}

.imgholder:after {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: rgba(255,255,255,.3);
    opacity: 0;
}

.imgholder:hover:after { opacity: 1; }

.imgholder:hover{
    border-radius: 20px;
}

.imgholder:hover img,
.imgholder:hover:after {
    border-radius: 20px;
}

而不是图像载体上的border-radius,选择器在图像顶部添加图层。应该更好吗?

Instead of a border-radius on the image holder, I used a :after selector to add a layer on top of the image. Should be better like this?

这篇关于调整图片大小时显示颜色叠加层(悬停)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆