0
我有這個簡化代碼來爲我的圖像添加視差。中心視差圖像
當我開始滾動時,最高值不正確。距離太大,當我回到網站的頂部時,圖片不像開頭那樣居中。
如何計算正確的最高值?
$(document).ready(function(){
\t var scrolledDefault = $(window).height()/2 - ($('.img__moi').height()/2) + 25;
\t $('.img__moi').css('top', scrolledDefault);
\t $(window).scroll(function(e){
\t \t parallax('.img__moi', '0.2');
\t });
\t function parallax(element, vitesse){
\t \t var scrolled = $(window).scrollTop() + ($(window).height()/2) - ($(element).height()/2) + 25;
\t \t console.info(scrolled*vitesse);
\t \t $(element).css('top',-(scrolled*vitesse)+'px');
\t }
});
body{
background-color:#cccccc;
height:3000px;
}
.align__text{
position:absolute;
top:calc(50% + 30px);
left:calc(25% + 20px);
width:70%;
-webkit-transform:translateY(-50%);
transform:translateY(-50%);
z-index:2;
}
.header__top{
background-color:$blanc;
padding:5px 0px;
height:50px;
position:fixed;
top:0;
left:0;
right:0;
z-index:9999999;
}
.img__moi{
float:left;
width:25%;
position:absolute;
margin-left:50px;
z-index:2;
transition:all 300ms ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div class="header__top"></div>
<img src="http://img15.hostingpics.net/pics/317625pexelsphoto.jpg" class="img__moi">
\t \t \t <div class="align__text">
\t \t \t \t <h1>The title here<br/> Two line</h1>
\t \t \t </div>
</header>
謝謝。 這比我找到的解決方案更好! –
感謝允許我的代表通過1k標記! :) – Sam0