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

标签 jquery css

(仔细阅读抽搐的扳机手指downvoters)

我试图让一个 div 及其内容在滚动到某个点后向下滑动。我已经广泛查看,但我只能找到如何向下滑动 div,但它的内容是静态的。换句话说,div 向下滑动以显示其内容,但其内容不会滑动。

这里使用的是JsFiddle...

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

CSS...

body {
    height: 10000px;
}

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

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

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();

如您所见,div 的内容没有移动。只有 div 本身。我需要内容随 div 一起向下移动,而不是仅由移动的 div 显示。

最佳答案

你可以使用class和transition来改变div的位置,让它看起来像是在下降

查看工作示例:

// 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>

关于jquery - 滚动后如何向下滑动div及其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44906650/

相关文章:

javascript - 输入无效时在 div 上添加抖动效果

php - Ajax 自动更新和服务器负载

html - 如何使用 CSS/HTML 在文本和文本旁边的框之间添加常规空格?

html - 如何在相同高度拍摄照片?

javascript - 如何将 css 加载器仅应用于内容的一个特定部分

c# - 无法加载文件或程序集 'AjaxMin,版本 = 4.97.4951.28478,

jquery - 使用 jQuery 比较 2 个 SELECT 框的值

php - 通过 Modal Bootstrap 传递数据并获取 php 变量?

css - 如何在 Twitter Bootstrap 中禁用缩小/调整图像大小?

ios - 为什么此 iFrame 在 IOS 上没有响应?