Finite Scroll Example
How do I make an finite scroll example?
Buffered divs scroll -- use a small number of divs to represent a large collection.. What is a finite scroll example? How do you make a finite scroll example? This script and codes were developed by Jeff Daze on 18 January 2023, Wednesday.
Finite Scroll Example - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Finite Scroll Example</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="output"></div>
<div class="container" id="cellDiv" onscroll="divScrollPos();"> <div class="cellContainer" id="cellCont"> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Finite Scroll Example - Script Codes CSS Codes
#output{ height:30px; position:fixed; right:20px; } #cellCont{ height:80px; position:relative; } .container{ width:300px; height:200px; overflow:auto; border:1px solid #000; } .cell{ width:280px; height:80px; border:1px solid #F00; }
Finite Scroll Example - Script Codes JS Codes
//vars //this works for a very high number of elements; //but breaks down after about 5000 -- perhaps some better thumbnail tracking is required? // for reference: http://stackoverflow.com/questions/2481350/retrieve-scrollbar-position-with-javascript var totalCells = 510; var divPos; var cellHeight = 80; var cellOffset; var viewportDiv = $(".container"); var cellDiv = $(".cell"); /** * Cells are 80px high; calculate how many we need for the current display... */ function getCellCount(){ //add two cells to the viewable items... //set the cell offset for calculating movement... cellOffset = Math.ceil((getViewportHeight(viewportDiv) / cellHeight) + 2); //always round up! return cellOffset; } /** * make the containing div as tall as all the cells... */ function setTotalCells(count){ $("#cellCont").style.height = count * cellHeight+"px"; } /** * create enough cells to cover the viewport... */ function generateCells(count){ for(var i=0;i<count;i++){ //create the cells... var div = document.createElement("div"); div.id = "d"+i; div.style.width = "280px"; div.style.height = "80px"; div.style.background = "#EAEAEA"; div.style.border = "1px solid #F00" div.style.color = "#000"; //magic! div.style.position = "absolute"; div.style.top = i * (cellHeight+2) +"px"; div.innerHTML = i; $(".cellContainer").appendChild(div); } } setTotalCells(totalCells); var bufferCellCount = getCellCount(); generateCells(bufferCellCount);
//viewport height is also important for calculating the last screen of cells at the bottom...
function getViewportHeight(viewportElement){ return viewportElement.clientHeight;
}
//get the scroll position of some parts...
//this fires onscroll
function divScrollPos() { divPos = viewportDiv.scrollTop; $("#output").innerHTML = divPos; //if the divPos has changed by the height of a cell, move the one that just passed and put it on the other end //what direction are we going? var divDir = detectDirection(divPos); if(divDir == "down"){ for(var i =0;i<bufferCellCount;i++){ var testDiv = $("#d"+i); //if the div has moved off screen, move it to the bottom... if(testDiv.offsetTop + cellHeight < divPos && divPos + getViewportHeight(viewportDiv) < parseInt($("#cellCont").style.height, 10)){ console.log(parseInt(testDiv.style.top, 10) + cellHeight * (bufferCellCount)+"px"); testDiv.style.top = parseInt(testDiv.style.top, 10) + (cellHeight+2) * (bufferCellCount)+"px"; } } } if(divDir == "up"){ for(var i =0;i<bufferCellCount;i++){ var testDiv = $("#d"+i); //if the div has moved off screen, move it to the top... if(testDiv.offsetTop > divPos + getViewportHeight(viewportDiv) && divPos > 0){ console.log(parseInt(testDiv.style.top, 10) - cellHeight * (bufferCellCount)+"px"); testDiv.style.top = parseInt(testDiv.style.top, 10) - (cellHeight+2) * (bufferCellCount)+"px"; } } }
}
//detect the direction of scroll... var scrollDelta = 0; function detectDirection(newPos){ if (newPos > scrollDelta){ direction = "down"; }else{ direction = "up"; } scrollDelta = newPos; return direction; }
//small selector method...
function $(selector) { return document.querySelector(selector);
}
Developer | Jeff Daze |
Username | jeffdaze |
Uploaded | January 18, 2023 |
Rating | 3 |
Size | 3,082 Kb |
Views | 12,144 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Pulsing Button | 1,659 Kb |
JS test and Box Shadow | 1,906 Kb |
Angular Drag Example | 2,497 Kb |
Simulation of famous C64 code... | 1,788 Kb |
Winter Scene | 2,739 Kb |
A test card for a Munchkin-like game | 2,044 Kb |
Tilemap Edge CSS | 2,159 Kb |
Circular Drag and Drop | 2,451 Kb |
Starfield using KineticJS | 3,290 Kb |
Speed Reading | 6,553 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Kudos Please | TimPietrusky | 6,624 Kb |
A Pen by Malith Hettiarachchi | MalZiiirA | 5,531 Kb |
Testing Portfolio Page | Sideshowli | 3,395 Kb |
Plotting Points with D3.js | Laurakelly | 31,996 Kb |
About Mazano | Kiti | 2,585 Kb |
Animated Vertical CSS3 Menu Black | MaCeLMp4 | 2,750 Kb |
Fellowship of the Ring | Aussieyang | 2,639 Kb |
Filtering with shuffle.js | Deyand | 2,712 Kb |
Snow collision | Wojtek1150 | 3,542 Kb |
Freecodecamp - Tribute Page | Samoht513 | 3,583 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!