【问题标题】:CSS transition on transparent images透明图像上的 CSS 过渡
【发布时间】:2016-02-04 15:04:32
【问题描述】:

我有三张图片(透明 png)

使用以下 html/css 堆叠

<div style="position: relative; left: 0; top: 0;">
    <img src="img/main.png" style="position: absolute; top: 0; left: 0;" />
    <img src="img/middle.png" style="position: absolute; top: 0; left: 0;"/>
    <img src="img/center.png" style="position: absolute; top: 0; left: 0;"/>
</div>

得到这个:

我想在每个图像上添加悬停效果(放大、边框、不透明度等)。

在悬停时放大的普通 CSS 是:

img {
    -webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
}

img:hover {
    -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
     transform:scale(1.25);
}

在这种情况下不起作用,因为悬停效果会应用于整个图像而不仅仅是图像部分(图像具有透明背景)。

我的问题是,是否可以使用不规则形状的 CSS 设置透明图像的样式?

jsfiddle 在这里:http://jsfiddle.net/h4mxysw5/

编辑:

似乎有些混乱。我不想一次缩放所有三个图像。

例如 - 当悬停在中心图像上时,我只想缩放中心图像(不是全部)。

更新了带边框的 jsfiddle:http://jsfiddle.net/h4mxysw5/4/

【问题讨论】:

  • 由于这些形状看起来相当简单,您是否考虑过使用 SVG?
  • @php_nub_qq 我想到了,但我也有复杂的图像。我正在分析可能的解决方案(例如 Flash 等)
  • 您可以使用 :nth-child 选择器选择包装器的任何子级。但我建议您使用不同的顶部、左侧和 z-index 来定位 img,为什么您需要所有 img 的图像?两个内圈很容易用简单的 html/css (border-radius) 重现。

标签: javascript jquery html css


【解决方案1】:

Demo
我为三个 div 使用了 z-index,每个 div 都有背景图像

  • /*Largest Circle*/ Div1=z-index:1
  • /*second Circle*/div1=z-index:2
  • /*Middle Small Circle*/ Div1=z-index:3

z-index 基本上是用来堆叠它,所以中间圆在顶部,第二个圆在最大圆和中间小圆之间,最大的圆最后这不会影响鼠标悬停在其他圆上,因为所有圆比它上面的圆圈大(在z-index),所以它们是可见的和可悬停的。 div id allImg 用于设置对齐、大小,因为所有子 div 到 allImg 都有宽度,高度百分比,它们会自动调整大小

/*Outer Div use for alignment and to set size*/
		#allImg{
			width: 200px;
			height: 200px;
			margin: 0 auto;
			position: relative;
			 top:100px;
		}
		/*Styling appling to all desendant div inside allImg*/
		#allImg > div{
			position:absolute;
			padding: 0px;
			-webkit-transition: all 1s ease;/* Safari and Chrome */
		    -moz-transition: all 1s ease;/* Firefox */
		    -o-transition: all 1s ease; /* IE 9 */
		    -ms-transition: all 1s ease;/* Opera */
		    transition: all 1s ease;
		    position: absolute;
			padding: 0px;
			transition: all 1s ease 0s;
			border: 1px solid #000;
			border-radius: 100px;
		}
		/*Div with smallest z-index i.e outer circle*/
		#img1{
			background-image: url('http://i.stack.imgur.com/GWShR.png');
			background-size: 100% 100%;
			width: 100%;
			height: 100%; 
			z-index: 1;

		}

		#img1:hover{
			-moz-transform: scale(1.25);/* Firefox */
			-webkit-transform:scale(1.25); /* Safari and Chrome */
		    -ms-transform:scale(1.25); /* IE 9 */
		    -o-transform:scale(1.25); /* Opera */
		     transform:scale(1.25);
		}
		/*Div with greater z-index  then Outer circle i.e 2nd circle*/
		#img2{
			background-image: url("http://i.stack.imgur.com/eWisy.png");
			background-size: 100% 100%;
			width: 50%;
			height: 50%;
			left: 25%;
			top: 25%;
			z-index:2;

		}
		#img2:hover{
			-moz-transform: scale(1.16);/* Firefox */
			-webkit-transform:scale(1.16); /* Safari and Chrome */
		    -ms-transform:scale(1.16); /* IE 9 */
		    -o-transform:scale(1.16); /* Opera */
		     transform:scale(1.16);
		}
		/*Div with greatest z-index i.e middle circle*/
		#img3{
			background-image: url("http://i.stack.imgur.com/VjygS.png");
			background-size: 100% 100%;
			width: 30%;
			height: 30%;
			left: 35%;
			top: 35%;
			z-index:3;
		}
		#img3:hover{
			-moz-transform: scale(1.13);/* Firefox */
			-webkit-transform:scale(1.13); /* Safari and Chrome */
		    -ms-transform:scale(1.13); /* IE 9 */
		    -o-transform:scale(1.13); /* Opera */
		     transform:scale(1.13);
		}
