Gradually enlarging the size of the font as time progresses

$( "h1" ).hover(
    function(){
        for(var i=0;i<255;i++){

            setTimeout(function(){$("#fat").css("font-size", (i + 12) + "pt")},200);
        }
    }, function(){$("#fat").delay(200).css("font-size","12pt")}
    );

My goal is to gradually increase the font size of an element when the mouse hovers over it, but currently there seems to be a delay countdown before the font size abruptly jumps to its largest value without any smooth growth effect.

I came across a similar issue in another discussion (Here) and suspect it may be relevant, although I'm still unsure about how to address it.

Answer №1

To optimize your code, avoid using a for loop and instead implement the logic within an anonymous function. Utilize setInterval and clear the interval when hovering out or reaching a certain limit.

(function(){
   var timer = null;
   $("h1").hover(function(){
      timer = setInterval(function(){
         var element = $("fat");
         var size = parseInt(element.css("font-size"));
         if(size>=255){ 
            clearInterval(timer); 
            return;
         }
         element.css("font-size",(size+1)+"pt");
      },200);
   },function(){
      clearInterval(timer);
      $("#fat").delay(200).css("font-size","12pt");
   });
})();

Additionally, to fix the issues with the for loop, consider using a closure and adjusting the timeout amount dynamically in each iteration to ensure consistent increases at specific intervals.

Answer №2

Enhancing with jQuery

If you're looking for a different way to achieve the same effect, consider using jQuery to create a smooth animation on hover:

$('#zoomIt').hover(function(){
    $(this).animate({"font-size":"4em"}, 1000); //Increase font size on hover
}, function(){
    $(this).animate({"font-size":"2em"}, 1000); //Decrease font size on hover out
});

Check out the demo here

Utilizing CSS Transitions

For a more streamlined approach, utilizing CSS transitions can also achieve the desired result:

h1 {
    transition: font-size 3s ease-in-out;
    font-size: 2em;
}
h1:hover {
    font-size: 4em;
}

Answer №3

If you want to avoid using javascript, you can still achieve this effect by setting limits on how much an item can grow:

h1 {
    transition: all 10s;
    font-size: 10pt;
}

h1:hover {
    font-size: 100pt;
}

You can customize the timing and size limits according to your preferences.

(Keep in mind that animating font-size may not provide a smooth zoom effect; it could appear 'stepped'. Consider using transform: scale() or zoom() for a better result.)

Answer №4

HTML

<h1 id="Example">Some Text</h1>

JQuery

$('#Example').hover(function(){
  // alert('here');
  for(var i=0; i<10; i++) {
    setTimeout(function() {
      $('#Example').delay(2000).css({ zoom: i });
    }, 100);
  }
});

$('#Example').mouseout(function() {
  $('#Example').css({ zoom: 0 });
});

View Demo

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

Gain command over the underline style of text without relying on the border property

Ingredients: just a simple text input field. Question: Is there a way to customize the size, color, and position of the underline styling on an input tag? I noticed that the property text-decoration-color is supported in the moz browser engine, but I&apos ...

"Combining Date and Integer Values with jQuery: A Step-by-Step

Hello everyone, <script type="text/javascript"> function calculateDueDate(){ var invoiceDate = $(".invoiceDate").val(); var paymentTerms = $(".paymentTerms").html(); } </script> ...

Mastering the art of Promises and handling errors

I've been tasked with developing a WebApp using Angular, but I'm facing a challenge as the project involves Typescript and asynchronous programming, which are new to me. The prototype already exists, and it includes a handshake process that consi ...

What is the best way to position and align an angle with the axis of a moving 3D figure?

My latest project involves a rotating planet, specifically Saturn with its iconic rings. To capture the best view of this celestial marvel, I configured the camera with precision: var camera = new THREE.PerspectiveCamera(45, width / height, 0.05, 1000); ...

Enhancing file upload buttons in Firefox for Rails 4 using Bootstrap

