简体   繁体   English

jQuery悬停和幻灯片

[英]jQuery hover and Slide

I have 20 different divs. 我有20个不同的div。

5 class="icon", 5 class =“ icon”,

5 class="rainskin", 5 class =“ rainskin”,

4 class="schoolproject", 4 class =“ schoolproject”,

4 class="wallpaper", & 4 class =“ wallpaper”,&

2 class="miscellaneous". 2 class =“杂项”。

Each div has one div with class=".type". 每个div都有一个带有class =“。type”的div。 I have the header portion (about 27 pixels) of the .type div showing, but the rest of it is hidden. 我显示了.type div的标题部分(大约27个像素),但其余部分被隐藏了。 When someone hovers over the .type div, it slides up (by changing the margin-top: to 0px). 当有人将鼠标悬停在.type div上时,它会向上滑动(通过将margin-top:更改为0px)。 When the mouse no longer hovers it, it goes back down to its original spot (margin-top: 110px). 当鼠标不再悬停时,它会回到原始位置(margin-top:110px)。

Here's my fiddle. 这是我的小提琴。 and java code. 和Java代码。

(function ($) {
    var original = [];
    $('.type').each(function (i) {
        original.push($(this).css('margin-top'));
    });
    $('.type').hover(function (e) {
        $(this).stop().animate(
            {"margin-top" : (
                $(this).parent().outerHeight() - 
                $(this).outerHeight()
            )},
            250
        );
    }, function (i){
        var i = $('.type').index($(this));
        $(this).stop().animate(
            {"margin-top": original[i]},
            250
        );
    });
}(jQuery));

It works perfectly fine, UNLESS someone hovers over one .type div, and goes to hover over another .type div BEFORE the first .type div slides back down. 它工作得很好,除非有人将鼠标悬停在一个.type div上,然后将鼠标悬停在另一个.type div上,然后再将第一个.type div滑回。 THEN the .icon, .rainkin, .schoolproject, etc. div is moved down a bit. 然后将.icon,.rainkin,.schoolproject等div向下移动一点。 Go to my fiddle and check it out yourself. 去我的小提琴 ,自己检查一下。 I don't know why it's doing it. 我不知道为什么要这么做。

when you use "margin-top" you're actually messing with the flow of the css divs, so if you use "top" rather , the position just applies to that div, you also need to set .type as "relative". 当您使用“ margin-top”时,您实际上在弄混CSS div的流程,因此,如果使用“ top”而不是,该位置仅适用于该div,则还需要将.type设置为“ relative”。 One other thing, your original position can just be "0", and the amount to pull up can be the size of your .type div. 另一件事,您的原始位置可以只是“ 0”,上拉的金额可以是您的.type div的大小。

Check this modification out: 检查此修改:

(Test here: http://jsfiddle.net/gfefN/9/ ) (在这里测试: http : //jsfiddle.net/gfefN/9/

(function ($) {
    var original = [];
    $('.type').each(function (i) {
        original.push(0);
    });
    $('.type').hover(function (e) {
        $(this).stop().animate(
            {"top" : -(
                $(this).height()
            )},
            250
        );
    }, function (i){
        var i = $('.type').index($(this));
        $(this).stop().animate(
            {"top": original[i]},
            250
        );
    });
}(jQuery));

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

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