Decrease div width from 100% to 0% in ten seconds

Posted on 2013-06-17
Last Modified: 2013-06-19

I need to make a time indicated decrease bar. The bar's width must decrease from 100% to 0% width in 10 seconds.

How could i do that?

Question by:peps03
LVL 52

Accepted Solution

Scott Fell,  EE MVE earned 375 total points
ID: 39254790
Working sample
<!DOCTYPE html>
<script src=""></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<button id="go">&raquo; Start</button>
<div id="container"> 
    <div id="bar"></div>

    width: "0%",
  }, 10000 );

Open in new window


Author Comment

ID: 39256229

i put the code in this function:

			$(function() {
				refreshContent(); // Call refreshContent immediately
			function refreshContent()
			        $(".bar").animate({width: "0",}, 10000 );	
				$.post('testemonial-content.php',{}, function(resp) {

Open in new window

and it sort of works as expected, for the first 2 rounds. after the content has switched twice, the bar doesn't run anymore.

Why does it stop running?
LVL 53

Expert Comment

ID: 39256597
In modern browsers you do it with the CSS:

.bar {
.barActive {
                      transition:width 10s ease-out;
Then all you need is an event to change the className from 'bar' to 'barActive'.

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39256827
I like that ease of use without js.  I have not fully embraced it yet because I enjoy making things overly complex for myself and get satisfaction from making it work.

Just add, "-webkit-transition:width 10s ease-out;" so it works with safari.  

If you are looking to do some type of timer that starts when an action happens like answering a test question, you will need to use javascript/jquery.
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39256839
>Why does it stop running?
Will you please edit the jsbin I created with just the important code and lets take a look.

Even better, please post the link to your sample page.
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 125 total points
ID: 39257545
The 10 second interval occurs just a bit earlier than the animation is complete.
In order to have it working you have to stop the animation using the stop method.

Open in new window


Author Closing Comment

ID: 39258645
@padas, thanks for the code, great!

@AlbertVanHalen, thanks!! This works!!
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39258731
Thanks for the points

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question