jquery - 在页面底部淡入(逐渐)

标签 jquery html css fadein

我想要一个能够逐渐将页面底部的不透明度从 0 更改为 1 的函数。

我在元素顶部使用了类似的功能,但我没有使用淡入,而是使用了淡出。创建这个相当容易,但在页面底部设置阈值的淡入是一场噩梦。

经过研究,我发现可以使用以下方法在页面底部创建淡出:

var scrollBottom: (($(document).height() - $(window).height()) - $(window).scrollTop())

要设置阈值,我必须将“scrollBottom”除以一个数字。一切正常,但无论如何,我无法将淡出更改为淡入。我试图玩弄颜色而不是不透明度。将文本从白色更改为黑色会给我完全相同的结果,但我无法将“scrollBottom”中的单独值分配给特定颜色并逐渐更改它。

经过多次尝试,我找到了解决方案,将淡出效果保留在页面底部,但不是淡出文本,而是淡出文本上方纯色的 div层。它有效,但我对以下事实感到不满意:在不透明度为 0 之前,您无法突出显示文本,我可以将 div 的 display: 设置为 none;

这是代码:

$(window).on("scroll", function() {

  $(".h-work").css("opacity", 1 - $(window).scrollTop() / 320);
  if ($(window).scrollTop() >= 320) {
    $(".h-work").hide();
  } else {
    $(".h-work").show();
  }

  var t = 320;
  var sb = (($(document).height() - $(window).height()) - $(window).scrollTop());
  var f = sb / t;
  $(".hide").css("opacity", f);

  if (sb >= 320) {
    $(".h-work-bottom").hide();
  } else {
    $(".h-work-bottom").show();
  }

  if (f <= 0) {
    $(".hide").hide();
  } else {
    $(".hide").show();
  }

});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
}

body {
  height: 400%;
  width: 100%;
}

.wrap-fixed-real {
  height: 100vh;
  width: 100%;
  position: fixed;
}

.h-work {
  display: flex;
  opacity: 1;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.h-work-bottom {
  display: flex;
  opacity: 1;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

h1 {
  position: relative;
  padding: 0px 48px;
  width: 100%;
  max-width: 640px;
  display: block;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 26px;
  text-align: left;
  line-height: 34px;
  color: #000000;
}

.hide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #fff;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div class="wrap-fixed-real h-work">
    <h1>Scroll down</h1>
  </div>
  <div class="wrap-fixed-real h-work-bottom">
    <h1>
      <div class="hide"></div>More, even worse projects to come.</h1>
  </div>
</body>

</html>

如您所见,在 var f 达到 0 之前,用户无法突出显示底部的文本。我知道,没有人会尝试选择一些随机文本,但对我来说这并不专业。我做了同样的事情,但使用了两种不同的方法。我真的希望我的代码更加一致。

所以这是我的问题:一个人可以实现与上面示例中看到的相同的结果,但文本淡入而不是 div 淡出吗?

编辑:我已经看到(实际上是几秒钟)来自某个陌生人的回复,他说 pointer-events: none; 解决了问题能够选择 div .cover 下的文本。有用!目前我认为这是要走的路。我不知道你为什么删除你的回复。

最佳答案

$(".h-work-bottom").hide();
$(window).on("scroll", function() {

  $(".h-work").css("opacity", 1 - $(window).scrollTop() / 320);
  if ($(window).scrollTop() >= 320) {
    $(".h-work").hide();
  } else {
    $(".h-work").show();
  }

  var t = 320;
  var sb = (($(document).height() - $(window).height()) - $(window).scrollTop());
  var f = sb / t;

  if (sb >= 320) {
    $(".h-work-bottom").hide();
  } else {
    $(".h-work-bottom").css("opacity", 1 - f);
    $(".h-work-bottom").show();
  }

  if (f <= 0) {
    $(".hide").hide();
  } else {
    $(".hide").show();
  }

});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  width: 100%;
  height: 100%;
  overflow-y: scroll;
}

body {
  height: 400%;
  width: 100%;
}

.wrap-fixed-real {
  height: 100vh;
  width: 100%;
  position: fixed;
}

.h-work {
  display: flex;
  opacity: 1;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.h-work-bottom {
  display: flex;
  opacity: 1;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

h1 {
  position: relative;
  padding: 0px 48px;
  width: 100%;
  max-width: 640px;
  display: block;
  font-family: 'Roboto', sans-serif;
  font-weight: normal;
  font-size: 26px;
  text-align: left;
  line-height: 34px;
  color: #000000;
}

.hide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #fff;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div class="wrap-fixed-real h-work">
    <h1>Scroll down</h1>
  </div>
  <div class="wrap-fixed-real h-work-bottom">
    <h1>
      More, even worse projects to come.</h1>
  </div>
</body>

</html>

您的主要问题是您的 H1 和 div.hide 未正确关闭,并且您在宽度为 0 的元素内有一个绝对位置元素。

我实际上已经设法取出 .hide 元素,并保留标记就像顶部一样。完成所有这些后,底部逐渐淡出而不是淡入。我必须将 f 减去 1,而不是使用 f 来反转效果。

关于jquery - 在页面底部淡入(逐渐),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038782/

相关文章:

javascript - 单击 div 时如何停止 youtube 视频?

javascript - 如何将元素拆分为两个可连续的部分并在它们之间添加另一个元素

html - CSS - 按钮不对齐

javascript - 如何使滚动条居中?

java - 无法在 apache tomcat 中使用 Context docBase 显示 html 图像

html - 在 FireFox 中对齐链接和提交按钮

javascript - 未捕获的语法错误 : Invalid or unexpected token even with correct syntax

javascript - 带有 will_paginate 的 jQuery Endless 页面在 Rails 中不起作用

html - 我的响应式 html 代码有什么问题

html - text-overflow 改变省略号的内容