简体   繁体   English

通过将鼠标悬停在单独的元素上来触发CSS动画

[英]Trigger CSS animations by hovering over a seperate element

I'm attempting to create a small animation on an image (the image grows slightly) when a link is hovered over using only CSS3 animations. 当仅使用CSS3动画将链接悬停在链接上时,我试图在图像上创建一个小的动画(图像会稍微增长)。

The relevant snippets from my code 我的代码中的相关片段

HTML: HTML:

<img id="enterimg" src="img.png" alt="" />
<a id="enterbutton" href="home.php">Enter</a>

CSS: CSS:

#enterimg {
  width: 350px;
  height: 350px;
  -webkit-transition: width 1s ease-out, height 1s ease-out;
  -moz-transition: width 1s ease-out, height 1s ease-out;
  -o-transition: width 1s ease-out, height 1s ease-out;
  transition: width 1s ease-out, height 1s ease-out;
}

a:hover ~ #enterimg{width:400px;height:400px;}

I'm sure the transitions themselves are correct, but none of the different "calls" (the last line of CSS) I've tried have worked. 我确信过渡本身是正确的,但是我尝试过的所有不同“调用”(CSS的最后一行)都没有起作用。

(This is similar to a number of other questions, but I've looked through them and as far as I can tell none of them answer my question) (这与其他一些问题类似,但是我已经仔细阅读了它们,据我所知,他们都没有回答我的问题)

Thanks to Lokesh Suthar. 感谢Lokesh Suthar。

The order of the sibling selector required I placed the link first in the markup. 兄弟选择器的顺序要求我将链接放置在标记中的第一位。 Since the selection was written: 由于选择是写的:

a:hover ~ #enterimg{width:400px;height:400px;}

The markup needed to be in that order 标记必须按此顺序

<a id="enterbutton" href="home.php">Enter</a>
<img id="enterimg" src="img.png" alt="" />

If you wrap the content in the trigger, then position the content absolutely, you can achieve something similar to triggering a sibbling with CSS. 如果将内容包装在触发器中,然后绝对定位内容,则可以实现类似于使用CSS触发同级的方式。 At least it'll look and act that way. 至少它将以这种方式外观和行为。

HTML HTML

<a href="">Click Me
    <img id="enterimg" src="http://www.druglessdrs.com/wp-content/uploads/2012/07/google-logo-small.jpg" alt="" />
</a>

CSS CSS

a img {
    display:block;
    position:absolute;
    top:80px;
    left:0;
    border:solid 1px black;
    -webkit-transition: width 1s ease-out, height 1s ease-out;
    -moz-transition: width 1s ease-out, height 1s ease-out;
    -o-transition: width 1s ease-out, height 1s ease-out;
    transition: width 1s ease-out, height 1s ease-out;
}

a:hover img {
    left:50px;
}

Fiddle 小提琴

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

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