Developing an animated feature that displays a dynamic count up to the current size of the browser window

I have a script that's able to determine the height and width of my browser window. However, I am facing a challenge in creating a way for these dimensions to count up from zero to their current values upon loading. The desired functionality would be similar to what is shown here: http://jsfiddle.net/YWn9t/, but with the target numbers being the aforementioned dimensions. Is there a more concise way to achieve this than what is presented in the jsfiddle?

HTML:

<div>
    <span></span>
    <span></span>
</div>

CSS:

body {text-align: center;}

div {border: 2px solid #000; display: inline-block; padding: 5px 20px; vertical-align: middle;}

span:first-of-type:after {content: "|"; margin: 0 10px;}

jQuery:

$(document).ready(function (e) {
    showViewportSize();
});

$(window).resize(function (e) {
    showViewportSize();
});

function showViewportSize() {
    var the_width = $(window).width();
    var the_height = $(window).height();
    $('span:first-of-type').text(the_width);
    $('span:last-of-type').text(the_height);
}

Answer №1

Check out this new JSFIDDLE.

Javascript Code

$('#begin').on('click', function(){

  var windowHeight = $(window).height();
  var windowWidth = $(window).width();
  $('.newdiv').html('Height: ' + 
                         windowHeight + 
                         ', Width: ' + 
                        windowWidth);

  var initialWidth = 0;
  var finalWidth = windowWidth;
  var initialHeight = 0;
  var finalHeight = windowHeight; 
  var timerA;
  var timerB;

timerOne();
timerTwo();

function timerOne()
{
   initialWidth = initialWidth + 1;
   timerA = setTimeout(function(){timerOne()}, 20);
   $('.showwidth').html(initialWidth);
   if (initialWidth == finalWidth)
      {
       clearTimeout(timerA);
       initialWidth = finalWidth;
       }
}

function timerTwo()
{
   initialHeight = initialHeight + 1;
   timerB = setTimeout(function(){timerTwo()}, 20);
   $('.showheight').html(initialHeight);
   if (initialHeight == finalHeight)
   {
       clearTimeout(timerB);
       initialHeight = finalHeight;
    }
}

 });

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

Tips for utilizing the AngularJS filter to group by two columns in Angular

In my array of objects, each item has three properties: 1. name 2. team 3. age http://jsfiddle.net/HpTDj/46/ I have successfully grouped the items by team, but I am unsure how to group them by both team and age in my current code. I want to display the ...

The art of spinning in CSS

Can anyone help me figure out how to make an animation in CSS stay at its last frame instead of reverting back to the beginning? The <i>animation-fill-mode: forwards;</i> property doesn't seem to be doing the trick. Is there a workaround ...

My images seem to lose control and go completely wild when I try to turn them into hyperlinks

Seeking advice on why my social icons are behaving strangely. This is a new issue for me and I suspect that some other CSS is causing the problem, but I can't seem to locate it. I want to ensure that the formatting of these images remains consistent ...

Ensuring that your content spans the entire viewport is crucial for optimizing

https://gyazo.com/1440b13007c6e011ec46218ceecabb6a Upon examining the screenshot, it is evident that there is a white border surrounding the main content of the page. Despite my attempts to adjust everything to 100% width, I have not been success ...

web font causing div not to resize to accurate height

Having some webfont service issues with jscrollpane. It seems like the height of jscrollpane is being calculated based on the default font size before switching to the webfont, causing the div to be slightly longer and cutting off text. I tried using auto ...

Experiencing a snag with implementing Firebase version 9 FCM in Next.js 12

I decided to incorporate push notifications into my Next.js (version 12) app, so I integrated Firebase Cloud Messaging. Here is the implementation: import { initializeApp, getApp, getApps } from "firebase/app" import { getMessaging, getToken } fr ...

Issues with npm @material-ui/core Button and TextField functionality causing errors and failures

I'm currently working on a React project and decided to incorporate the material-ui framework. After creating a component that includes a textfield and a button, I've encountered an issue where I can't interact with either of them as they s ...

Issues with the functionality of the shopping cart are causing a disruption

This is the coding for the online shopping cart: <!DOCTYPE html> <html lang="en-us"> <head> <meta charset="UTF-8" /> <title>Online Shopping Cart</title> <script src="jquery-3.1.1.min.js"></script> ...

When attempting to access an API hosted on Cloudflare from Heroku, a 403 error is returned

I am currently utilizing the Cowin API () to access available slots. My implementation is based on Node.js. Interestingly, it functions well on my local machine but encounters an error after deployment on Heroku. 2021-05-09T11:07:00.862504+00:00 app[web.1] ...

How can I apply a texture to a 3D rectangle in THREE.js?

I am attempting to create a 3D box in THREE.js that represents a box made up of 2x4 Legos, measuring 24 pieces wide by 48 pieces long and an unspecified number of pieces tall. I have created a texture displaying this pattern using random colors: https://i ...

What is the best way to add a right-hand side navigation bar without blocking the scrollbar from view?

Take a look at this webpage: I am designing a website that includes a header, a footer, and a central container with left and right navigation bars surrounding a content area. However, I want the scrollbar for the content to appear on the right side of th ...

Node.js: Error - Module 'html' not found

Currently, I am utilizing Node.js and attempting to exclusively serve HTML files without any Jade or EJS engines. Below is the code for my entry point (index.js): var express = require('express'); var bodyParser = require('body-parser&apo ...

stop an html page from opening a new window

When using my HTML-based application, there are instances when it needs to open another URL within an iframe. However, a problem arises when the third-party URL within the iframe also opens a new window with the same content and URL. How can I prevent th ...

Issues arise when the Angular controller fails to load

I'm experiencing an issue with my Angular controller where the code inside its constructor is not running. Here's a snippet of the relevant pieces: conversationcontrollers.js: var exampleApp = angular.module('exampleApp',[]); console ...

Adjust the color according to the chosen route in a Vue component

I am looking to dynamically change the CSS color based on the route or a prop for a route. For example, if I navigate to the home page, I want the header to be red. If I visit the about page, I want the header to be green. I have attempted this using rout ...

Adding a fresh data point to Highcharts

Can the highchart line graph be updated every minute without starting from scratch? I want it to add new data points to the existing line, similar to the example shown in this jsfiddle. $(function () { $.getJSON('https://www.highcharts.com/sample ...

Enhancing the shadow effects on ThreeJS using buffergeometry

I'm currently working on a project where I have loaded a .gltf model in ThreeJS with a keyframe animation that alters the shape of the object (it's a point level animation). Everything seems to be functioning correctly, except for the fact that t ...

angularJS controller does not seem to be functioning properly due to a certain

var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0! var yyyy = today.getFullYear(); var currentdate = yyyy + ',' + mm + ',' + dd; var appointmentDate = moment($scope.AppointmentsList[i].J).f ...

Error! React is unable to find the window object

I recently added the "react-speech" package to my application in order to incorporate text-to-speech functionality. However, upon importing the package, I encountered an error that has been challenging to resolve despite extensive research. Any assistance ...

Selecting menu items in React Material UI with various background colors using the RAL color selector component

Seeking a solution to create a component for selecting RAL colors using Material UI's TextField and MenuItem. Each item in the menu should have a background color reflecting the RAL color it represents. However, Material UI no longer supports inline s ...