滚动

时间:2018-01-21 08:46:33

标签: javascript jquery css

我有一些代码可以在用户滚动时移动标题图像。它似乎工作正常:

<script>
$(window).scroll(function(e) {
                var scrolled = $(window).scrollTop(),
                    header = $('.site-header');

                header.css('top', -(scrolled) * 0.5 + 'px');

                if (scrolled > $(document).height() - $(window).height() - 470) {
                    var scrollPos = scrolled - ($(document).height() - $(window).height() - 470);
                    var position = scrollPos / 10;
                    var opacity = (scrollPos * 1) / 470;
                }
            });
</script>

我正在尝试改变不透明度,所以当我向下滚动时它会淡出。这一点似乎不起作用。

这个完整的代码在这里:

https://jsfiddle.net/spadez/acz13129/1/

代码中似乎已经出现了一些关于不透明度的内容,但它并没有改变,控制台也没有抛出任何错误,所以我有点卡住了。

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
ifC
&#13;
$(window).scroll(function(e) {
  var scrolled = $(window).scrollTop(),
    header = $('.site-header');

  header.css('top', -(scrolled) * 0.5 + 'px');

  var scrollPos = scrolled - ($(document).height() - $(window).height() - 470);
  var position = scrollPos / 10;
  var opacity = ((scrollPos / -1000) - 1.734) * 1.5; // Opacity is in decimals e.g. 0.5
  $('.site-header').css({
    "opacity": opacity,
    "position": position + '%'
  });
});
&#13;
body {
  margin: 0;
  background: #f1f1f1;
}

header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: -1;
  height: 500px;
  line-height: 500px;
  text-align: center;
  margin: 0;
  padding: 0;
}

.wrapper-parallax {
  margin-top: 500px;
}

.content {
  border-top: 5px solid white;
  position: relative;
  z-index: 1;
  background: white;
  min-height: 2800px;
  background-image: url(https://i.imgur.com/DCXgzWc.png);
  background-position-y: top;
  background-repeat: no-repeat;
  background-size: 300%;
  background-position-x: center;
}
&#13;
&#13;
&#13;

这是你想要的吗?