Animating CSS based on the viewport's position

Currently, I am working on incorporating CSS Keyframe animations into my application that are triggered by different events. The animation involves a unicorn soaring up from the bottom of the screen, pausing in the middle momentarily, and then continuing upwards out of view.

I am facing an issue where the height of the page varies for different users due to varying content amounts, causing the animation timing to be relative to the overall page height rather than the user's position within the viewport.

This is what my code looks like at the moment:

<img src="../***" v-if="aCounter === 1" class="unicornUp" alt="Unicorn">

.unicornUp {
  position: absolute;
  width: 50%;
  height: 50%;
  right: 20%;
  opacity: 0;
  z-index: 99;
  animation-name: unicornMoveUp;
  animation-duration: 3s;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  animation-iteration-count: 1;
  animation-delay: 0.5s;
}
@keyframes unicornMoveUp {
  from { bottom: 0; opacity: 1;}
  20% {bottom: 20%; opacity: 1; transform: rotate(5deg);}
  40% {bottom: 50%; opacity: 1;}
  60% {bottom: 50%; opacity: 1; transform: rotate(-5deg);}
  80% {bottom: 60%; opacity: 1;}
  100% {bottom: 90%; opacity: 0;}
}

I have been considering using a sticky parent div to address this issue, but I am unsure if this is the correct approach and where to begin. Any guidance or assistance you can provide would be greatly appreciated!

Answer №1

To achieve a fixed positioning, use position: fixed instead of position: absolute. The former is relative to the viewport while the latter is relative to the entire document.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A layout featuring a grid of 3 rows and 2 columns, each containing 6

Is there a more effective method to construct this 3x2 "table" using DIVS? I'm considering using traditional tables code <table> since it's simpler, but modern practice leans towards Divs. So, what is the most polished approach to achiev ...

When executing class methods, Ember.js encounters errors stating "method does not exist."

I am facing a situation where I need to trigger a model reload when a user clicks a refresh button. In the past, I successfully implemented this with Ember-Model. However, since migrating to Ember-Data, I am encountering an error when attempting to execute ...

Issues with using hooks in a remote module in Webpack 5 module federation

I am attempting to create a dynamic system at runtime using Module Federation, a feature in webpack 5. Everything seems to be working well, but I encounter a multitude of 'invalid rule of hooks' errors when I add hooks to the 'producer' ...

When the form is submitted, the text input vanishes and then the page is refreshed using AJAX technology

Can anyone help me troubleshoot why my page is reloading even though I used AJAX, and how to prevent it from clearing my input text after clicking the submit button? I tried using the show method to resolve this issue but it didn't work. <form met ...

What is the best way to set conditions for document side script?

I'm struggling to disable the horizontal scroll when the viewport width is 480px or less. The script that controls the scroll behavior on my website looks like this: <script> $(function () { $("#wrapper").wrapInner("< ...

Unable to apply 3rd condition with ngClass into effect

I've been attempting to incorporate a third condition into my ngClass. Initially, I managed to get two classes working in my ngClass to change the row colors alternately. [ngClass]="{ totalrow:i%2 != 0, odd:i%2 == 0}" Now, I'm trying to introdu ...

Ways to determine the name of the calling function in an AJAX or XMLHttpRequest request?

I am currently exploring ways to programmatically identify the function name responsible for triggering an Ajax call in JavaScript or jQuery within an existing codebase. As I delve into instrumenting a large existing codebase, I am seeking to pinpoint the ...

Discovering MaterialUI breakpoints within CSS styling

Working on a create-react-app project, I have incorporated material-ui 1.x and customized the theme with unique breakpoints... import { createMuiTheme } from '@material-ui/core/styles'; let customTheme = createMuiTheme({ breakpoints:{ val ...

Challenges arising with the express search feature

Currently, I am in the process of developing an API for a to-do application. I have successfully implemented the four basic functions required, but I am facing some challenges with integrating a search function. Initially, the search function worked as exp ...

What is the process for adding information to datatables using jQuery?

I have been struggling to make a data table in jQuery function correctly. I am able to append data to the table, but the sorting, pagination, and search features are not working. Even though the data is visible in the table, it shows as 0 records. I am wor ...

Ways to efficiently convert HTML form data into JSON format

Currently, I am in the process of developing an ecommerce platform. As part of this project, I have created a billing form to gather essential information such as email addresses and physical addresses from users. The intention behind collecting this data ...

Is there a way to render an image onto a canvas using an input tag?

Looking to create an image preview using canvas? Upload the image with the input tag and watch it display on canvas. I've experimented with various methods such as using img tags, setting img src for canvas using img tags, and trying onclick function ...

Tips for exporting telegram information to a Google spreadsheet

Recently, I set up a basic telegram bot using the telegraf framework and wrote this code snippet to log essential data: bot.on('text', (ctx, next) => { console.log(`[text] ${ ctx.message.chat.id } ${ ctx.from.username } ${ ctx.message.chat.f ...

Applying Bootstrap 4 styles to the 'Select File' input in a Django form: A step-by-step guide

I have created a Django template that includes a form for uploading an image. Below is the structure of the template: {% extends 'header.html' %} {% block content %} <br> <div class="w-50 card mx-auto"> <div class="card-body ...

Why isn't Google Map showing up in my HTML <div> section?

I am currently working on a web page that consists of two containers. The mainmapdiv container covers the entire page, while the mainhomediv container is positioned on top of the mainmapdiv. My goal is to hide the mainhomediv container and show a Google Ma ...

Linking input to radio buttons in Vue.js

When creating an edit form page, I encountered an issue where the page was not loading properly. Before the page loaded, I ran the following code: beforeMount(){ if(this.$route.meta.mode === 'edit'){ this.initialize = '/api/arti ...

Guide to adding customized CSS and JavaScript to a specific CMS page on Magento

I'm trying to incorporate a lightbox for video playback on a specific page of my CMS. I've placed my CSS file in JS/MY THEME/JQUERY/PLUGIN/VENOBOX/CSS/myfile.css and the JS files in JS/MY THEME/jquery/plugins/venobox/js/myfile.js, but it doesn&ap ...

Error in whatsapp-web.js: Unable to access properties of an unidentified object (reading 'default')

As I followed the step-by-step guide on how to use whatsapp-web.js, I encountered an issue. Here is the link to the guide const { Client } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const client = new Cl ...

What could be the reason for my router not redirecting to the interactive map?

I am currently working on an interactive map where each country is clickable and should redirect the user to a specific page for that country. However, I am facing an issue with the Redirect function and can't seem to figure out what's causing th ...

Issue with custom font display malfunction

I'm having difficulty getting my custom @font-face to function properly. When I apply this class style to a <p>, it always defaults to Arial. Can anyone point out what might be going wrong here? <style> .stonefont @font-face { font-fa ...