简体   繁体   English

滚动后如何向下滑动div及其内容

[英]How to slide down div AND its contents after scrolling

(read carefully twitchy trigger finger downvoters) (仔细阅读抽动式扳机指头)

I am trying to get a div AND its contents to slide down after scrolling to a certain point. 我正在尝试滚动到某个点后使div及其内容向下滑动。 I have looked extensively, but I have only been able to locate how to slide down the div, however its contents are static. 我已经进行了广泛的研究,但是我只能找到如何将div向下滑动,但是它的内容是静态的。 In other words, the div slides down to reveal its contents, but its contents do NOT slide. 换句话说,div向下滑动以显示其内容,但其内容不会滑动。

Here is the JsFiddle is used... 这是使用的JsFiddle ...

http://jsfiddle.net/BinaryMuse/Ehney/1/ http://jsfiddle.net/BinaryMuse/Ehney/1/

CSS... CSS ...

body {
    height: 10000px;
}

.fixedDiv {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
}

header {
    height: 300px;
    border: 1px solid black;
}

JAVASCRIPT JAVASCRIPT

// Get the headers position from the top of the page, plus its own height
var startY = 500;

$(window).scroll(function(){
    checkY();
});

function checkY(){
    if( $(window).scrollTop() > startY ){
        $('.fixedDiv').slideDown();
    }else{
        $('.fixedDiv').slideUp();
    }
}

// Do this on load just in case the user starts half way down the page
checkY();

As you can see, the content of the div does not move. 如您所见,div的内容不会移动。 Only the div itself. 只有div本身。 I need the content to MOVE down WITH the div, NOT to only be revealed by a moving div. 我需要的内容是随着div向下移动,而不仅仅是被移动的div所揭示。

You can use class and transition to change the position of the div to make it look like its coming down 您可以使用class和transition更改div的位置,使其看起来像下降

See working example: 请参阅工作示例:

 // Get the headers position from the top of the page, plus its own height var startY = 500; $(window).scroll(function() { checkY(); }); function checkY() { if ($(window).scrollTop() > startY) { $('.fixedDiv').addClass("slide"); } else { $('.fixedDiv').removeClass("slide"); } } // Do this on load just in case the user starts half way down the page checkY(); 
 body { height: 10000px; } .fixedDiv { visibility: hidden; position: fixed; top: -10px; left: -0; transition: all .5s; } .slide { visibility: visible; top: 0; } header { height: 300px; border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='fixedDiv'>Here is a fixed div.</div> <header>Here is the header! It is kinda tall to demonstrate...</header> 

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

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