jquery - 改变滚动背景颜色

标签 jquery css colors

我将此 javascript 与 colors.js 结合使用,可以更改滚动条的背景颜色。这很好用,我唯一想做的是下一个 div 出现时淡入淡出已经完成。

我的codepen的例子;当您开始滚动时,背景会慢慢从白色变为黑色,但我希望当#two 部分出现在底部时它已经完全变黑了。

代码笔: https://codepen.io/pixelarchitect/pen/PxBmXB

var sections = [];
var sectionsYStart = [];
var activeSection = 0;

var pageInit = function() {
  sections = [];
  sectionsYStart = [];
  $("section").each(function(i, v) {
    sections[i] = v;
    sectionsYStart[i] = $(v).offset().top;
  });
};

var ChangeColorOnScroll = function() {
  var scroll = $(window).scrollTop();
  scrollColors(scroll, $("body"), ["#FFFFFF", "#000000"]);
}

var scrollColors = function(scroll, el, colors) {
  // which of all the sections, are we in between?
  var z = 0,
    seclen = sections.length;
  for (var i = 0; i < seclen; i++) {
    if (scroll > sectionsYStart[i]) {
      z = i;
    }
  }
  activeSection = z;

  scroll_pos = scroll;
  var animation_begin_pos = sectionsYStart[z]; //where you want the animation to begin
  var animation_end_pos = sectionsYStart[z + 1]; //where you want the animation to stop
  var beginning_color = $.Color(colors[z]);
  var ending_color = $.Color(colors[z + 1]);

  if (scroll_pos >= animation_begin_pos && scroll_pos <= animation_end_pos) {
    var percentScrolled = scroll_pos / (animation_end_pos - animation_begin_pos);
    if (percentScrolled > 1) {
      percentScrolled = percentScrolled - z;
    }
    var newRed = beginning_color.red() + ((ending_color.red() - beginning_color.red()) * percentScrolled);
    var newGreen = beginning_color.green() + ((ending_color.green() - beginning_color.green()) * percentScrolled);
    var newBlue = beginning_color.blue() + ((ending_color.blue() - beginning_color.blue()) * percentScrolled);

    var newAlpha = beginning_color.alpha() + ((ending_color.alpha() - beginning_color.alpha()) * percentScrolled);

var newColor = new $.Color(newRed, newGreen, newBlue, newAlpha);
el.animate({
  backgroundColor: newColor
}, 0);
  } else if (scroll_pos > animation_end_pos) {
    el.animate({
      backgroundColor: ending_color
    }, 0);
  } else if (scroll_pos < animation_begin_pos) {
    el.animate({
      backgroundColor: beginning_color
    }, 0);
  } else {}

};

$(function() {
  pageInit();
  $(document).scroll(ChangeColorOnScroll);
  $(window).resize(pageInit);
});

如果有人还可以帮助我在滚动时更改文本的颜色,那将是一个不错的额外功能

最佳答案

你可以给你的 animation_end_pos 添加一个偏移来完成你想要的

scroll_pos = scroll;
  var animation_begin_pos = sectionsYStart[z]; //where you want the 
animation to begin
  var animation_end_pos = sectionsYStart[z + 1] - $(window).height() - 50; 
//where you want the animation to stop
  console.log($(window).height() - 50);

关于jquery - 改变滚动背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489036/

相关文章:

javascript - Kohana/JQuery Auth 访问问题

输入类型 ="file"上的 jQuery 更改方法

css - 为什么 CSS 没有应用 flex-mobile 字段?

c# - 如何在 SQLite 数据库中存储 Windows.UI.Color 对象?

javascript - Winston 3.0 为控制台上的整个输出着色

javascript - 如何创建一个函数来将新数组添加到 JavaScript 中的对象

javascript - 使用多个按钮触发同一个弹出框

javascript - 如何在 Canvas 中绘制具有适当坐标的箭头

javascript - 如何通过 CSS 和 JS 让我的背景图像每 15 秒左右更改一次

Javascript 在点击时改变颜色并保持相同的颜色