[英]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.