javascript - Using Jquery to move a div based on .scroll depending on window.scrollY -
what trying accomplish have div
appear left side after user has scroll past number of pixels. when passes point moves right , disappears again. if user scrolls have div
reverse movement.
the issue running when div
moves right side extends view unlike when on left side. have tried changing left value right value nothing have tried works.
here code have far jquery:
window.addeventlistener('scroll', function() { if (window.scrolly <= 50) { $('#leftinfo').animate({left: '-150%'}); } else if (window.scrolly >= 50 && window.scrolly <= 200){ $('#leftinfo').animate({left: '25%'}, 'swing'); } else if (window.scrolly >= 200) { $('#leftinfo').animate({left: '100%'}, 'swing'); } });
i have link js bin. http://jsbin.com/hadabe/7/edit?css,js,output
first of all, jquery animations on same object queued , executed in first come first serve order. need stop them first calling .stop()
clear animation queue new animation execute right away.
secondly, prevent window extending scrollable area, need instruct html body hide elements overflow boundries, on x-axis.
window.addeventlistener('scroll', function() { if (window.scrolly <= 50) { console.log("phase i"); $('#leftinfo').stop().animate({ left: '-150%' }); } else if (window.scrolly >= 50 && window.scrolly <= 200) { console.log("phase ii"); $('#leftinfo').stop().animate({ left: '25%' }, 'swing'); } else if (window.scrolly >= 200) { console.log("phase iii"); $('#leftinfo').stop().animate({ left: '100%' }, 'swing'); } });
body { margin: 0; height: 75rem; width: auto; float: right; display: inline-block; overflow-x: hidden; } .frontintro { border: 1px solid black; background-color: #ffffff; padding: 1rem; display: inline-block; color: #000000; border-radius: 15px; } #leftinfo { position: absolute; top: 15rem; width: 65%; z-index: 100; left: -150%; } #leftinfo p { font-size: 2rem; text-align: center; } #rightinfo { float: right; position: relative; margin: 5rem; width: 35%; } #rightinfo p { font-size: 1.5rem; text-align: center; } #first { background: #c6bea6; background-size: cover; color: white; height: 52rem; margin: 0 auto; padding: 0; position: relative; z-index: 300; } #second { background: #9e9e9e; background-size: cover; color: white; height: 800px; margin: 0 auto; overflow: hidden; padding: 0; position: relative; z-index: 200; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main-content"> <div id="first"> <div class="frontintro" id="leftinfo"> <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. maecenas luctus fringilla elementum. integer malesuada justo id bibendum dignissim.</p> </div> <!-- end of .frontintro & leftinfo --> </div> <!-- end of #first --> <div id="second"> <div class="frontintro" id="rightinfo"> <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. maecenas luctus fringilla elementum. integer malesuada justo id bibendum dignissim. praesent pellentesque sagittis lacinia. pellentesque tincidunt diam @ turpis tincidunt egestas.</p> </div> <!-- end of .frontintro & .rightinfo --> </div> <!-- end of #second --> </div>
Comments
Post a Comment