How can I trigger the second animation for an object that is constantly moving within a specific range in the first animation?

I am looking to create a simulation of rising and falling sea levels. Currently, when I click the button, the level rises from its starting point to the top, but I am unsure how to achieve this effect in my code. Below is the code I have written so far, any help is appreciated!

Is there an alternative method in CSS to indicate the current sea level instead of directly setting the keyframes "rise" with a specific value?

<button id="btn" onclick="startFunction()">Click</button>
    <div class="sea" id="searise"></div>

    <style>
      .sea {
        position: absolute;
        height: 650px;
        left: 0;
        bottom: 0;
        right: 0;
        background: #a4c2ef;
        animation: wave 3s ease-in-out infinite;
        z-index: 0;
      }
  
      @keyframes wave {
         0% {
           top: 300px;
         }
         50% {
           top: 290px;
         }
         100% {
           top: 300px;
       }
     }
  
     @keyframes rise {
       0% {
         top: 290px;
         height: 650px;
       }
       100% {
         top: 0;
         height: 850px;
       }
     }

   </style>
   <script>
     function startFunction() {
       var searise = document.getElementById("searise");
       searise.style.animation = "rise 10s forwards";
     }
   </script>

Answer №1

To retrieve the current sea level using JavaScript, you can access the offsetTop property of the sea element. To incorporate this information into your CSS styling, you must define a custom CSS property or variable that can be dynamically utilized in your CSS code.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>;
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button id="btn" onclick="startFunction()">Click</button>
  <div class="sea" id="searise"></div>

  <style>
    .sea {
      position: absolute;
      height: 650px;
      left: 0;
      bottom: 0;
      right: 0;
      background: #a4c2ef;
      animation: wave 3s ease-in-out infinite;
      z-index: 0;
    }
    
    @keyframes wave {
      0% {
        top: 300px;
      }
      50% {
        top: 290px;
      }
      100% {
        top: 300px;
      }
    }
    
     @keyframes rise {
      0% {
        top: var(--dynamic-top); /* 👈 */
        height: 650px;
      }
      100% {
        top: 0;
        height: 850px;
      }
    }
   </style>
   <script>
    function startFunction() {
      var searise = document.getElementById("searise");
      searise.style.setProperty("--dynamic-top", `${searise.offsetTop}px`); // 👈
      searise.style.animation = "rise 10s forwards";
    }
  </script>
</body>

</php>

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

Utilize Android accelerometer data to bring objects to life with animation

Utilizing an Android app, I am streaming accelerometer data to a Python script on my PC. The data is then saved to a text file. My goal is to utilize Javascript and jQuery to animate a 3D CSS cuboid (representing the device) to replicate the movements capt ...

Guidelines for utilizing the php mail() function

It seems that there is an issue with this code when running it on a local host versus a live server. I am attempting to use this code for a mail function, which works on another site but not on this one. I know the code will produce an error on localhost ...

Unique rewritten text: "Displaying a single Fancybox popup during

My website has a fancybox popup that appears when the page loads. I want the popup to only appear once, so if a user navigates away from the page and then comes back, the popup should not show again. I've heard that I can use a cookie plugin like ht ...

What steps can be taken to create an electron menu for easily conducting a general search within the current window?

I am searching for a solution to create an electron menu that includes the label "Find..." and performs a general search within the current browser window. While I was successful in adding the option, I am struggling to figure out how to access the browser ...

Retrieve all items pertaining to a specific week in the calendar

I'm trying to obtain a list of week ranges for all data in my MongoDB. When a week range is clicked, only the records for that specific week range should be displayed. By clicking on the week range, the ID of the week (let's say 42, representing ...

Issue with accessing $index.$parent in function parameter within ng-repeat in AngularJS

Can anyone with more experience please explain to me why this particular code compiles successfully: <li class="btn dropdown top-stack breadcrumb-btn" ng-repeat="nodeName in selectedNodeNames"> <a class="dropdown-toggle btn-anchor"> ...

