【问题标题】:CSS3 transition on a background image背景图像上的 CSS3 过渡
【发布时间】:2011-11-10 16:01:09
【问题描述】:

我想做一个 CSS3 变换:rotate(360deg);在过渡 1s; 在背景图像而不是单独的图像(元素)上.. 这可能吗?我已经从谷歌搜索了地狱,但没有成功! 我想要达到的目标是:

#footerLogo { 
  background: url('ster.png'), 
  -moz-transition: -moz-transform 1s,
  transition: transform 1s,
  -o-transition: -o-transform 1s,
  -webkit-transition: -webkit-transform 1s;
  background-position: #outlinedtotheleft;
}
#footerLogo:hover {
  background: transform: rotate(360deg);
  -o-transform: rotate(360deg);
  -moz-transform: rotate(360deg);
  -webkit-transform: rotate(360deg);
}

我希望这是可能的!我知道这在 JS (jQuery) 中很容易实现:

$('#something').hover(function(){morecodehere});

...但我想知道是否可以仅使用 CSS(3)

HTML:

<div id="footerLogo">
  <img src="carenza.png"/>
</div>

【问题讨论】:

  • 下次添加代码块时,只需将整个代码块缩进4+空格即可保留缩进。对内联代码使用反引号。

标签: html css background css-transitions


【解决方案1】:

我认为您不能对背景图像本身应用变换(与 div 分开)。但是,您可以旋转整个 div,包括使用过渡对其进行动画处理 (here's a working example.)

如果您能描述您想要达到的确切效果,可能会有所帮助?

代码:

#footerlogo {
    width: 200px;
    height: 200px;
    background-image: url(http://lorempixum.com/200/200);
    -webkit-transition: -webkit-transform 1s;
    -moz-transition: -moz-transform 1s;
    transition: transform 1s;
}

#footerlogo:hover {
    -webkit-transform: rotate(360deg);
    -moz-transform: rotate(360deg);
    transform: rotate(360deg);
}

【讨论】:

  • 无前缀的 css 声明应始终放在最后!您想在准备好后使用标准实现,而不是被浏览器特定的覆盖它,因为它可能会被破坏。
  • @gondo 在 2011 年,我怀疑有人会想得那么远 :) 我会改变顺序。
【解决方案2】:

当然可以,试试这样的:

HTML

<div id="planet">
</div>

CSS

#planet { 
    width:200px;
    height:200px;
    background: transparent url(http://cdn3.iconfinder.com/data/icons/nx11/Internet%20-%20Real.png) no-repeat center center;
}

#planet {
  -webkit-animation-name: rotate;
  -webkit-animation-duration:2s;
  -webkit-animation-iteration-count:infinite;
  -webkit-animation-timing-function:linear;
  -moz-animation-name: rotate;
  -moz-animation-duration:2s;
  -moz-animation-iteration-count:infinite;
  -moz-animation-timing-function:linear;
}

@-webkit-keyframes rotate {
  from {-webkit-transform:rotate(0deg);}
  to {  -webkit-transform:rotate(360deg);}
}

@-moz-keyframes rotate {
  from {-moz-transform:rotate(0deg);}
  to {  -moz-transform:rotate(360deg);}
}

JSFiddle

【讨论】:

  • 几乎,但它只需要在鼠标悬停时转换,并且 div 包含另一个 IMG。想象;您有一个小星星图像(单独但属于原始徽标的一部分)以及该徽标的其余部分(“某个名称”)旁边。如果我将鼠标悬停在 div (两个图像之一)上,那么只有星星旋转。只有 CSS3 .. 不过还是很好的例子 :-) !
  • 现在我正在尝试编辑您的代码以实现如果您的鼠标不再悬停时星星将旋转回(到起点)的效果
  • 使用 css3 没有办法解决它(至少我知道没有办法)。悬停状态是非常线性的,不允许您在悬停和悬停之间转换,您可以使用 javascript。
  • 是的,你可以用 JS 解决这个问题,但挑战是在 CSS3 中做到这一点;).. 我之前已经实现了;让过渡回到'onmouseout'(你可以称之为;)但我已经得到了很多帮助并且主要问题得到了回答(我猜?)。我想我不能在同一个线程中也问这个问题。
  • 好吧,我认为使用任何其他类型的其他转换很简单,例如填充,您可以操纵它在 :hover 状态下将填充增加 10px 并降低填充on :active 状态,但是像旋转这样抽象的东西我不认为只用 css 就可以实现这种特殊性。再说一次,我可能是错的,我每天都会在 CSS3 中看到一些新东西。
猜你喜欢
  • 2014-08-01
  • 2018-02-25
  • 2014-07-22
  • 2011-08-11
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多