更改背景颜色的CSS滚动链接定位效果警告

时间:2017-01-08 09:14:26

标签: jquery html css scroll

当新的部分成为最明显的部分时,我想要一个background-transaparant-color-overlay更改效果。它按原样运行,但是给了我一个警告:

  

此网站似乎使用滚动链接定位效果。这可能不适用于异步平移;有关详细信息,请参阅https://developer.mozilla.org/docs/Mozilla/Performance/ScrollLinkedEffects并加入有关相关工具和功能的讨论!   https://fiddle.jshell.net/_display/

这种效果是否可以在没有警告的情况下实现?

请参阅带有警告at this fiddle的示例。

$(window).on("load scroll touchmove", function() {
  var sections = ["#first", "#second", "#third", "#four", "#five"];
  var colors = [
    "rgb(255, 255, 255)", //white
    "#ffC0CB", //pink
    "rgb(0, 255, 0)", //green
    "rgb(223, 181, 182)",
    "rgb( 98,  83,  80)",
    "rgb(157, 163, 135)",
    "rgb(208, 212, 189)",
  ];

  var spacer = $(window).height() * 0.4;
  var scrollTop = $(document).scrollTop();
  $.each(sections, function(index, value) {
    //console.log("each " + index + ", " + value);
    if ($(value).length) {
      if (index == 0 || index == sections.length - 1) {
        if (scrollTop >= $(value).position().top - spacer) {
          $('body').css('background-color', colors[index]);
        };
      } else {
        if (scrollTop > $(value).position().top - spacer) {
          $('body').css('background-color', colors[index]);
        };
      }
    } else {
      console.log('Background color scroll: $(' + value + ') not found.');
    }
  });
});
section {
  min-height: 100vh;
}
body {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  border: 0 none;
  padding: 0 0 0 0;
  margin: 0 0 0 0;
  background-color: red;
}
body::after {
  content: "";
  z-index: -1;
  background: url(https://unsplash.it/1300/1150);
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: fixed;
  background-repeat: no-repeat;
  background-size: contain;
  width: 100%;
  height: 100%;
  background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <section id="first">
    first first
  </section>
  <section id="second">
    second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
    <br>second
  </section>
  <section id="third">
    third
  </section>
  <section id="four">
    4
  </section>
  <section id="five">
    five
  </section>
  <div id="bg">bla</div>
</body>

1 个答案:

答案 0 :(得分:0)

使用靶心库

时有效

https://jsfiddle.net/sottobe/xjtxnqwa/13/

    //using bullseye from http://static.pixeltango.com/jQuery/Bullseye/
$(function() {
    var sections =  {
            first:"rgb(255, 255, 255)", 
            second: "#ffC0CB", 
            third: "rgba(104, 212, 94, 0.4)", 
            four: "#e6f5ff", 
            five: "#e0e0eb", 
            six:"rgb(157, 163, 135)",
                    };
    function changebgcolor(e) {
            var color = sections[$(this).attr('id')];
            console.log($(this).attr('id') + ': ' + e.type + ' --> ' + color);
            $('body').css('background-color', color);
    }
    alert('binding');
    $('section').bind('enterviewport', changebgcolor).bullseye();
});