Solved

resize jquery animation

Posted on 2013-05-10
5
652 Views
Last Modified: 2013-05-12
Hi,

My code works but what do I do about responsive design?
When I resize the browser I want the jquery animation to resize as well .
This code is made for a static display on a desktop.


<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script>
	<script src="jquery.easing.1.3.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(document).ready(function() {
		
 		 window.img = ["images/bcave1.png", "images/bcave2.png"];
		 window.indexNum=0;
		 setTimeout("animation()",100);
			
			
		});
		
	
		
		function animation(){
			setInterval("sun_raft()",2000);
			setInterval("cloud1()",1000);
			setInterval("pic1()",100);
			setInterval("animatepic1()",100);
		}
		
		function animatepic1(){
			 $("#pic").attr("src", img[indexNum]);
     		 indexNum++;	
      		 if (indexNum >= 2) {indexNum = 0;} 
	   
	    }
		
		
		
		function pic1(){
			$("#pic").animate({left:"650px"},4000).animate({left:"-150px"}, 0);
		}
		
		function sun_raft(){
			$("#sun").animate({opacity:".1"},1000).animate({opacity:"1"},1000);
	
/*			setTimeout("sun_raft()",2000);*/
		}
		function cloud1(){
			/*$("#cloud1").animate({left:"+=850px"},10000).animate({left:"-150px"}, 0)*/
		  $("#cloud1").animate({left:"850px"},1000);

			/*setTimeout("cloud1()",10000);*/
		}
		
	</script>
	<style type="text/css">
	    *{ margin:0; padding:0; }
	    body { text-align: center; background: red; }
			#wrapper{ margin:0px auto; width:700px; }
		#content{ position:relative; width:700px; height:300px; top:30px; overflow:hidden; border:5px solid #f5f5f5; background: url('images/scene_bg.jpg'); }
			#sun{ position:absolute; top:10px; left:30px; }
			#pic{ position:absolute; top:80px; left:30px; }
			#cloud1{ position:absolute; top:60px; left: -150px; z-index:1; }
		#img {  max-width: 100%; }
	</style>
</head>


<body>
<div id="wrapper">
		<div id="content">
        
			<img id ="pic" src="images/bcave1.png" alt="eight ball" />
			<img id="sun" src="images/sun.gif"/>
			<img id="cloud1" src="images/cloud1.png">
		
		</div>
	</div>

Open in new window

0
Comment
Question by:jagguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:jagguy
ID: 39157302
i think by the lack of responsive and my searching for this, jquery for animation for responsive designs is a lemon
0
 
LVL 42

Expert Comment

by:David S.
ID: 39157489
I would suggest that you try using the CSS3 "transform" property to scale it: https://developer.mozilla.org/en-US/docs/Web/CSS/transform#scale
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39157595
What you will want to do is set the width of the div using your responsive class that will change by the detected screen width.  

Perhaps at over 1000 px window #content is 700px wide but below a 700 px window it goes to to 500px and so on.   Therefor, step one is getting your containing div for the animation to use responsive code which it is not doing now.

Now you need to dynamically set your widths in your js so this type of code is not static
function pic1(){
			$("#pic").animate({left:"650px"},4000).animate({left:"-150px"}, 0);
		}

Open in new window

I would suggest making the "650px" a percent of the containing div.  Actually do that for all the numbers.  

This is a working example I quickly made to detect the change of a div  http://jsbin.com/utalub/1/edit
    .test{width:80%;height:100px;background-color:green;margin-top:100px;}

.animation{margin-top:30px;font-size:38px;}

Open in new window

var x = '700px';
$(window).resize(function(){
 var test_width=$(this).width();
 $('span#green').text(test_width);
  var floor = Math.floor;
  var animation_width=floor(test_width * 0.7);
  $('div.animation').text(animation_width);
 
  
});
  

Open in new window

  <div class="test">Hi EE. I am <span id="green"></span> px wide</div>
  
  <div class="animation"></div>

Open in new window

0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39158250
For modern browsers, just use the vh and vw units to set the dimensions and sizes and everything should scale to the viewport size.

Cd&
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39158315
Good one Cd&.  I have not fully adopted all the cool css3/html 5 tricks on a regular basis yet so they are out of mind.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

636 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