javascript - Creating a jQuery vertical scroll loop -
i'm trying write jquery script continually scroll these <p>
tags within <div>
parent. got idea this site.
function shuffle(){ $('#wrapper > p').each(function(){ h = ($(this).offset().top + 130); if( h > 500 ){ console.log(h); $(this).css ('top', 0 ); return; } if( h == 270 ){ $(this).addclass('current' ); } if( h == 360 ){ $(this).removeclass('current' ); } $(this).animate({ top: h, easing: 'easein' }, 500, ''); if( h > 1350 ){ $(this).css ('top', 0 ); } }); window.settimeout(shuffle, 2500); } var = 0; $('#wrapper > p').each(function(){ starter = $(this).css('top', ((i * 90) ) + 'px' ); starterwhite = ($(this).offset().top + 130); if((i*90) == 270) $(this).addclass('current'); $(this).css('top', starter ); i++; }); window.settimeout(shuffle, 2000);
#wrapper { height: 500px; overflow: hidden; position: relative; } p { position: absolute; margin: 0; padding: 0; } .current { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> </div>
problem when try make scroll <p>
tags overlap, , highlighted tag doesn't change. idea how make work? here's jsfiddle of got far.
check this out:
// constants, can play var step = 90; var current_index = 3; // variables calculating var currenttop = step * current_index; var nextforcurrenttop = step * (current_index + 1); var $numbers = $('#wrapper > p'); // in case = 7 var count = $numbers.length; var secondtolasttop = step * (count - 1); $numbers.each(function(i) { var $this = $(this); $this.css('top', * step + 'px'); if (i === current_index) { $this.addclass('current'); } }); window.settimeout(shuffle, 2000); function shuffle() { $numbers.each(function() { $this = $(this); // calculating new position of element h = parseint($this.css('top')) + step; // in case = 540 if (h > secondtolasttop) { $this.css('top', 0); return; } // in case visually = 3rd element if (h === currenttop) { $this.addclass('current'); } // in case visually = 4th element if (h === nextforcurrenttop) { $this.removeclass('current'); } $this.animate({ top: h, easing: 'easein' }, 500, ''); }); window.settimeout(shuffle, 2500); }
#wrapper { height: 500px; overflow: hidden; position: relative; } p { position: absolute; margin: 0; padding: 0; } .current { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="wrapper"> <p>1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <p>7</p> </div>
i improved , commented code example, removed magic numbers. can play constants.
Comments
Post a Comment