Using Rails 4 and Bootstrap 3 I am working on a form that has multiple file upload fields. This is the code snippet for one of the file upload fields: <%= f.file_field :image, class: "form-control" %> While the design looks good in most ...

Tips for incorporating an HTML file using ng-include in double curly brace syntax {{ }}

Here is the code snippet I am working with: <div ng-repeat="pTabs in child.productTabs" ng-click="toggleProductTab($index)" ng-if="productTabIsActive(pTabs, $index)"> <div ng-repeat="specs in pTab.additionalSpecs"> <p>{{spec ...

"Enhance your website's search functionality with the power

I initially used Jquery ui auto-complete version 1.8.2, but later upgraded to 1.8.11 and encountered some issues. 1.8.2 1.8.11 $('#term').autocomplete({ minLength : 4, source : rootPath+'/search', se ...

Creating dual modes (night/day) for your AngularJS application

Currently, I am in the process of developing a project with Angularjs and facing an obstacle with animations. My goal is to implement a feature in my application that allows it to display in two different modes - night mode and day mode. I have come ac ...

Extracting Values from JSON Array using Form Input Text

After designing a form for my web application, I encountered a situation where some fields needed to be populated with values from a JSON array retrieved from the treatment table in the database. {"treatment":[ { "id" : 1, "name" : "Leg Training" }, ...

Ways to display encoded URL image using <img src>

I have a scenario where I am dealing with a URL of an image link location that does not have a file extension like .jpg, but when accessed it displays the corresponding image. How can I output this URL in PHP? For instance, take the example of the reCAPTC ...

The onChange function in Material UI Text Field is preventing users from inputting additional values

My application contains a custom TextField component. I am facing an issue where I cannot increase the value of the first TextField due to the presence of the onChange method. However, the second TextField does not have the onChange method, and I can succe ...

Hovering over the image causes it to flicker and it remains the same

I have an image column within a grid, where the images are displayed at 25px x 25px to fit nicely in each row. I've added a hover effect to the images so that when you hover over them, they shift left (which is working) and then expand for better vis ...

Bizarre symbols observed while extracting data from HTML tables produced by Javascript

I am in the process of extracting data from Specifically, my focus is on the "tournament-page-data-results" div within the source code. Upon inspecting the HTML source code, the data does show up, but it appears with a mix of real information and random c ...

The issue with VueEditor in Nuxt.js is that it's throwing an error

When working on my Nuxt.js project, I integrated the vue2-editor package for writing articles with HTML. Everything functions properly when I enter text and submit it, however, upon reloading the page, I encounter an error stating "document is not defined" ...

Button Type Comparison: Click Event vs Submit Event

Having trouble getting a simple submit button to trigger the click and submit event... html <input type="submit" id="test"> JQuery: $(document).ready(function(){ $('#test').onclick(function(){ alert('Hi') }) $ ...

Using CSS alone, incorporate two images within a section in HTML5

Is there a way to add two images inside a section using CSS to make them look like this: https://i.sstatic.net/74JSK.png However, the only result I can achieve with CSS looks like this: https://i.sstatic.net/TWrSR.png I could use divs in HTML and add th ...

Markers on the map are not receiving the necessary click event handlers

The block of code below is designed to place markers on a map. However, it seems that the add Listener event is not properly attached to each marker. var mapDiv = document.getElementById("google-map"); var infoWindow = new google.maps.InfoWindow({ ...

Ensure that the corner ribbon remains in place on the pricing table by making

Looking to enhance the functionality of a price table that features a corner ribbon? Currently, there is a hover effect on the price table that displaces the corner ribbon. Check out the code snippet below. The goal is to keep the corner ribbon sticky when ...

Do you need to finish the Subject when implementing the takeUntil approach to unsubscribing from Observables?

In order to prevent memory leaks in my Angular application, I make sure to unsubscribe from Observables using the following established pattern: unsubscribe = new Subject(); ngOnInit() { this.myService.getStuff() .pipe(takeUntil(this.unsubscr ...

Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format. let _columns = []; let commonStyle = {"width":"20 ...