Finite Scroll Example

Developer
Size
3,082 Kb
Views
12,144

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 Previews

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);
}
Finite Scroll Example - Script Codes
Finite Scroll Example - Script Codes
Home Page Home
Developer Jeff Daze
Username jeffdaze
Uploaded January 18, 2023
Rating 3
Size 3,082 Kb
Views 12,144
Do you need developer help for Finite Scroll Example?

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!

Jeff Daze (jeffdaze) Script Codes
Create amazing love letters with AI!

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!