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 |
A test card for a Munchkin-like game | 2,044 Kb |
Simple handlebars demo | 1,940 Kb |
Circular Drag and Drop | 2,451 Kb |
Transparent Gradient | 2,735 Kb |
Code snippets for a Puyo clone | 2,363 Kb |
Tilemap Edge CSS | 2,159 Kb |
Simulation of famous C64 code... | 1,788 Kb |
Desktop Notifications | 1,975 Kb |
Angular Drag Example | 2,497 Kb |
Quick torus demo three.js | 1,894 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 |
Starfield old school style | Bolloxim | 5,214 Kb |
Fullscreen Parallax | Bassta | 3,313 Kb |
A bit of elegance | Hackthevoid | 9,095 Kb |
SCSS Social Icons Flat | Mattsince87 | 3,482 Kb |
Donald Trump - The New Yorker | Agbales | 2,502 Kb |
Force Counter | Kenlauguico | 2,732 Kb |
HEXAGON | Aurumlux | 1,684 Kb |
Experiment | Toddmoy | 2,849 Kb |
Fluid Responsive Typography | Jonmilner | 4,205 Kb |
Algorithm practice | Abensur | 5,620 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!