jQuery-更改滚动条的背景色 [英] jQuery - Change background colour on scroll

查看:91
本文介绍了jQuery-更改滚动条的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在滚动时转换固定标题元素的背景色.因此,当用户向下滚动整页的块网站时,标题会巧妙地更改以补充块的颜色.我几乎已经在上实现了这一目标,但是我不太确定如何测量滚动了多少作为更改时间的标志.

I want to transition the background colour of a fixed header element on scroll. So as a user scrolls down a full page block website, the header subtly changes to complement the block colours. I have almost achieved this on a Pen, however I can't quite work out how to measure how much has been scrolled as a flag for when to change.

一些其他信息:要更改的滚动量为400px.背景颜色以阵列形式存储和提取.作为参考,我的jQuery代码如下:

Some extra info: The scroll amount to change at is 400px. The background colours are stored and fetched in an array. For reference my jQuery code is below:

$(document).ready(function(){
  var bgArray = ["#252525","#333333","#454545","#777777"];  
  var scrollHeight = 400;
  var scrolled = $(window).scrollTop(); //What is this measuring?

  $(window).scroll(function() { //Can these conditions be neatened into one function?
    if(scrolled < scrollHeight) {
      $('header').css('background', bgArray[0]);
    }
    if(scrolled > scrollHeight) { // i.e more than 400px
      $('header').css('background', bgArray[1]);
    }
    // and so on (800, 1200...)
  })
})

有关完整代码,请参阅笔.任何建议,我们将不胜感激!

Please refer to the Pen for full code. Any suggestions are greatly appreciated!

推荐答案

更新的解决方案(2019)

要在滚动时在header下方的视图中基于当前header设置background:

Updated Solution (2019)

To set a background for the header based on the current block in view below the header while scrolling:

  • 因为header具有固定位置,所以我们可以通过$header.offset().top获得窗口滚动的量

  • because header has fixed position, we can get the amount by which window has scrolled by using $header.offset().top,

(视图中当前块的索引)是(窗口滚动量)与(高度的每个区块),

(index of the current block in view) is the ratio of (the amount by which window has scrolled) to the (height of each block),

现在调整header的高度,视图中当前块的索引为Math.floor(($header.offset().top + headerHeight) / sectionHeight).

now adjusting for the height of the header, the index of the current block in view is Math.floor(($header.offset().top + headerHeight) / sectionHeight).

请参见下面的简化演示

$(function() {
  var $header = $('header'),
    $window = $(window),
    bgArray = ["#252525", "red", "blue", "green"],
    headerHeight = 50,
    sectionHeight = 400;

  $window.scroll(function() {
    $header.css('background', bgArray[Math.floor(($header.offset().top + headerHeight)
        / sectionHeight)]);
  });
});

:root {
  --header: 50px; /* header height */
  --block: 400px; /* block height */
}

* {
  box-sizing: border-box; /* include padding in width / height calculations */
}

body {
  margin: 0; /* reset default margin of body */
}

header {
  height: var(--header); /* sets height of header */
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  color: #FFF;
  padding: 12px 0;
  background: #252525; /* initial background */
  transition: background 1s ease;
}

.container {
  margin: 0 auto;
}

.wrap>div {
  height: var(--block); /* sets height of each block */
  text-align: center;
}

p {
  margin: 0; /* reset margin of p */
}

.block-1 {
  background: #27AACC;
  color: #FFF;
}

.block-2 {
  background: #668E99;
  color: #FFF;
}

.block-3 {
  background: #4AFFC1;
  color: #444;
}

.block-4 {
  background: #FF8F8A;
  color: #FFF;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <div class="container">
    Website Title.
  </div>
</header>

<div class="wrap">
  <div class="block-1">
    <div class="container">
      <p>This pen was made to solve a problem on a project...</p>
    </div>
  </div>
  <div class="block-2">
    <div class="container">
      <p>...I needed a sticky header with thr right bg colour.</p>
    </div>
  </div>
  <div class="block-3">
    <div class="container">
      <p>But this conflicted with the footer, which was the same colour...</p>
    </div>
  </div>
  <div class="block-4">
    <div class="container">
      <p>So the solution was to subtley change the header's bg on scroll</p>
    </div>
  </div>
</div>

检查每个blocktop关于使用$(window).scrollTop() > $('.block-1').offset().top滚动了多少窗口(scrollTop).因此,现在我们可以在进入块时使用它来更改颜色-参见下面的演示:

Check the top of each block with respect to how much the window has been scrolled (scrollTop) using $(window).scrollTop() > $('.block-1').offset().top. So now we can use this to change color on entering the block - see demo below:

$(document).ready(function() {
  var $header = $('header'),
    $window = $(window),
    bgArray = ["#252525", "#333333", "#454545", "#777777"],
    headerHeight = $header.outerHeight();

  $window.scroll(function() {
    for (var i = 1; i < 5; i++) {
      if ($window.scrollTop() + headerHeight > $('.block-' + i).offset().top) {
        $header.css('background', bgArray[i - 1]);
      }
    }
  });
});

@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,700');
body {
  font-family: 'Roboto', sans-serif;
  font-size: 30px;
  font-weight: 300;
  margin-top: 0;
}

header {
  width: 100%;
  height: 50px;
  line-height: 50px;
  position: fixed;
  font-size: 24px;
  font-weight: 700;
  color: #FFF;
  padding: 12px 0;
  margin: 0;
  background: #252525;
  transition: background 1s ease;
}

.wrap {
  padding-top: 74px;
  margin: 0;
}

.container {
  width: 960px;
  margin: 0 auto;
  overflow: hidden;
}

.block-1,
.block-2,
.block-3,
.block-4 {
  height: 400px;
  text-align: center;
}

p {
  margin-top: 185px;
}

.block-1 {
  background: #27AACC;
  color: #FFF;
}

.block-2 {
  background: #668E99;
  color: #FFF;
}

.block-3 {
  background: #4AFFC1;
  color: #444;
}

.block-4 {
  background: #FF8F8A;
  color: #FFF;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <div class="container">
    Website Title.
  </div>
</header>
<div class="wrap">
  <div class="block-1">
    <div class="container">
      <p>This pen was made to solve a problem on a project...</p>
    </div>
  </div>
  <div class="block-2">
    <div class="container">
      <p>...I needed a sticky header with thr right bg colour.</p>
    </div>
  </div>
  <div class="block-3">
    <div class="container">
      <p>But this conflicted with the footer, which was the same colour...</p>
    </div>
  </div>
  <div class="block-4">
    <div class="container">
      <p>So the solution was to subtley change the header's bg on scroll</p>
    </div>
  </div>
</div>

请注意,此解决方案会不必要地循环浏览器调用的每个滚动更新中的各个部分-我不喜欢它的外观.

Note that this solution needlessly loops through the sections on each scroll update called by the browser - and I don't like the look of it.

这篇关于jQuery-更改滚动条的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