Solved

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

Posted on 2013-06-17
8
315 Views
Last Modified: 2013-06-19
Hi,

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?

Thanks!
0
Comment
Question by:peps03
8 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 375 total points
ID: 39254790
Working sample http://jsbin.com/obeyob/1/edit
code
<!DOCTYPE html>
<html>
<head>
  <style>
    #container
    {
      width:500px;
      border:thin;
      border-color:black;
      border-style:solid;
      
    }
#bar
    {
      width:500px;
      height:30px;
      background-color:red;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
 
<button id="go">&raquo; Start</button>
<div id="container"> 
    <div id="bar"></div>
</div>  
<script>
 

 
$("#go").click(function(){
  $("#bar").animate({
    width: "0%",
   
  }, 10000 );
});
</script>
</body>
</html>

Open in new window

0
 

Author Comment

by:peps03
ID: 39256229
Thanks!

i put the code in this function:

			$(function() {
				refreshContent(); // Call refreshContent immediately
				setInterval('refreshContent()',10000);
			});
			
			function refreshContent()
			{
				$('.bar').width("100%");
			        $(".bar").animate({width: "0",}, 10000 );	
				$.post('testemonial-content.php',{}, function(resp) {
					$('div.cycle.contentleft:hidden').html(resp.left);
					$('div.cycle.contentright:hidden').html(resp.right);
					$('div.cycle').fadeToggle(0);
				},'json');
				
			}

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?
0
 
LVL 53

Expert Comment

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

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

Cd&
0
 
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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.
0
 
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.
$('.bar').stop().width("100%");

Open in new window

0
 

Author Closing Comment

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

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

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SharePoint Online Adding A People Picker to Search 14 23
Css issue on scroll 1 18
Problem to page 4 26
CSS measurement 10 16
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now