简体   繁体   English

将两个单独的元素附加到一个动画脚本

[英]Attaching two separate elements to one animate script

I'm trying to get a div to change its width through hovering over it. 我试图让div通过将其悬停来更改其宽度。 I've got that sussed but I would also like its width to change through hovering an h5 tag which is separate. 我对此表示怀疑,但我也希望通过将h5标签悬停在单独的标签上来更改其宽度。 Does anyone have any ideas how to do this? 有谁知道如何做到这一点? Here's what I have at the moment. 这是我目前所拥有的。

$(function(){
$("#hoverbox").hover(
    function(){
       $(this).animate({width: '150px'});
    },
    function(){
        $(this).animate({width: '38px'});
    }
);
}};

https://jsfiddle.net/m0u8braj/5/ ... alternatively it's at http://www.cosmosdesign.co.nz/ . https://jsfiddle.net/m0u8braj/5/ ...或者位于http://www.cosmosdesign.co.nz/ Also you may notice the box can act a little spastic and expand and retract numerous times, does this relate to my current code? 另外,您可能会注意到盒子可能会有点痉挛,并且会多次伸缩,这与我当前的代码有关吗? Thanks. 谢谢。

I've edited your Fiddle . 我已经编辑了你的小提琴 It now does what you desire with CSS only. 现在,仅使用CSS即可满足您的需求。 No need for jQuery's .animate() 不需要jQuery的.animate()

It's working like this, because CSS's :hover can affect sibling elements with the + or ~ selectors, like so: 它的工作原理是这样的,因为CSS的:hover可能会影响带有+~选择器的兄弟元素,如下所示:

#hoverbox:hover, h5:hover + #hoverbox{
  width: 150px;
}

Hope this helps 希望这可以帮助

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

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