简体   繁体   English

背景图像之间的平滑过渡

[英]Smooth Transition Between Background Images

So, I made this script where the background changes the farther you scroll down the page, but I want it so there is a transition between each of the images. 因此,我制作了此脚本,其中,随着您向下滚动页面的距离,背景会发生变化,但是我希望这样做,因此每个图像之间都有一个过渡。 So when you scroll from one image to the next, it slowly fades into the next. 因此,当您从一个图像滚动到下一个图像时,它会慢慢淡入下一个图像。 Sort of like a parallax. 有点像视差。

JS: JS:

$(document).ready(function() {
  $("body ").css("background-image", "url('http://i.imgur.com/rs2Ittp.jpg')");
  $(window).scroll(function() {
    if($(this).scrollTop() > 0) {
     $("body ").css("background-image", "url('http://i.imgur.com/rs2Ittp.jpg')");
    }
    if($(this).scrollTop() > 1000) {
      $("body ").css("background-image", "url('http://i.imgur.com/H5QLuD6.jpg')");
    }
    if($(this).scrollTop() > 2000) {
      $("body ").css("background-image", "url('http://i.imgur.com/KzZpgdS.jpg')");
    }
    if($(this).scrollTop() > 3000) {
     $("body ").css("background-image", "url('http://i.imgur.com/UsLLJSx.jpg')");
    }
  });
});

Any solutions are appreciated (: 任何解决方案表示赞赏(:

The easiest solution is through CSS: 最简单的解决方案是通过CSS:

body {
    transition: background-image 0.5s ease-in-out;
}

Edit: My answer may have been a bit premature, since this is not a cross-browser solution. 编辑:我的回答可能还为时过早,因为这不是跨浏览器的解决方案。 A better way to do this would be by using two divs with different background images using transition: opacity 0.5s; 更好的方法是使用两个具有不同背景图像的div,并使用transition: opacity 0.5s; A lot more javascript is involved in this solution though. 但是,此解决方案涉及更多的JavaScript。

Here's one way of doing it. 这是一种方法。 Create two elements with position: fixed , then change the opacity as you scroll. 创建两个具有position: fixed元素position: fixed ,然后在滚动时更改不透明度。 Once you get over a certain scroll position, swap the next images in and use the same opacity logic minus the intended scroll length. 一旦超过某个滚动位置,就交换下一张图像,并使用相同的不透明度逻辑减去预期的滚动长度。

edit: my opacity math could be more refined + need to add swapping the images back when scrolling the reverse direction 编辑:我的不透明度数学可以更精确+反向滚动时需要添加交换图像

<!-- html -->
<div class="bottom"></div>
<div class="top"></div>

/* css */
.bottom,
.top {
  position: fixed;
  background-size: cover;
  height: 100%;
  width: 100%;
}

// js
var img1 = 'url(http://i.imgur.com/rs2Ittp.jpg)'
  , img2 = 'url(http://i.imgur.com/H5QLuD6.jpg)'
  , img3 = 'url(http://i.imgur.com/KzZpgdS.jpg)'

$(document).ready(function() {
  $('.top').css("background-image", img1)
  $('.bottom').css("background-image", img2)

  $(window).scroll(function() {
    var scrollTop = $(this).scrollTop()

    if (scrollTop < 1000) {
      $('.top').css('opacity', 100 / scrollTop)
    }

    else if ($(this).scrollTop() > 1000) {
      $('.top').css("background-image", img2)
      $('.bottom').css("background-image", img3)
      $('.top').css('opacity', 100 / (scrollTop - 1000))
    }
  })
})

codepen codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM