I Built JavaScript-Powered Minimap Scroll Animation

Codegrid
21 Apr 202411:09

Summary

TLDRIn this tutorial, the creator explores a unique mini map scroll animation, inspired by the Artificial Garage site. They guide viewers through building the feature using pure JavaScript, without plugins or external libraries. The project includes a dynamic light mode that adjusts based on scroll depth, enhancing user experience. The video covers setting up HTML, CSS for styling and responsive design, and JavaScript for interactive elements, providing a comprehensive walkthrough for developers looking to implement a visually appealing and functional mini map.

Takeaways

  • 🌟 The video introduces a creative web project focusing on a mini map scroll animation without using plugins or scroll triggers.
  • 🎨 The project integrates a dynamic light mode that changes the theme based on the user's scroll depth, enhancing user experience.
  • πŸ“ The HTML structure includes a navigation bar, a gallery section with a mini map and main images, and a footer with placeholder content.
  • πŸ–ΌοΈ The mini map features a preview container for thumbnail images and an active image indicator that highlights the current view.
  • πŸ”§ JavaScript is used to manipulate the position of elements based on user scroll actions, creating a synchronized scroll effect between the main content and the mini map.
  • 🎨 CSS is utilized to style the page, including responsive design elements through media queries to adjust image item sizes on smaller screens.
  • πŸ› οΈ The 'get element top' function is crucial for calculating the distance from the top of the page to any given element, aiding in scroll synchronization.
  • πŸ”„ The 'handle scroll' function updates the mini map's position in real-time as users scroll, ensuring a seamless visual representation of the user's location within the content.
  • πŸŒ“ The theme switching logic is based on scroll depth, with a toggle point set at four times the viewport height to dynamically change the page's appearance.
  • πŸ”— The 'handle scroll' and 'check scroll' functions are attached to the window's scroll event to ensure the page responds dynamically to user interactions.

Q & A

  • What was the main motivation behind the project described in the script?

    -The main motivation was to try something new and different from the usual sliders, landing pages, and menus, which led to the decision to recreate a mini map scroll animation using vanilla JavaScript without any plugins, libraries, or scroll triggers.

  • What is the inspiration for the mini map scroll animation?

    -The inspiration came from a site called 'artificial garage' by Dennis Nanberg, which featured an amazing mini map scroll animation that caught the creator's eye.

  • What are the key features of the mini map implementation in the project?

    -The mini map includes a preview container with thumbnail images and an active image indicator that highlights the part of the mini map currently in view. It also features a sticky position to remain in place as the user scrolls.

  • How is the layout of the HTML structure designed for this project?

    -The layout consists of a wrapper with a navigation bar, a gallery section split into a mini map and main images, and a generic container at the bottom filled with placeholder content.

  • What is the purpose of the 'item preview' divs within the mini map?

    -The 'item preview' divs are used to wrap each thumbnail image in the mini map, allowing for easy manipulation and duplication to fill the mini map for the demo.

  • How does the CSS styling support the light and dark mode functionality?

    -The CSS includes a class toggle on the wrapper that switches the background and text colors to accommodate both light and dark modes as the user scrolls.

  • What is the role of the 'get element top' function in the JavaScript implementation?

    -The 'get element top' function calculates the distance from the top of the page to any given element, which is vital for determining the exact position of images on the page and syncing the scroll position with the mini map preview.

  • How is the scrolling experience synchronized with the mini map preview?

    -As the user scrolls, the position of the mini map preview is updated proportionally to the scroll position within the images container, ensuring a seamless visual representation of where the user is in the images set.

  • What is the significance of the multiplier value 2.84 in the scroll animation?

    -The multiplier value 2.84 dictates the maximum translation of the preview container, determining how far the bottom of the preview should move by the end of the scroll range, which is crucial for fine-tuning the synchronization of the preview with the scrolling content.

  • How does the theme switching based on scroll depth enhance the user experience?

    -The theme switching based on scroll depth dynamically changes the page's appearance to either light or dark mode as the user scrolls, enhancing the user experience by adapting the page's theme to the scrolling depth.

  • How are the scroll event listeners attached to ensure dynamic responses to user interactions?

    -The 'handle scroll' and 'check scroll' functions are attached to the window's scroll event, ensuring that the page responds dynamically to user interactions as they navigate through the content.

Outlines

00:00

🌟 Introduction to Mini Map Scroll Animation

The speaker expresses a desire to explore a new and trendy web design element, the mini map. They were inspired by a mini map scroll animation on 'artificial garage' and decided to recreate it using vanilla JavaScript. The project aims to integrate a dynamic light mode that changes the theme based on scroll position, all without plugins or scroll triggers. The video encourages viewers to like and subscribe before diving into the tutorial. The tutorial starts with setting up the HTML structure, which includes a navigation bar, a gallery section divided into a mini map and main images, and a container for placeholder content. Each thumbnail in the mini map is wrapped in a div called 'item preview', and the main images are housed within a structure that includes an image and a text description.

05:02

🎨 Styling the Mini Map and Gallery

The speaker moves on to the styling part of the project, starting with resetting margins and paddings, setting box-sizing to border-box, and specifying the font family. The wrapper is set to full width and height with a starting black background and a transition property for color animation based on scroll events. Images are styled to occupy 100% of their container's width and height with an 'object-fit' property set to 'cover'. Links, paragraphs, and headers are given generic styling properties. The navigation bar is positioned at the top using 'position: fixed' and styled with 'Flexbox'. The gallery section is made to take the entire available space, with the mini map set to 'position: sticky' and covering 25% of the width and full viewport height. The mini map has a black background that changes color during scrolling, and the active image indicator is styled to stand out against varying backgrounds. The preview container is designed to scroll within the mini map, and the main images take up the remaining space with specified dimensions. A light mode class toggle is introduced for the wrapper to switch between light and dark themes as the user scrolls. Media queries are added for responsiveness on smaller screens.

10:03

πŸ’» Implementing JavaScript for Interactive Features

The tutorial concludes with the implementation of JavaScript to bring interactivity to the project. The speaker selects key elements such as the main container for images, the preview section, and the mini map itself. A function called 'get element top' is created to calculate the distance from the top of the page to any given element, which is crucial for syncing the scroll position with the mini map preview. The 'handle scroll' function updates the position of the mini map as the user scrolls, creating a seamless scrolling experience. A multiplier value is introduced to dictate the maximum translation of the preview container, which can be adjusted based on the project's specific requirements. The theme switching logic is implemented based on scroll depth, with a toggle point set at four times the viewport height to switch the page's theme dynamically. Finally, the 'handle scroll' and 'check scroll' functions are attached to the window's scroll event to ensure the page responds dynamically to user interactions.

Mindmap

Keywords

πŸ’‘Mini Map

A 'Mini Map' in the context of the video refers to a small, interactive map that provides a visual overview of a larger area or content. It is used to navigate through a website's content more intuitively. In the video, the creator aims to recreate a mini map scroll animation that was found on the 'artificial garage' site, showcasing how to implement such a feature using vanilla JavaScript without relying on plugins or additional libraries.

πŸ’‘Scroll Animation

A 'Scroll Animation' is a visual effect that occurs as a user scrolls through a webpage. It can include elements moving, changing size, or transforming in appearance. The video describes creating a mini map with a scroll animation that updates as the user navigates through different sections of the website, enhancing the user experience by providing a dynamic visual representation of the content being viewed.

πŸ’‘Vanilla JavaScript

Vanilla JavaScript refers to JavaScript code that is written without the use of external libraries or frameworks. It is the core JavaScript language without any additional features provided by libraries like jQuery or React. The video emphasizes the use of vanilla JavaScript to create the mini map and scroll animation, showcasing the capabilities of the language itself without relying on external tools.

πŸ’‘Dynamic Light Mode

The 'Dynamic Light Mode' mentioned in the video refers to a feature that changes the theme of a website based on user interaction, such as scrolling. As the user scrolls, the theme of the website transitions from a light mode to a dark mode, providing a responsive and adaptive user experience. This feature is integrated into the website using JavaScript, demonstrating how to manipulate styles based on scroll events.

πŸ’‘HTML Structure

The 'HTML Structure' is the arrangement of HTML elements that define the layout and content of a webpage. In the video, setting up the HTML structure involves creating a wrapper, a navigation bar, a gallery section, and a content container. This structure is essential for organizing the elements of the page and ensuring that the mini map and scroll animation function correctly.

πŸ’‘CSS Styling

CSS Styling is the use of Cascading Style Sheets to define the appearance of HTML elements. The video discusses various CSS properties used to style the navigation bar, gallery, and content container, including positioning, dimensions, colors, and transitions. Proper CSS styling is crucial for creating a visually appealing and functional user interface.

πŸ’‘JavaScript Manipulation

JavaScript Manipulation refers to the process of dynamically altering the content, structure, style, or behavior of HTML elements using JavaScript. In the video, the creator discusses how to manipulate elements like the mini map and preview section to respond to scroll events, creating a synchronized scrolling experience and theme switching.

πŸ’‘Sticky Positioning

Sticky Positioning is a CSS positioning property that keeps an element in a fixed position relative to the viewport, even when the user scrolls. In the video, the mini map is given a sticky position to ensure it remains visible and in place as the user scrolls through the gallery section, enhancing the navigational aspect of the mini map.

πŸ’‘Flexbox

Flexbox is a CSS layout mode that allows for the efficient arrangement of items within a container, even when their size is unknown or dynamic. The video mentions using Flexbox to space out the links in the navigation bar and to align the text within the image descriptions. Flexbox provides a flexible and responsive layout that adapts to different screen sizes and content changes.

πŸ’‘Media Queries

Media Queries are CSS techniques that apply different styles based on the device characteristics, such as screen size, resolution, or orientation. In the video, media queries are used to adjust the size of image items on smaller screens, ensuring that the layout remains responsive and visually appealing across various devices.

Highlights

Introduction to creating a mini map scroll animation with vanilla JavaScript.

Inspiration from the site 'artificial garage' by Dennis Nanberg for the mini map design.

Challenge to recreate the mini map without using plugins or scroll triggers.

Integration of a dynamic light mode that changes the theme based on scroll.

Setting up the HTML structure with a navigation bar, gallery section, and placeholder content.

Designing the gallery with a mini map and main images in a two-column layout.

Creating a preview container and active image indicator for the mini map.

Styling the navigation bar with fixed positioning and Flexbox for link spacing.

Using CSS to style links, paragraphs, and headers with generic properties.

Setting up the gallery with sticky positioning and a defined height for the mini map.

Styling the active image indicator with a solid white border and mix-blend-mode.

Creating a preview container with a defined height and Flexbox for thumbnail alignment.

Styling the images section with a specified width and height for the main content.

Implementing a class toggle for light and dark mode based on scroll depth.

Adding media queries for responsive design on smaller screens.

Selecting key elements with document.querySelector for JavaScript manipulation.

Creating a function to calculate the distance from the top of the page to any given element.

Syncing the scroll position with the mini map preview using JavaScript.

Defining a handle scroll function to update the mini map position during scrolling.

Adding logic to handle theme switching based on scroll depth.

Attaching event listeners for dynamic responses to user interactions.

Transcripts

play00:00

this time around I found myself growing

play00:02

a bit tired of the usual sliders landing

play00:04

pages and menus we typically create I

play00:06

wanted to try something new so we are

play00:08

diving into something a bit different

play00:10

and very trendy mini Maps recently I

play00:13

came across an amazing mini map scroll

play00:15

animation on a site called artificial

play00:17

garage by Dennis nanberg it really

play00:19

caught my eye and I thought it would be

play00:21

an interesting challenge to recreate

play00:22

using vanilla JavaScript no plugins no

play00:25

gap or scroll trigger we are also going

play00:27

to integrate that Dynamic light mode

play00:29

that changes the theme as you scroll all

play00:31

using JavaScript before we get started

play00:34

don't forget to hit that like button and

play00:35

subscribe if you haven't already all

play00:37

right let's get into it without wasting

play00:39

any more time let's kick things off by

play00:41

setting up our HTML structure we'll

play00:44

begin with a wrapper inside this wrapper

play00:46

the first thing we'll add is a

play00:47

navigation bar it will have a couple of

play00:53

links next up is the gallery section

play00:56

this section will be split into two main

play00:58

parts to create a clear layout on one

play01:00

side the mini map and on the other the

play01:02

main images it's like having two columns

play01:05

within the mini map we'll have a preview

play01:07

container holding all our thumbnail

play01:09

images and an active image indicator

play01:11

that will highlight the part of the mini

play01:13

map currently in view each thumbnail

play01:15

will be wrapped in a div called item

play01:16

preview since we need 10 images for this

play01:19

demo I'll set up one and then duplicate

play01:21

it nine more times to fill up the mini

play01:23

map

play01:38

switching over to the right column the

play01:40

images container this is where our main

play01:42

visual content lives each image will be

play01:44

housed within a item D split into two

play01:47

sections item image for the image itself

play01:50

and item copy for the text

play01:54

description we'll replicate this setup

play01:56

for each of 10 images

play02:07

[Music]

play02:17

[Music]

play02:23

lastly at the bottom of our page I will

play02:25

add a generic container filled with some

play02:27

placeholder content a few headers and

play02:30

image just to give it a bit more life

play02:32

and context to our phage

play02:41

[Music]

play02:47

layout that's it let's move on to the

play02:50

styling

play02:51

part first up we'll reset the margins

play02:54

and paddings for all elements and set

play02:56

their box sizing to border box we'll

play02:58

also specify the the font family for the

play03:01

body the wrapper is key here it's set to

play03:04

100% width and height with a starting

play03:06

background color of black we'll

play03:08

introduce a transition property as well

play03:10

because we will animate the background

play03:11

color based on scroll events images will

play03:13

occupy 100% of their parents width and

play03:16

height and I'll set their object fit to

play03:18

cover to maintain their aspect ratio now

play03:21

let's style our links paragraphs and

play03:23

headers with some generic properties

play03:25

including color font size font weight

play03:27

text transform Etc

play03:32

for the navigation I'm using position

play03:34

fixed to keep it at the top employing

play03:36

Flex box to space out the links and

play03:38

setting some

play03:41

[Music]

play03:45

padding the container holding the dummy

play03:47

website content will also take the

play03:49

entire available space with some

play03:53

padding moving on to the gallery this is

play03:56

where it gets interesting I'll set its

play03:58

position to relative s sticky positioned

play04:00

elements behave

play04:02

correctly the mini map particularly will

play04:05

be sticky positioned with its stop set

play04:07

to zero to keep it in place as you

play04:09

scroll through the gallery it will cover

play04:11

25% of the width and full viewport

play04:14

height we will add some top padding to

play04:16

push the preview images down and set the

play04:18

Overflow to Hidden which is crucial for

play04:20

our scrolling animation the mini map

play04:23

will maintain a black background with a

play04:25

transition property because we will

play04:26

change its color during the scroll our

play04:29

activ image indicator within the mini

play04:31

map will be absolutely positioned about

play04:33

300 pixels down centered horizontally

play04:35

with the left and transform properties

play04:38

it will have a specified width and

play04:39

height a solid white

play04:45

border its mix blend mode set to

play04:48

difference will make it stand out

play04:50

against varying

play04:52

backgrounds the preview container itself

play04:55

will also be absolutely positioned

play04:57

centered horizontally and designed to

play04:59

scroll within the mini map it will have

play05:01

100% width and a defined height based on

play05:04

our item previews we'll align these

play05:06

thumbnails in a vertical column using

play05:08

Flex boox currently the height of the

play05:11

preview container is set based on the

play05:13

number of the items and height of the

play05:15

child elements however you can easily

play05:17

verify or adjust this height by using

play05:19

your browser's inspect tool simply hover

play05:22

over the preview container to see the

play05:24

exact

play05:25

[Music]

play05:27

value each item preview will have have a

play05:30

relative position with fixed Dimensions

play05:32

similar to our active image indicator

play05:34

plus some padding for

play05:53

spacing the images section taking up the

play05:56

remaining 75% of the vid

play06:00

will have items with a specified width

play06:02

and

play06:14

height inside the item image will

play06:17

directly match the dimensions of the

play06:19

item

play06:21

itself with each item copy styled using

play06:23

Flex boox to neatly align the text

play06:39

to accommodate light mode I'll use a

play06:41

class toggle on the rapper when

play06:43

activated it will switch the background

play06:45

and text colors accordingly this ensures

play06:48

our page adopts to both light and dark

