简体   繁体   English

在内部div内上下滑动内部div全高

[英]Slide inner div full height up and down inside outer div

I hope I can describe this well enough, I apologize if it's a little long but I want to be descriptive. 我希望我能够很好地描述这一点,如果它有点长,我道歉但我想描述一下。 This is a simplified version of the problem I'm having on a site I'm currently working on: 这是我正在处理的网站上的问题的简化版本:

I have an inner div inside an outer div- the height of the inner div is not static, sometimes it's height is 'x' pixels while another 'y'. 我在内部div中有一个内部div-内部div的高度不是静态的,有时它的高度是'x'像素,而另一个'y'。 The outer div is a fixed height with overflow:hidden for the overlap of the inner divs, in all cases the inner divs are larger in height than the outer. 外部div是一个带溢出的固定高度:隐藏内部div的重叠,在所有情况下,内部div的高度大于外部div。

My problem is I need to animate the inner div to slide up and down the full height of the inner div so that all of it shows within the outer div. 我的问题是我需要设置内部div的动画来上下滑动内部div的整个高度,以便所有它都显示在外部div中。 You can see I tried using animate with a percentage here (33% was arbitrary): 你可以看到我尝试在这里使用百分比的动画(33%是任意的):

http://jsfiddle.net/FLMp8/301/ http://jsfiddle.net/FLMp8/301/

var stuff = $('#inner1, #inner2');

function runIt() {
    stuff.animate({
        top: '-=33%'
    }, 3000);
    stuff.animate({
        top: '+=33%'
    }, 3000, runIt);
}

runIt();

I can't seem to get it to work uniformly no matter the height however. 然而,无论高度如何,我似乎无法使其均匀地工作。 Any suggestions? 有什么建议? Thanks. 谢谢。

Try this, 试试这个,

var stuff = $('#inner1, #inner2');

function runIt() {
    stuff.each(function() {
        $(this).animate({
            top: '0'
        }, 3000);
    });
    stuff.each(function() {
        $(this).animate({
            top: -$(this).height() + $(this).parent('div').height()
        }, 3000, runIt);
    });
}

runIt();

FIDDLE 小提琴

You can simplify this code as follows 您可以按如下方式简化此代码

var stuff = $('#inner1, #inner2');

function runIt() {
    stuff.each(function() {
        var $this = $(this);
        $this.animate({
            top: '0'
        }, 3000).animate({
            top: -$this.height() + $this.parent('div').height()
        }, 3000, runIt);
    });
}

runIt();

FIDDLE 小提琴

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

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