Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see Model versions and lifecycle.
Stay organized with collections
Save and categorize content based on your preferences.
Walking through Javascript code block
You can query a model directly and test the results returned when using
different parameter values with the Cloud console, or by calling the
Vertex AI API directly.
This code creates a simple, automatic, and looping content slider or carousel.
What It Does
The script manages a set of "slides" inside a "wrapper" container. It creates the visual effect of sliding from one slide to the next by horizontally moving the entire wrapper. It automatically advances to the next slide every 3 seconds and loops back to the beginning after the last slide (and vice-versa).
How It Works, Step-by-Step
let currentSlide = 0;
This is a global variable that acts as the "state" for the slider. It keeps track of the index of the currently visible slide, starting with the first one (index 0).
function showSlide(index)
This is the core function that does the actual visual work.
It first finds the necessary HTML elements: the .slides-wrapper (the container that will be moved) and all the individual .slide elements.
The if conditions handle the looping logic. If the requested index is past the last slide, it resets currentSlide to 0. If it's before the first slide, it sets currentSlide to the index of the last slide.
The key line is slidesWrapper.style.transform = .... This line manipulates the CSS transform property of the wrapper.
translateX(-${currentSlide * 100}%) moves the wrapper horizontally.
If currentSlide is 0, it's translateX(0%), showing the first slide.
If currentSlide is 1, it's translateX(-100%), which shifts the entire wrapper to the left by 100% of its width, revealing the second slide.
If currentSlide is 2, it's translateX(-200%), shifting it left by 200%, revealing the third slide, and so on.
Note: This effect relies on a specific CSS setup where all slides are laid out horizontally (e.g., using display: flex) inside the wrapper, and the wrapper's parent has overflow: hidden to hide the slides that are not in the viewport.
function nextSlide() and function prevSlide()
These are simple control functions.
nextSlide() increments the currentSlide counter and then calls showSlide() to update the display.
prevSlide() decrements the currentSlide counter and then calls showSlide().
These would typically be attached to "Next" and "Previous" buttons in the HTML.
window.onload = function() { ... }
This function executes once the entire page has finished loading.
showSlide(currentSlide);: This initializes the slider, ensuring it's correctly positioned at the first slide when the page loads.
setInterval(nextSlide, 3000);: This is what makes the slider automatic. It calls the nextSlide function every 3000 milliseconds (3 seconds), causing the slider to advance on its own.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-19 UTC."],[],[],null,[]]