【问题标题】:want image to scroll up, then freeze, then text to scroll over it希望图像向上滚动,然后冻结,然后文本滚动它
【发布时间】:2018-02-06 18:19:21
【问题描述】:

我有一个元素的背景图像比屏幕高度高一点。当用户向下滚动时,我希望图像向上滚动,直到图像底部与窗口底部齐平,然后冻结图像,然后让文本在图像顶部滚动。

我有两个问题。一个是,我希望文本具有透明背景,因此它在图像上流动,而没有覆盖背景图像的白色背景。如果用户以另一种方式滚动,另一个是将所有内容反转。请问有人有解决办法吗?

$(document).ready(function() {

  var heightOfIntro = $('#partone').height() - $(window).height();


  $(function() {
    $(window).scroll(function() {
      if ($(this).scrollTop() >= heightOfIntro) {
        $('.parallax').css('background-attachment', 'fixed');

      } else {
        $('.parallax').css('background-attachment', 'absolute');
      }


    });
  });


});
.parallax {
  /* Full height */
  height: 1180px;
  background-attachment: absolute;
  background-position: right top;
  background-repeat: no-repeat;
  position: relative;
}

.parallax#partone {
  background-image: url('../img/rogers_bg.jpg');
  background-color: #eaf1f4;
}

.story {
  width: 50%;
  padding: 20px;
  overflow: hidden;
}

.story p {
  color: black;
  font-family: 'proxima-nova', sans-serif;
  font-size: 100%;
  line-height: 110%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parallax" id="partone">
  <div class="story">
    <p>Mr. Utterson the lawyer was a man of a rugged countenance, that was never lighted by a smile; cold, scanty and embarrassed in discourse; backward in sentiment; lean, long, dusty, dreary, and yet somehow lovable. At friendly meetings, and when the
      wine was to his taste, something eminently human beaconed from his eye; something indeed which never found its way into his talk, but which spoke not only in these silent symbols of the after-dinner face, but more often and loudly in the acts of
      his life. He was austere with himself; drank gin when he was alone, to mortify a taste for vintages; and though he enjoyed the theatre, had not crossed the doors of one for twenty years. But he had an approved tolerance for others; sometimes wondering,
      almost with envy, at the high pressure of spirits involved in their misdeeds; and in any extremity inclined to help rather than to reprove. I incline to Cain's heresy, he used to say quaintly: I let my brother go to the devil in his own way. In
      this character it was frequently his fortune to be the last reputable acquaintance and the last good influence in the lives of down-going men. And to such as these, so long as they came about his chambers, he never marked a shade of change in his
      demeanour.
    </p>

  </div>
</div>

【问题讨论】:

  • 你基本上试图达到与置顶标题相同的效果。
  • 你需要知道图片的高度。 B. 窗口对图像的偏移量。 C. 将两者相加。 D. 在 C 的值处设置一个窗口触发器。
  • @Cam:在 jquery 代码中,我设置了触发器。问题是:1)文本背景覆盖背景图像和2)如何撤消效果。我的撤消代码不起作用。
  • 在底部查看我的代码。不要使用 CSS。它是问题的原因,使用类来添加/减去或撤消。

标签: jquery css scroll background-image window-scroll


【解决方案1】:

这只是如何解决问题的想法。我认为还有更多。你需要做一些错误检查。但我没有这个经过有效测试的自动取款机。

  $(document).ready(function() {
  function scrollStatus(){
      var offheight = $('.parallax').offset().top; //get height of the image
      if ($(this).scrollTop() >= offheight) {

        $('.parallax').removeClass('fixedAb').addClass('fixedPos'); //use addclass to simplify the add subtract of the position

      } else {
        $('.parallax').removeClass('fixedPos').addClass('fixedAb');
      }
  }
  scrollStatus();
  $(window).on('scroll',function(){
    scrollStatus();
  });

});

【讨论】:

  • 可能然后删除offheight,但如果你没有得到实际img的高度,你就没有得到真正的偏移高度。相信我,我在跳转生效时遇到了这个问题。
  • 它不起作用。图像保持固定,完全不滚动,文本向上滚动,背景覆盖视差元素。
  • codepen.io/designsbycamaron/pen/RQGZda 这是一支带有您当前代码的笔。
  • 修复了!现在工作
  • 发生了两件令人困惑的事情,这导致了您的问题。一,你的背景是 1180px 高度。为什么?其次,文本堆叠在顶部。因此,您只想滚动到底部,但文本在到达底部之前就消失了。所以这很令人困惑。你有图表或示例模型吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
相关资源
最近更新 更多