简体   繁体   English

jQuery-单击动画元素

[英]JQuery - Click animated element

I am trying to create something where you hover over the boxes and 'tabs' slide out which you should be able to click. 我正在尝试创建一些内容,使您将鼠标悬停在框上,然后滑出“选项卡”,您应该可以单击它们。

However, I can't seem to work out how I can keep the red box extended when hovering over it so I can click the red tab and as you mouse off the red tab the animation goes back. 但是,我似乎无法弄清楚如何将红色框悬停在红色框上,因此我可以单击红色选项卡,然后将鼠标悬停在红色选项卡上,动画就会返回。

Here is what I have got so far: http://jsfiddle.net/gLsWw/1/ 这是到目前为止我得到的: http : //jsfiddle.net/gLsWw/1/

<div class="box">
    <a class="slide"></a>
</div>

<div class="box">
    <a class="slide"></a>
</div>

$(".box").hover(function() {
    $(".slide").stop(true, false).animate({ width: "200px" });
}, function() {
    $(".slide").stop(true, false).animate({ width: "auto" });
}); 

.box {
    width: 65px;
    height: 65px;
    background-color: green;
    border: 1px blue solid;
}

.slide {
    position: absolute;
    width: 65px;
    height: 65px;
    background-color: red;
    z-index: -1;
}

Thanks in advanced. 提前致谢。

I believe I have it working how you want: http://jsfiddle.net/594Yk/ 我相信我可以按照您的意愿进行操作: http : //jsfiddle.net/594Yk/

$(".box").hover(function() {
    $(this).find(".slide").animate({ width: "200px" });
}); 

$(".box").mouseout(function() {
    $(this).find(".slide").animate({ width: "65px" }); 
});

The main problem is that .slide has a negative z-index. 主要问题是.slide的Z索引为负。 You would need to set this up without the negative z-index in order for it to work properly. 您需要在没有负z-index的情况下进行设置,以使其正常工作。 Here is the updated CSS: 这是更新的CSS:

.box {
    width: 65px;
    height: 65px;
    background-color: green;
    border: 1px blue solid;
    position: relative;
}

.slide {
    position: absolute;
    width: 0;
    height: 65px;
    background-color: red;
    left: 66px;
}

Along with that, I assume that you only want one red element to slide out at a time, not both. 除此之外,我假设您一次只希望滑出一个红色元素,而不希望两者都滑出。 Here's the updated JS: 这是更新的JS:

$(".box").hover(function() {
    $(this).find(".slide").stop().animate({ width: 200 });
}, function() {
    $(this).find(".slide").stop().animate({ width: 0 });
}); 

I changed the following in the JS: 我在JS中更改了以下内容:

  • Added .stop() to the animations to cancel any existing animations. 在动画中添加了.stop()来取消任何现有动画。
  • Changed the hidden width to 0 so that it will animate. 将隐藏的宽度更改为0,以便对其进行动画处理。
  • Selectors are now wrapped in a .find call to limit it to the current element. 选择器现在包装在.find调用中,以将其限制为当前元素。

Updated JSFiddle: http://jsfiddle.net/gLsWw/28/ 更新的JSFiddle: http : //jsfiddle.net/gLsWw/28/


Alternatively, you could do this entirely with CSS: 或者,您可以完全使用CSS做到这一点:

.box {
    width: 65px;
    height: 65px;
    background-color: green;
    border: 1px blue solid;
    position: relative;
}

.slide {
    position: absolute;
    width: 0;
    height: 65px;
    background-color: red;
    left: 66px;
    -webkit-transition: width 0.5s;
    transition: width 0.5s;
}

.box:hover .slide {
    width: 200px;
}

JSFiddle: http://jsfiddle.net/gLsWw/30/ JSFiddle: http : //jsfiddle.net/gLsWw/30/

One solution is that you could have a container element that Holds both the box and the sliding tab. 一种解决方案是您可以拥有一个既包含框又包含滑动选项卡的容器元素。

Something like... 就像是...

<div class="boxContainer">
    <div class="box">
        <a class="slide"></a>
    </div>
</div>

$(".boxContainer").hover(function() {
    $(".slide").stop(true, false).animate({ width: "200px" });
}, function() {
    $(".slide").stop(true, false).animate({ width: "auto" });
}); 

http://jsfiddle.net/pTU2d/ http://jsfiddle.net/pTU2d/

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

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