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

Is it possible for a media query to target only 2 elements instead of all 3?

Currently, I am dealing with an older website for a client, and it appears that a media query is not correctly affecting three specific classes out of the total. These problematic classes are .free-ship, .wholesale, .chronicles The CSS styling for these c ...

Use Javascript to create commands and variables specific to the domain or site of a webpage

Currently, I have implemented the code below to fetch the latest tweet. However, I am looking for a way to customize the TWITTERUSERNAME based on the domain where the template is being used. This template is shared across multiple domains. <script type ...

Is having async as false really detrimental?

Splitting my inquiry into two sections. Within my website, I am dynamically generating some divs by utilizing ajax post requests to retrieve data from the database. Following is the structure of my setup. <html> <body> <script type=" ...

Erasing the content of a text field with the help of a final-form computation tool

I'm attempting to utilize the final-form calculator in order to reset a field whenever another field is modified. In my scenario, there are two fields. Whenever the first field changes, the second field gets reset as expected. However, an issue aris ...

WebWorker - Error in fetching data from server using Ajax call

I've been experimenting with making AJAX calls to an ajax.htm file using web workers. The goal is to have the data continuously updated at set intervals. Although I'm not seeing any errors and the GET request appears to be successful, the data i ...

Passing the title of a page as data to a component in Next.js

I am currently working on setting a custom title for each page within my next.js project. Here is an example of a simple Layout: const MainLayout = props => { return ( <Layout style={{ minHeight: "100vh" }}> <Head> < ...

What is the specific operation of this function?

Could someone clarify how this function operates? I have a grasp on the ternary operator, which checks if the flag is true and if not, then it uses the second value. However, I am unsure why flag is initially a Boolean and how it toggles between true and ...

Steps for removing a recently added list item with a child button included

I'm currently working on developing a multi-section user-generated list application similar to this example. The goal is to allow users to input text into a field and then, based on the button they click, determine which section of the list their new ...

accessing PHP array elements within JSON

How can I access an array of arrays returned from PHP in JSON format? This is the PHP array: $cities = array(); while($row = mysql_fetch_array($result)){ $cityRow= array('cityNo'=>$row['city_no'], 'cityName'=>$row[ ...

What is the best way to incorporate a button that can toggle the visibility of the sidebar on my post page?

Check out this post of mine I noticed a button on someone's page that could hide their sidebar and expand it again when clicked. How can I implement this feature? Is it a simple task? ...

Is there a more efficient method to execute this AJAX request?

$('#request_song').autocomplete({ serviceUrl: '<%= ajax_path("trackName") %>', minChars:1, width: 300, delimiter: /(,|;)\s*/, deferRequestBy: 0, //miliseconds params: { artists: 'Yes' }, onSelect: functi ...

Exploring AngularJS $compile and the concept of scoping within JavaScript windows

I've encountered a scoping issue with the use of this inside an angular-ui bootstrap modal. The code below functions perfectly outside of a modal, but encounters problems when run within one: var GlobalVariable = GlobalVariable || {}; (fun ...

Progressively modifying related elements over time intervals

I have a specific list of p elements in my HTML code that I need to modify sequentially with a time interval of 1 second. Here is the HTML: <p>Changing first sentence!</p> <p>Second sentence ready!</p> <p>Third one coming up ...

What is the most efficient method to import multiple MaterialUI components using a shared namespace without requiring the entire library to be imported

In order to prevent name mixing with other libraries, I want my MaterialUI components to be under the same namespace. For example, ensuring that <Box> references <MaterialUI.Box> and not <SomeOtherLibrary.Box>. Although it seems like a si ...

Unable to connect to a style sheet

I'm in the process of developing a web application and I'm facing an issue with linking a stylesheet to my app. Check out my code below: <html> <head> <title>Bubble</title> </head> <link rel=" ...

What is the best way to unselect the "all" selector if one of the inputs is no longer selected?

I am facing an issue with a search filter functionality. When all filters are selected and then deselected individually or together, the "all" button remains selected. I need help in ensuring that when any filter is deselected, the "all" button also gets d ...

Utilizing Jquery to automatically scroll to a specific div on page load by setting an

I am attempting to automatically scroll to the div specified in the URL. The URL may include one of 21 different IDs such as: url.com/test#lite1 url.com/test#lite2 url.com/test#lite3 This scrolling action should happen when the page loads (so that user ...

change visibility:hidden to visible in a css class using JavaScript

I've put together a list of Game of Thrones characters who might meet their demise (no spoilers included). However, I'm struggling with removing a CSS class as part of my task. Simply deleting the CSS is not the solution I am looking for. I' ...

I'm having trouble getting a response from the user.php file in my login system

I am trying to create a login system using ajax and pdo with a button as the submitter. The issue I am facing is that when I click the button to execute, nothing happens. There are no errors in the console. I can see in the network tab that it sends the us ...

VueJS does not support loading Jquery

When setting up my project with Bootstrap, I encountered an issue where even though I imported the jquery.min file before Bootstrap in my main.js file using require(), Bootstrap still threw an error saying that it requires jQuery. In my main.js file, I ha ...