スクロールに応じて背景画像を徐々にblurさせる

Ads

Result

jQuery

$(function() {
  zoom = $('.feature').css('background-size')
  zoom = parseFloat(zoom) / 100
  size = zoom * $('.feature').width();
  $(window).on('scroll', function() {
    fromTop = $(window).scrollTop();
    newSize = size - (fromTop / 3);
    if (newSize > $('.feature').width()) {
      $('.feature').css({
        '-webkit-background-size': newSize,
        '-moz-background-size': newSize,
        '-o-background-size': newSize,
        'background-size': newSize,
        '-webkit-filter': 'blur(' + 0 + (fromTop / 100) + 'px)',
        'opacity': 1 - ((fromTop / $('html').height()) * 1.3)
      });
    }
  });
});

$(function() {
  var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);

  if (isChrome || isSafari) {} else {
    $('.feature').append('<div class="opaque"></div>');
    $(window).on('scroll', function() {
      var opacity = 0 + ($(window).scrollTop() / 5000);
      $('.opaque').css('opacity', opacity);
    });
  }
});

css

.opaque {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  background: #d2d6f1;
}

.lt-ie9 .opaque {
  display: none;
}

.feature {/*背景画像*/
  position: fixed;
  top: 0;
  z-index: 0;
  width: 100%;
  padding-top: 50%;
  background: url(https://picsum.photos/1500) center center no-repeat;
/*background-sizeが本スクリプトのキモの部分*/
  -webkit-background-size: 250%;
  -moz-background-size: 250%;
  -o-background-size: 250%;
  background-size: 250%;
  -webkit-box-shadow: 0 -50px 20px -20px #232323 inset;
  -moz-box-shadow: 0 -50px 20px -20px #232323 inset;
  box-shadow: 0 -50px 20px -20px #232323 inset;
}

.content {
  position: relative;
  z-index: 1;
  padding-top: 45%;
  width: 90%;
  margin: 0 auto;
}

html

<div class="feature"></div>
<div class="content">
foo bar
</div>

via

Zoom and Blur background Image

タイトルとURLをコピーしました