jquery - Text opacity fade when at edge of viewport -
i'm trying .title fade when it's close top/bottom of screen. think problem jquery document targetting. can see, initial fade works perfectly, text not fade when close top/bottom.
you can view codepen here.
any appreciated!
function scrollbanner() { $(document).scroll(function() { var scrollpos = $(this).scrolltop(); $('.title').css({ 'top': (scrollpos / 3) + 'px', 'opacity': 1 - (scrollpos / 100) }); $('.title').css({ 'background-position': 'center ' + (-scrollpos / 200) + 'px' }); }); } scrollbanner(); $(document).ready(function() { $(".title").delay(1000).hide().fadein(2000); });
/* parallax base styles --------------------------------------------- */ .parallax { height: 500px; /* fallback older browsers */ height: 100vh; overflow-x: hidden; overflow-y: auto; -webkit-perspective: 300px; -webkit-overflow-scrolling: touch; perspective: 300px; } .parallax__group { position: relative; height: 500px; /* fallback older browsers */ height: 100vh; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } .parallax__layer { position: absolute; top: 0; left: 0; right: 0; bottom: 0; } .parallax__layer--fore { -webkit-transform: translatez(90px) scale(.7); transform: translatez(90px) scale(.7); z-index: 1; } .parallax__layer--base { -webkit-transform: translatez(0); transform: translatez(0); z-index: 4; } .parallax__layer--back { -webkit-transform: translatez(-300px) scale(2); transform: translatez(-300px) scale(2); z-index: 3; } .parallax__layer--deep { -webkit-transform: translatez(-600px) scale(3); transform: translatez(-600px) scale(3); z-index: 2; } /* demo styles --------------------------------------------- */ body, html { overflow: hidden; } body { font: 100% / 1.5 arial; } * { margin: 0; padding: 0; } .parallax { font-size: 200%; } /* centre content in parallax layers */ .title { text-align: center; position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } /* style groups --------------------------------------------- */ #group1 { z-index: 5; /* slide on group 2 */ } #group1 .parallax__layer--base { background: rgb(102, 204, 102); } #group2 { z-index: 3; /* slide under groups 1 , 3 */ } #group2 .parallax__layer--back { background: rgb(123, 210, 102); }
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-it6q9imjyuqimwnd9ldybustiq/8puow33aoqmvfpqi=" crossorigin="anonymous"></script> <div class="parallax"> <div id="group1" class="parallax__group"> <div class="parallax__layer parallax__layer--base"> <div class="title">base layer lorem ipsum dolor sit amet, consectetur adipiscing elit. ut tellus risus, vestibulum non neque ut, consectetur fermentum neque. nam pharetra tellus pulvinar ante suscipit dapibus. quisque pharetra libero vel lectus placerat laoreet.</div> </div> </div> <div id="group2" class="parallax__group"> <div class="parallax__layer parallax__layer--base"> <div class="title">base layer</div> </div> <div class="parallax__layer parallax__layer--back"> <div class="title">background layer</div> </div> </div>
i took closer @ code , ran in jsfiddle , figured out why scroll event wasn't firing. changed $(document).scroll
$('.parallax').scroll
because you're scrolling .parallax
element not document. moved call scrollbanner()
in $(document).ready
function scrollbanner() { $('.parallax').scroll(function() { var scrollpos = $(this).scrolltop(); $('.title').css({ 'top': (scrollpos / 3) + 'px', 'opacity': 1 - (scrollpos / 100) }); $('.title').css({ 'background-position': 'center ' + (-scrollpos / 200) + 'px' }); }); } $(document).ready(function() { $(".title").delay(1000).hide().fadein(2000); scrollbanner(); });
/* parallax base styles --------------------------------------------- */ .parallax { height: 500px; /* fallback older browsers */ height: 100vh; overflow-x: hidden; overflow-y: auto; -webkit-perspective: 300px; -webkit-overflow-scrolling: touch; perspective: 300px; } .parallax__group { position: relative; height: 500px; /* fallback older browsers */ height: 100vh; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } .parallax__layer { position: absolute; top: 0; left: 0; right: 0; bottom: 0; } .parallax__layer--fore { -webkit-transform: translatez(90px) scale(.7); transform: translatez(90px) scale(.7); z-index: 1; } .parallax__layer--base { -webkit-transform: translatez(0); transform: translatez(0); z-index: 4; } .parallax__layer--back { -webkit-transform: translatez(-300px) scale(2); transform: translatez(-300px) scale(2); z-index: 3; } .parallax__layer--deep { -webkit-transform: translatez(-600px) scale(3); transform: translatez(-600px) scale(3); z-index: 2; } /* demo styles --------------------------------------------- */ body, html { overflow: hidden; } body { font: 100% / 1.5 arial; } * { margin: 0; padding: 0; } .parallax { font-size: 200%; } /* centre content in parallax layers */ .title { text-align: center; position: absolute; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } /* style groups --------------------------------------------- */ #group1 { z-index: 5; /* slide on group 2 */ } #group1 .parallax__layer--base { background: rgb(102, 204, 102); } #group2 { z-index: 3; /* slide under groups 1 , 3 */ } #group2 .parallax__layer--back { background: rgb(123, 210, 102); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parallax"> <div id="group1" class="parallax__group"> <div class="parallax__layer parallax__layer--base"> <div class="title">base layer lorem ipsum dolor sit amet, consectetur adipiscing elit. ut tellus risus, vestibulum non neque ut, consectetur fermentum neque. nam pharetra tellus pulvinar ante suscipit dapibus. quisque pharetra libero vel lectus placerat laoreet.</div> </div> </div> <div id="group2" class="parallax__group"> <div class="parallax__layer parallax__layer--base"> <div class="title">base layer</div> </div> <div class="parallax__layer parallax__layer--back"> <div class="title">background layer</div> </div> </div> </div>
with changes made code works. may not how intend work, it's working.
Comments
Post a Comment