Disabling Immediate Row Checkbox in Angular Datatable Based on Column Data

Is there a way to prevent the checkbox in a row from being checked based on certain column data? In my datatable, I need to implement a condition where if firstname="Superman", then the checkbox for that particular row should be disabled to preve ...

Why are the $refs always empty or undefined within Vue components?

I am completely new to working with Vue and I've been attempting to utilize $refs to retrieve some elements within the DOM from a sibling component. My objective is very basic, as I just need to obtain their heights, but I haven't had much succes ...

transferring information from PHP to JavaScript through the use of JSON encoding

I am currently in the process of developing a Google maps page that utilizes latitude and longitude data coordinates to generate a heat map displaying the distribution of foxes within a specific area. At present, my application functions properly when the ...

What is the best way to send a variable from my controller to the HTML and then to a JavaScript function?

After my controller runs its course, it finally outputs the following: res.render('my-html', { title: `${title}`, myVar1, myVar2 }); Once this data is rendered in the HTML, I am able to directly display it. However, my goal is to then pass thes ...

Incorporating video responses into an HTML player using Node.js with the req.pipe(res) method

I have successfully implemented ytdl-core to play a video and it is working as expected. Below is the code snippet: app.get('/',function(req,res) { var fs = require('fs'); var ytdl = require('ytdl-core'); ...

Express Angular Node Template Render throwing an error: module 'html' not found

I am currently in the process of creating a web application using AngularJS with ui-router for routing via $stateProvider, ensuring that only the specified states are displayed in the ui-view. In my server.js file, I have set up an initial framework such ...

"Strategies for effectively utilizing the .find method to locate specific users within a database

I'm currently working on developing a User Authentication system, but I've hit a roadblock when it comes to verifying users. Specifically, I'm struggling with understanding how to iterate through all of my users in order to filter out their ...

Clicking on a list item in React's material design will illuminate the item, marking it

I have 2 panels with a list group on each panel, following material design guidelines. Concern: When clicking the first list-item on panel 1, it does not get selected and change to style = "success", or highlight the selected item. The same issue occurs ...

I am experiencing issues with md-no-focus-style not functioning correctly in my configuration

I have a button that triggers the opening of my navigation bar: https://i.sstatic.net/nMr0i.jpg When I click on it, a hitmarker appears: https://i.sstatic.net/OLQaE.jpg The issue is that even after clicking on a navigation item and transitioning to the ...

Auto-populate AngularJS form using PHP array via JSON and $http

I came across a fantastic autocomplete feature in this plunker that I need to replicate using a PHP array with $http. When I tried adding an alert to check my array, it worked perfectly. However, I'm stuck on how to implement the filter in AngularJS. ...

The text in the image caption has been drastically reduced along with the size of the image

Currently, I am dealing with the following code: <div style="min-width: 1000px;"> <p> Some text </p> <div class='div_img_left'> <img src="images/sth.png" alt="missing image" style="max-width ...

How can I incorporate JSON data retrieved from my backend server into the content of Tabs in a ReactJS application, utilizing either the map() or forEach() method

I need help assigning a key from an object to each tab using a loop in order to navigate to its content when clicked on. I attempted to use map() but it didn't work, so I'm looking for guidance as I am new to React. Below is the code. Any assist ...

Any ideas on how to extract binary data from PHP passthru using a JavaScript ajax request?

I am facing an issue with retrieving and playing an audio file from a server using JavaScript. The ajax call in my code doesn't seem to callback, and I'm not certain if I'm handling the audio correctly in JavaScript. Below is the PHP file t ...

AngularJS nested menu functionality not functioning properly

I am currently working on a nested menu item feature in AngularJS. I have a specific menu structure that I want to achieve: menu 1 -submenu1 -submenu2 menu 2 -submenu1 -submenu2 angular.module('myapp', ['ui.bootstrap']) .cont ...