play06:50

modes as the user Scrolls and to ensure

play06:53

our design remains responsive I will add

play06:55

some media queries to adjust the image

play06:57

item sizes on smaller screens

play07:00

[Music]

play07:02

that wraps up our CSS let's move on to

play07:04

the exciting part bringing all this to

play07:06

life with JavaScript once the page is

play07:08

ready we start by selecting the key

play07:10

elements we will manipulate we grab the

play07:13

main container for the images the

play07:15

preview section inside our mini map and

play07:17

the mini map itself using document.

play07:19

query selector these elements are

play07:21

crucial because it will be directly

play07:23

adjusting their positions based on the

play07:25

users scroll actions now to handle

play07:28

positioning we we create a function

play07:30

called get element top this function

play07:32

calculates the distance from the top of

play07:35

the page to Any Given element it works

play07:37

by summing up all the distance from our

play07:39

element to its highest ancestor this is

play07:42

vital because it tells us exactly where

play07:44

our images start on the

play07:48

page using this function we determine

play07:51

the top position of our images container

play07:53

and calculate its End by adding the

play07:56

height of the container to its starting

play07:58

position knowing where our images start

play08:00

and end allows us to sync the scroll

play08:02

position with the mini map preview we

play08:05

also grab the viewport height and the

play08:07

height of our preview section these

play08:09

measurements are essential to understand

play08:11

how much space we have to work with as

play08:13

the user

play08:14

[Music]

play08:18

Scrolls now you might be wondering where

play08:20

the random multiplier of 2.84 for

play08:23

preview Max translate variable came from

play08:26

this value is crucial as it dictates the

play08:28

maximum translation of the preview

play08:30

container essentially it tells us how

play08:33

far the bottom of the preview should

play08:35

move by the end of our scroll range

play08:38

remember we defined a fixed height for

play08:40

the preview rapper in the

play08:42

CSS through the experimentation I found

play08:44

that 2.84 work perfectly based on that

play08:48

fixed height however this value may vary

play08:50

in your project depending on the number

play08:53

of items in your mini map and the

play08:55

dimensions of those items I encourage

play08:57

you to play around with this multi

play08:59

multiplier to see how it affects where

play09:01

the preview stops as you scroll

play09:03

adjusting this value will help you

play09:05

fine-tune the synchronization of the

play09:07

preview with the scrolling through your

play09:08

content now here is where the magic

play09:11

happens we Define a handle scroll

play09:13

function that updates the position of

play09:15

the mini map as you scroll through the

play09:17

images as you scroll down we calculate

play09:20

your scroll position relative to the

play09:22

height of the images container this

play09:24

calculation helps us determine how far

play09:26

the preview should move within the mini

play09:28

map we translate the preview vertically

play09:31

using CSS Translate Y value the

play09:33

translation is proportional to your

play09:35

scroll position within the images

play09:37

container ensuring that as you approach

play09:39

the end of the images the preview also

play09:41

reaches the end of the mini map this

play09:43

sync creates a seamless scrolling

play09:45

experience that visually represents

play09:47

where you are in the images set

play09:49

additionally we add a logic to handle

play09:52

cases when the user Scrolls beyond the

play09:54

images container if the user Scrolls

play09:56

above the start of the images the

play09:58

preview res at the

play10:03

Top If the user Scrolls beyond the end

play10:05

the preview remains at the maximum

play10:07

possible translation

play10:23

Point next we handle theme switching

play10:26

based on the scroll depth we set a

play10:28

toggle point at 4 times the viewport

play10:30

height basically 400 viewport

play10:41

height if the user Scrolls past this

play10:44

point we add a light them class to the

play10:46

rapper to switch its appearance this

play10:49

Dynamic change enhances the user

play10:51

experience by adopting the pages them to

play10:53

scrolling depth finally we attach handle

play10:56

scroll and check scroll functions to the

play10:58

windows scroll event

play10:59

this setup ensures that our page

play11:01

responds dynamically to user

play11:03

interactions as they navigate through

play11:05

the content hope you found the video

play11:06

helpful see you in the next one

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Web DevelopmentJavaScriptMini MapScroll AnimationTheme SwitchingCSS StylingResponsive DesignInteractive UITutorialCreative Coding