繁体   English   中英

视差缩小效果问题

[英]Parallax zoom out effect issues

我正在尝试使用视差并尝试在滚动时获得很好的缩小,但是我正在努力使图像变得小于浏览器的宽度和div的高度。

如您在我的示例中所见,向下滚动时,包装部分的红色背景可见。

您可以在www.adamkhourydesign.com/test上查看示例。

HTML

<header id="header_container">
    <div class="header_back"></div>
    <div class="logo"></div>
</header>

CSS

#header_container {
  position: relative;
  height: 600px;
  background-color: rgba(255, 100, 85, 0.5);
  background-repeat: no-repeat;
  background-size: auto 800px;
  background-position: top center;
  background-attachment: fixed;
  overflow: hidden;
}

.logo {
  height: 100px;
  width: 100%;
  background-image: url(../img/name.png);
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  top: 50%;
  margin-top: -50px;
}

.header_back {
  position: relative;
  height: 600px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: top center;
  background-attachment: fixed;
  background-image: url(../img/header_bg.jpg);
  overflow: hidden;
}

jQuery的

var pContainerHeight = $('#header_container').height();

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();
  var wZoomIn = 1+(wScroll*.0005);
  var wZoomOut = 1-(wScroll*.00005);

  if (wScroll <= pContainerHeight) { 
    $('.header_back').css({
      'transform' : 'scale('+ wZoomOut +')'
    });
    $('.logo').css({
      'transform' : 'translate(0px, '+ wScroll /0.7 +'%)'
    });
  }

修改您的代码,我可以在滚动时获得平滑的视差缩小,但是使用background-size属性而不是transform: scale() 我通过首先将背景图像稍微放大(例如105% ),然后在向下滚动时将其逐渐缩小回100%来做到这一点。 我还确保background-size不会低于100% ,以使其始终覆盖整个标头区域。

CSS

...
.header__back {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url("http://www.adamkhourydesign.com/test/img/header_bg.jpg") top center no-repeat fixed;
    background-size: 105%; /** the initial zoom **/
    overflow: hidden;
}
...

使用Javascript

var height = $('#header_container').height();
var logo = $('.logo');
var background = $('.header__back');

$(window).on('scroll', function() {
    var scroll = $(this).scrollTop();

    logo.css('transform', 'translateY(' + scroll / 0.7 + '%)');

    var backgroundSize = 105 - (5 - (height - scroll) / height * 5);
    backgroundSize = backgroundSize < 100 ? 100 : backgroundSize;
    background.css('background-size', backgroundSize + '%');
});

查看此小提琴以查看其实际效果: 视差缩小演示

感谢Arnell Balance,通过您发布的答案,我设法略微调整了代码以使其正常运行。

我将背景图像从Background属性中移出,并将其作为图像添加到div中。 然后在CSS中,我制作了图像,例如140%的宽度和高度自动。 并最终将超过100%的金额的左边距补偿为-20%。

现在的代码如下所示:

HTML

    <header id="header_container">
            <div class="header_back">
                <img src="img/header_bg.jpg">
            </div>
            <div class="logo"></div>
   </header>

JQUERY

var pContainerHeight = $('#header_container').height();

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();
  var wZoomIn = 1+(wScroll*.0005);
  var wZoomOut = 1-(wScroll*.0005);

  if (wScroll <= pContainerHeight) { 
    $('.header_back img').css({
      'transform' : 'scale('+ wZoomOut +')'
    });
    $('.logo').css({
      'transform' : 'translate(0px, '+ wScroll /0.7 +'%)'
    });
  }

CSS

#header_container {
  position: relative;
  height: 600px;
  width: 100%;
  background-color: rgba(255, 100, 85, 0.5);
  background-repeat: no-repeat;
  background-size: auto 800px;
  background-position: top center;
  background-attachment: fixed;
  overflow: hidden;
}

.logo {
  height: 100px;
  width: 100%;
  background-image: url(../img/name.png);
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  top: 50%;
  margin-top: -50px;
}

.header_back {
  position: relative;
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;
  background-size: 100%;
  background-position: top center;
  background-attachment: fixed;
  background-color: rgba(154, 100, 85, 0.5);
  text-align: center;
  overflow: hidden;
}
.header_back img {
  height: auto;
  width: 140%;
  margin-left: -20%;
}

暂无
暂无

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

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