简体   繁体   English

图像上的圆圈悬停效果

[英]Circle on image hover efect

I want to have a picture that will look like this (background should be white - not transparent like in my pic): 我想要一张看起来像这样的照片(背景应该是白色的 - 不像我的照片那样透明):

在此输入图像描述

And when I hover with the mouse on that picture, circle should fully expand and show my original picture that will look like this: 当我用鼠标悬停在该图片上时,圆圈应该完全展开并显示我的原始图片,如下所示:

在此输入图像描述

In order to do that I try something like this: 为了做到这一点,我尝试这样的事情:

 #myimage{ background-color: #679b08; display: table-cell; vertical-align: middle; height: 50px; width: 50px; text-align: center; border-radius: 30px; color: #fff; text-transform: uppercase; font-size: 24px; font-family: Verdana; animation: expand-in .5s ease-out; animation-delay: 0s; animation-fill-mode: backwards; } #myimage:hover{ animation: expand-out .5s ease-out; animation-delay: 0s; animation-fill-mode: forwards; } @keyframes expand-in{ 0%{ width: 300px; height: 100px; border-radius: 5px; } 100%{ width: 120px; height: 120px; border-radius: 60px; } } @keyframes expand-out{ 0%{ width: 120px; height: 120px; border-radius: 60px; } 100%{ width: 600px; height: 200px; border-radius: 5px; } } 
 <div id ="myimage"> <img width="600" height="445" src="http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png"> </div> 

Here is also the jsfiddle link: https://jsfiddle.net/bq89efr8/ 这里也是jsfiddle链接: https ://jsfiddle.net/bq89efr8/

Problem is that my circle in behind picture and it won't properly center and show my picture, I would like to work this only on CSS and HTML (althou I don't mind using some jquery) if possible! 问题是我的圈子在后面的图片中并且它不能正确地居中并显示我的图片,我想只在CSS和HTML上工作(如果可能的话,我不介意使用一些jquery)!

Why use animations? 为什么要使用动画? You can do this using the CSS transition attribute on the image inside your #myimage div. 您可以使用#myimage div中图像上的CSS transition属性执行此操作。

 #myimage{ background-color: #fff; display: table-cell; vertical-align: middle; height: 50px; width: 50px; text-align: center; border-radius: 30px; color: #fff; text-transform: uppercase; font-size: 24px; font-family: Verdana; } #myimage img { transition: all .3s ease-in-out; border-radius: 100%; } #myimage img:hover { border-radius: 0%; } 
 <div id ="myimage"> <img width="600" height="445" src="http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png"> </div> 

You can use CSS. 你可以使用CSS。 It includes browsercompatible webkit. 它包括浏览器兼容的webkit。

img{
  border-radius: 50%;
  transition-duration: 0.25s;
  -webkit-transition-duration: 0.25s;;
  -moz-transition-duration: 0.25s;;
  -ms-transition-duration: 0.25s;;
}

img:hover{
 border-radius: 0%;
}

Use border-radius and :hover to get desired result. 使用border-radius和:hover来获得所需的结果。

 img{ border-radius: 50%; transition-duration: 0.25s; } img:hover{ border-radius: 0%; } 
 <img src="https://placeimg.com/64/64/animals?t=1513765419166"/> 

Edit Added transition-duration: 0.25s; 编辑已添加的transition-duration: 0.25s; to add the desired animation effect 添加所需的动画效果

Another possible solution would be to use onmouseover and onmouseout properties. 另一种可能的解决方案是使用onmouseoveronmouseout属性。

Basically, you trigger the event when ever you point your mouse on the image, or outside the image. 基本上,只要您将鼠标指向图像或图像外部,就会触发事件。 You can use that to change the border-radius of image. 您可以使用它来更改图像的边界半径。

  function normalImage(image){ image.style.borderRadius = "0%"; } function roundedImage(image){ image.style.borderRadius = "50%"; } 
 img{ border-radius: 50%; } 
  <img onmouseover="normalImage(this)" onmouseout="roundedImage(this)" width="600" height="445" src="http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png"> 

Here is a solution (with transitions). 是一个解决方案(带过渡)。

 img { border-radius: 50%; transition: border-radius 1s; } img:hover { border-radius: 0; } 
 <img src="http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png" width="600" height="445"> 

You could also use the following 您还可以使用以下内容

#myimage {
background-color: #679b08;
display: table-cell;
vertical-align: middle;
width: 320px;
height: 320px;
text-align: center;
border-radius: 300px;
color: #fff;
text-transform: uppercase;
font-size: 24px;
font-family: Verdana;
animation: expand-in .5s ease-in;
animation-delay: 0s;
background-image: url('http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png');
background-size: cover;
}

#myimage:hover {
animation: expand-out .5s ease-out;
animation-delay: 0s;
animation-fill-mode: forwards;
}

@keyframes expand-in {
0% {
    width: 600px;
    height: 500px;
    border-radius: 5px;
}
100% {
    width: 320px;
    height: 320px;
    border-radius: 300px;
}
}

@keyframes expand-out {
0% {
    width: 320px;
    height: 320px;
    border-radius: 300px;
}
100% {
    width: 600px;
    height: 500px;
    border-radius: 5px;
}
}

Then change your HTML to 然后将您的HTML更改为

  <div id ="myimage">
  </div>

You can use a pseudo element and the image as background so you avoid stretching the image with animation and you can easily control the position of the circle by adjusting top/right/bottom/left values . 您可以使用伪元素和图像作为背景,这样可以避免使用动画拉伸图像,并且可以通过调整顶部/右侧/底部/左侧值来轻松控制圆的位置。

 #myimage { background-color: #679b08; position: relative; height: 445px; width: 600px; margin: auto; } #myimage:before { content: ""; position: absolute; top: 50px; bottom: 50px; right: 120px; left: 120px; border-radius: 50%; background-image: url(http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png); background-size: auto; background-position: center; background-repeat: no-repeat; transition: 0.5s; } #myimage:hover::before { top: 0; bottom: 0; right: 0; left: 0; border-radius: 0; } 
 <div id="myimage"> </div> 

Maybe I didn't understood... I thougth it had to be a circle. 也许我不理解......我认为它必须是一个圆圈。 (full screen to see it at real size) (全屏以实际尺寸查看)

 #myimage{ background-color: #ffff; text-align: center; height:547px; width:547px; display:block; margin:0 auto; border-radius:50%; overflow:hidden; transition-duration: 0.25s; } #myimage img{ display:inline-block; margin:0 auto; height:100%; width:auto; position:relative; left:-138px; transition-duration: 0.25s; } #myimage:hover img{left:0;} #myimage:hover{ width:823px; border-radius:0; } 
 <div> <div id ="myimage"><img src="http://i.prntscr.com/cC-VFJNfRbqxjohddP9AGw.png"></div> </div> 

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

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