<div id="allImg">
  <div id="img1"></div>
  <div id="img2"></div>
  <div id="img3"></div>
</div>

另请注意,您需要将图像裁剪为@Dave Gomez 所说的实际尺寸

【讨论】:

    【解决方案2】:

    试试Pixel Selection:一个可以处理悬停透明度的jQuery库。

    $(function() {
      $('img').Pixelselect({
        over: function(e, obj, hit) {
          if (hit) {
            obj.addClass('hover');
          } else {
            obj.removeClass('hover');
          }
          e.preventDefault();
        },
        out: function(e, obj) {
          obj.removeClass('hover');
          e.preventDefault();
        },
        sublayers: true
      })
    })
    img {
      -webkit-transition: all 1s ease;
      /* Safari and Chrome */
      -moz-transition: all 1s ease;
      /* Firefox */
      -ms-transition: all 1s ease;
      /* IE 9 */
      -o-transition: all 1s ease;
      /* Opera */
      transition: all 1s ease;
      opacity: 1;
    }
    img.hover {
      opacity: 0.5;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div style="position: relative; left: 0; top: 0;">
      <img src="http://i.stack.imgur.com/80Jxj.png" style="position: absolute; top: 0; left: 0;" />
      <img src="http://i.stack.imgur.com/Eewcq.png" style="position: absolute; top: 0; left: 0;" />
      <img src="http://i.stack.imgur.com/VXk7A.png" style="position: absolute; top: 0; left: 0;" />
    </div>

    (这不适用于缩放图像,因为悬停区域会改变,并且图像需要在同一个域中)

    【讨论】:

    • 那是 jQuery,而不是普通的 JavaScript
    • @Jean-Paul,但它的语法很简洁,可能可以满足操作人员的需求。
    • 当然,但是 OP 没有包含 jQuery 标签(可能是故意的)
    • @Jean-Paul 当然,但它可能对其他人有帮助
    • @penne12 你能写个jsfiddle吗?我无法让它工作jsfiddle.net/63rec2qv/view
    【解决方案3】:

    一种可能的解决方案是:
    (使用The HTML map element

    var vi = function(el) {
      
      var imgEl  = document.getElementById(el.getAttribute('data-img'));
      if(imgEl) imgEl.classList.add('effectOn');
    }
    
    var vo = function(el) {
      
      var imgEl  = document.getElementById(el.getAttribute('data-img'));
      if(imgEl) imgEl.classList.remove('effectOn');
      
    
    }
    img {
        -webkit-transition: all 1s ease; /* Safari and Chrome */
        -moz-transition: all 1s ease; /* Firefox */
        -ms-transition: all 1s ease; /* IE 9 */
        -o-transition: all 1s ease; /* Opera */
        transition: all 1s ease;
    }
    
    img.effectOn {
        -webkit-transform:scale(1.25); /* Safari and Chrome */
        -moz-transform:scale(1.25); /* Firefox */
        -ms-transform:scale(1.25); /* IE 9 */
        -o-transform:scale(1.25); /* Opera */
         transform:scale(1.25);
    }
    <div style="position: relative; left: 0; top: 0;">
      
      <img id='main' src="http://i.stack.imgur.com/80Jxj.png" style="position: absolute; top: 0; left: 0;" usemap='#main' />
      <img id='middle' src="http://i.stack.imgur.com/Eewcq.png" style="position: absolute; top: 0; left: 0;" usemap='#main'/>
      <img id='center' src="http://i.stack.imgur.com/VXk7A.png" style="position: absolute; top: 0; left: 0;" usemap='#main'/>
    
      <map id="main">
    
        <area shape="circle" onmouseover='vi(this)' onmouseout='vo(this)' data-img='center' coords="147,147,58" />
        <area shape="circle" onmouseover='vi(this)' onmouseout='vo(this)' data-img='middle' coords="147,147,90" />
        <area shape="circle" onmouseover='vi(this)' onmouseout='vo(this)' data-img='main' coords="147,147,147" />
    
      </map>
    
    </div>

    您可以在任何类型的表格上使用这种方式,矩形、三角形、多边形...
    最困难的是划定地图,但有各种软件可以帮助您(GIMP 做到这一点)。

    【讨论】:

    • 悬停时没有做任何事情。另外你如何计算坐标?可以通过 GIMP 完成吗?
    • 我已经在 Firefox 42 上使用了上面的工作 sn-p !对于the coords you can read this,是的,您可以使用GIMP
    【解决方案4】:

    这里的主要问题是您使用的所有图像的大小都相同 - 因此,由于它们彼此叠放,因此您只会将鼠标悬停在最上面的那个上。仅仅因为图像是透明的,当您将鼠标悬停在图像的任何部分上时,它仍然会触发 :hover。 为了演示使用您自己的 CSS,您可以在没有图像的情况下这样做:

    div > div {
    -webkit-transition: all 1s ease; /* Safari and Chrome */
    -moz-transition: all 1s ease; /* Firefox */
    -ms-transition: all 1s ease; /* IE 9 */
    -o-transition: all 1s ease; /* Opera */
    transition: all 1s ease;
    border:10px solid #f00;
    border-radius:50%;
    position: absolute; 
     }
    
    .outer {
    width:200px;
    height:200px;
        top: 25px; 
    left: 25px;
    border:30px solid #f00;
    }
    .middle {
    width:150px;
    height:150px;
        top: 60px; 
    left: 60px;
    border:20px solid #f00;
    }
    .inner {
    width:100px;
    height:100px;
    top: 95px; 
    left: 95px;
    border:10px solid #f00;
    }
    
    div > div:hover {
    -webkit-transform:scale(1.25); /* Safari and Chrome */
    -moz-transform:scale(1.25); /* Firefox */
    -ms-transform:scale(1.25); /* IE 9 */
    -o-transform:scale(1.25); /* Opera */
     transform:scale(1.25);
    }
    <div style="position: relative; left: 0; top: 0;">
        <div class="outer"></div><div class="middle"></div><div class="inner"></div>
    </div>

    在这里,通过一些调整,您可以使用相同的 CSS,但也可以使用图像作为“背景图像”来提供您想要实现的效果。

    div > div {
        -webkit-transition: all 1s ease; /* Safari and Chrome */
        -moz-transition: all 1s ease; /* Firefox */
        -ms-transition: all 1s ease; /* IE 9 */
        -o-transition: all 1s ease; /* Opera */
        transition: all 1s ease;
        border:1px solid #f00;
    border-radius:50%;
    position: absolute; 
     }
    
    .outer {
        width:280px;
        height:280px;
            top: 25px; 
        left: 25px;
        background-image:url(http://i.stack.imgur.com/bFfbC.png);
    }
    .middle {
        width:130px;
        height:130px;
            top: 100px; 
        left: 100px;
        background-image:url(http://i.stack.imgur.com/Eewcq.png);
        background-position:center;
    }
    .inner {
        width:75px;
        height:75px;
        top: 125px; 
        left: 125px;
        background-image:url(http://i.stack.imgur.com/VXk7A.png);
        background-position:center;
    }
    
    div > div:hover {
        -webkit-transform:scale(1.25); /* Safari and Chrome */
        -moz-transform:scale(1.25); /* Firefox */
        -ms-transform:scale(1.25); /* IE 9 */
        -o-transform:scale(1.25); /* Opera */
         transform:scale(1.25);
    }
    <div style="position: relative; left: 0; top: 0;">
        <div class="outer"></div>
        <div class="middle"></div>
        <div class="inner"></div>
    </div>

    【讨论】:

    • 这就是我想要实现的,但使用图像。是否可以使用图像代替外部、内部、中间元素?
    • 是的,你可以,但你需要确保图像没有相互重叠。如果您想象您有 3 张绝对定位的图像,那么如果它们的大小都相同(z-index 最高的那张),您将只能将鼠标悬停在其中的一张上。我现在会制作一些图片并更新我的答案
    • 如何完全不使用问题中的图像作为答案。只是对border-radius的评论。
    • 它表明 OP 使用的技术有效,但实现无效。以前的答案似乎并没有掌握 OP 的要求。 @Shikkediel
    • 最后一点在任何情况下都是正确的。 ;-)
    【解决方案5】:

    出于纯粹的好奇,我只需要创建一个 CSS only 版本。虽然它不使用图像作为 OP 所需的,但我仍然认为,作为img 和/或JS 的替代品,结果值得发布。

    在 sn-p 中,您将看到无阴影和阴影版本。请给你的cmets...

    (顺便说一句:在 W7 上的 FF DE 44+、Chrome 46+ 和 IE11+ 中测试)

    html, body      { box-sizing: border-box; 
                      height: 100%; width: 100%; background-color: #f9f7f1;
                      margin: 0px; padding: 0px; border: 0px;
                      cursor: default }
    *, *:before, 
    *:after         { box-sizing: inherit }
    
    .donut-button   { position: relative;
                      width:  280px;
                      height: 280px; 
                      margin: 100px auto;
                      cursor: pointer }
    
    .r-outer        { width: 100%; height: 100%; border-width: 55px; top:  0.0%; left:  0.0% }
    .r-middle       { width:  50%; height:  50%; border-width: 15px; top: 25.0%; left: 25.0% }
    .r-center       { width:  25%; height:  25%; border-width: 20px; top: 37.5%; left: 37.5% }
    
    .ring           { position: absolute;
                      border-color : hsl(205, 69%, 58%);
                      border-style : solid;
                      border-radius: 50%;
                      transition: all 50ms }
    
    .ring:hover     { transform: scale(1.10) }
    .ring:active    { transform: scale(0.95) }
    
    
    /* demo extras, shadow and color manipulation during hover */
    [btn]           { box-shadow: inset    0    0    1px hsla(205, 69%,48%, 1),  /* hide white overflow (quirk) */
                                  inset   10px 10px 10px hsla(205, 69%, 8%,.3),  /* inset shadow */
                                           0    0    1px hsla(205, 69%,58%, 1),  /* hide white overflow (ditto) */
                                          20px 20px 10px hsla(205, 69%, 8%,.4),  /* inner outside shadow */
                                           0    0    1px hsla(205, 69%, 8%,.3) } /* outer outside shadow */
    
    [btn]:hover     { border-color: hsl(205, 69%, 62%);
                      box-shadow: inset 10px 10px 10px hsla(205, 69%, 8%,.4),
                                        20px 20px 10px hsla(205, 69%, 8%,.3) }
    
    [btn]:active    { border-color: hsl(205, 69%, 54%);
                      box-shadow: inset  8px  8px  8px hsla(205, 69%, 8%,.5),
                                        10px 10px 10px hsla(205, 69%, 8%,.4) }
        <div id="donut-1" class="donut-button">
            <div class="ring r-outer"></div>
            <div class="ring r-middle"></div>
            <div class="ring r-center"></div>
        </div>
    
        <div id="donut-2" class="donut-button">
            <div btn class="ring r-outer"></div>
            <div btn class="ring r-middle"></div>
            <div btn class="ring r-center"></div>
        </div>

    【讨论】:

    • 非常令人印象深刻的雷内。但是对于不能使用 CSS 绘制的多边形甚至图像呢?
    • 你真的在逼我更深入?哦....但我认为在这种情况下,您总是可以退回到堆叠具有透明背景的几张图像。但是,您为什么不添加一些指向复杂图像的链接,以便我们(我)可以查看?
    • 这是我会提供的答案,但我会想出一些添加长阴影的方法(可能是父元素上带有overflow:hidden; 的伪元素)。
    • @fauxserious 给我看!创建一支笔或其他东西并发布链接。
    • @fauxserious,我会深入研究这个并在稍后提出另一个不错的解决方案。 tnx 的例子(和挑战!)
    【解决方案6】:

    你必须做的两件事。

    • 裁剪图像以仅适合它们所需的空间,而不是整个容器大小,这样它们就不会相互重叠。
    • div 中删除:hover 并使用img 选择器为每个图像添加:hover 行为。

    下面是例子:

    div {
        margin: 50px; /* Just for demo purposes */
    }
    
    img {
        -webkit-transition: all 1s ease; /* Safari and Chrome */
        -moz-transition: all 1s ease; /* Firefox */
        -ms-transition: all 1s ease; /* IE 9 */
        -o-transition: all 1s ease; /* Opera */
        transition: all 1s ease;
    }
    
    img:hover {
        -webkit-transform:scale(1.25); /* Safari and Chrome */
        -moz-transform:scale(1.25); /* Firefox */
        -ms-transform:scale(1.25); /* IE 9 */
        -o-transform:scale(1.25); /* Opera */
         transform:scale(1.25);
    }
    <div style="position: relative; left: 0; top: 0;">
        <img class="one" src="http://i.stack.imgur.com/bFfbC.png" style="position: absolute; top: 0; left: 0;" />
        <img class="two" src="http://i.imgur.com/iEwbExs.png" style="position: absolute; top: 76px; left: 72px;"/>
        <img class="three" src="http://i.imgur.com/hdwFlUv.png" style="position: absolute; top: 102px; left: 100px;"/>
    </div>

    更新

    检查您可以使用 SVG 做什么:

    path {
        -webkit-transition: all 1s ease; /* Safari and Chrome */
        -moz-transition: all 1s ease; /* Firefox */
        -ms-transition: all 1s ease; /* IE 9 */
        -o-transition: all 1s ease; /* Opera */
        transition: all 1s ease;
        transform-origin: center center;
    }
    
    path:hover {
        -webkit-transform:scale(1.25); /* Safari and Chrome */
        -moz-transform:scale(1.25); /* Firefox */
        -ms-transform:scale(1.25); /* IE 9 */
        -o-transform:scale(1.25); /* Opera */
         transform:scale(1.25);
    }
    <svg width="400px" height="400px">
        <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <path d="M140.5,178 C161.210678,178 178,161.210678 178,140.5 C178,119.789322 161.210678,103 140.5,103 C119.789322,103 103,119.789322 103,140.5 C103,161.210678 119.789322,178 140.5,178 Z M141,158 C150.388841,158 158,150.388841 158,141 C158,131.611159 150.388841,124 141,124 C131.611159,124 124,131.611159 124,141 C124,150.388841 131.611159,158 141,158 Z" fill="#4BA1DF"></path>
                <path d="M140,205 C175.898509,205 205,175.898509 205,140 C205,104.101491 175.898509,75 140,75 C104.101491,75 75,104.101491 75,140 C75,175.898509 104.101491,205 140,205 Z M140,189 C167.061953,189 189,167.061953 189,140 C189,112.938047 167.061953,91 140,91 C112.938047,91 91,112.938047 91,140 C91,167.061953 112.938047,189 140,189 Z" fill="#4BA1DF"></path>
                <path d="M140,280 C217.319865,280 280,217.319865 280,140 C280,62.680135 217.319865,0 140,0 C62.680135,0 0,62.680135 0,140 C0,217.319865 62.680135,280 140,280 L140,280 Z M140.5,226 C187.720346,226 226,187.720346 226,140.5 C226,93.2796539 187.720346,55 140.5,55 C93.2796539,55 55,93.2796539 55,140.5 C55,187.720346 93.2796539,226 140.5,226 L140.5,226 Z" fill="#4BA1DF"></path>
        </g>
    </svg>

    【讨论】:

    • @Dave 如下图:jsfiddle.net/afLmrmmk/2 背景也有悬停效果。是否可以仅将其应用于可见(图像)部分?
    • 您可以使用 SVG 实现该行为,但比这更复杂。这样你就不能只将悬停添加到蓝色部分,因为图像是一个矩形,它不关心该矩形内的颜色或活动区域
    • 捎带这个答案 - 您可以添加边框半径并获得更准确的悬停区域:jsfiddle.net/afLmrmmk/4
    • @DaveGomez 你如何将图像转换为 SVG?
    • 您不能@WhatisSober,您必须使用任何矢量图像创建工具明确地将图像创建为 SVG,但是将图像创建为 SVG 的负担将通过灵活性和图像质量得到补偿只有 SVG 图像才能为您提供。
    【解决方案7】:

    正如评论中所说,只有一张图片,另外两张只有 html/css。 这里的小提琴,希望你需要的效果是这样的:)

    https://jsfiddle.net/keypaul/8dr25184/

    HTML

    <div id="wrap">
        <div></div>
        <div></div>
        <img src="http://i.stack.imgur.com/bFfbC.png" alt="" />
    </div>
    

    CSS

        #wrap {
        position:relative;
        width:280px;
        height:280px;
    }
    
    #wrap img{
        position:relative;
        max-width:100%;
        height:auto;
        top:0;
        left:0;
        z-index:1;
        transform: scale(1);
        transition: 0.4s;
    }
    
    #wrap img:hover {
         transform:scale(1.25);
    }
    
    #wrap div:nth-child(1){
        background: transparent;
        border: 15px solid red;
        border-radius: 100px;
        height: 99px;
        left: 75px;
        overflow: hidden;
        position: absolute;
        top: 77px;
        width: 99px;
        z-index: 2;
        transform: scale(1);
        transition: 0.4s;
    }
    
    #wrap div:hover:nth-child(1){
        transform: scale(1.5);
    }
    
    #wrap div:nth-child(2){
        background: transparent;
        border: 20px solid red;
        border-radius: 40px;
        height: 34px;
        left: 103px;
        overflow: hidden;
        position: absolute;
        top: 105px;
        width: 34px;
        z-index: 3;
        transform: scale(1);
        transition: 0.4s;
    }
    
    #wrap div:hover:nth-child(2){
        transform: scale(1.5);
    }
    

    您只需从大png中删除内部小圆圈,更改内部甜甜圈的颜色并在css中添加供应商前缀。

    【讨论】:

    • 我不想使用 CSS 来绘制形状。问题是对不规则图像(具有透明背景的图像)产生悬停效果。
    • 对不起,我现在也看到了你需要的效果。
    【解决方案8】:

    使用 JavaScript,您可以对悬停区域进行硬编码,如下所示:

    JavaScript

    function animateCircles(obj) {
        var x = window.event.x - obj.offsetLeft;
        var y = window.event.y - obj.offsetTop;
        var img1 = document.getElementById('1');
        var img2 = document.getElementById('2');
        var img3 = document.getElementById('3');
    
        var centerR = 45;
        var middleR = 75;
        if (x >= img3.offsetLeft + (img3.offsetWidth / 2 - centerR) && 
            x <= img3.offsetLeft + (img3.offsetWidth / 2 + centerR) && 
            y >= img3.offsetTop + (img3.offsetHeight / 2 - centerR) && 
            y <= img3.offsetTop + (img3.offsetHeight / 2 + centerR))
            img3.className += " onhover";
        else
            img3.className = "normal";
            
        if (x >= img2.offsetLeft + (img2.offsetWidth / 2 - middleR) && 
            x <= img2.offsetLeft + (img2.offsetWidth / 2 + middleR) && 
            y >= img2.offsetTop + (img2.offsetHeight / 2 - middleR) && 
            y <= img2.offsetTop + (img2.offsetHeight / 2 + middleR))
            img2.className += " onhover";
        else
            img2.className = "normal";
        
        if (x >= img1.offsetLeft && 
            x <= img1.offsetLeft + img1.offsetWidth && 
            y >= img1.offsetTop && 
            y <= img1.offsetTop + img1.offsetHeight)
            img1.className += " onhover";
        else
            img1.className = "normal";
    }
    

    您可以在其中使用变量centerRmiddleR 指定悬停“正方形”的大小(应该进行悬停的图像的中心)。请注意,您还可以改进此代码以随着图像的增长而扩大悬停区域,这样图像只会在您悬停在放大的图像之外时缩小。请注意,我特意对图像的所有宽度和高度进行了软编码:如果您决定更改图片,这将提供更大的灵活性。

    使用以下 HTML:

    <div style="position: relative; left: 0; top: 0;">
        <img id="1" src="http://i.stack.imgur.com/bFfbC.png" style="position: absolute; top: 0; left: 0;" onmousemove="animateCircles(this)" />
        <img id="2" src="http://i.stack.imgur.com/Eewcq.png" style="position: absolute; top: 0; left: 0;" onmousemove="animateCircles(this)" />
        <img id="3" src="http://i.stack.imgur.com/VXk7A.png" style="position: absolute; top: 0; left: 0;" onmousemove="animateCircles(this)" />
    </div>
    

    和 CSS:

    .normal {
        -webkit-transition: all 1s ease;
        -moz-transition: all 1s ease;
        -ms-transition: all 1s ease;
        -o-transition: all 1s ease;
        transition: all 1s ease;
    }
    .onhover {
        -webkit-transform:scale(1.25);
        -moz-transform:scale(1.25);
        -ms-transform:scale(1.25);
        -o-transform:scale(1.25);
        transform:scale(1.25);
    }
    

    这会产生以下结果:

    > DEMO

    【讨论】:

    • 可能是我解释错了问题,但是这个解决方案保证了当光标向内移动时,外部图像保留悬停动画。其他解决方案似乎对问题的解释不同(例如,分别为图像设置动画)。
    • 注意:我提供的解决方案也可以非常快速地构建单个动画。只需在 if 语句中添加一些额外的 add classremove class 行。
    【解决方案9】:

    我认为你需要 => JSFiddle demo :)

        div:hover > img {
        -webkit-transform:scale(1.25); /* Safari and Chrome */
        -moz-transform:scale(1.25); /* Firefox */
        -ms-transform:scale(1.25); /* IE 9 */
        -o-transform:scale(1.25); /* Opera */
         transform:scale(1.25);
    }
    

    【讨论】:

    • "悬停效果应用于整个图像,而不仅仅是图像部分"
    • 我还建议添加 -webkit-backface-visibility: hidden; 以防止 chrome 上显示锯齿状边缘:)
    猜你喜欢
    • 2021-07-26
    • 2015-07-13
    • 2018-01-31
    • 2021-05-11
    • 1970-01-01
    • 2020-05-29
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多