animating with jquery

notarypublic

New Member
Working on my personal website - expect a post on it soon!

However, in the meantime I've been trying to teach myself jquery. For this challenge, I want to make the blue div move up or down a certain distance based on which list item is clicked on. I don't have anywhere to host this at the moment, so here is the code in its entirety:

HTML:
<!DOCTYPE html>
<html>
<head>
  <style>
.block {
  position:absolute;
  background-color:#abc;
  left:50px;
  width:90px;
  height:90px;
  margin:5px;
}

.sandbox {
	width: 800px;
	height: 800px;
	background-color: #ccc;
}
</style>
  <script src="http://code.jquery.com/jquery-git.js"></script>
</head>
<body>

	<ul>
        <li id = "writing">writing</li>
        <li id = "math">math</li>
        <li id = "science">science</li>
        <li id = "english">english</li>
        <li id = "you">you</li>
    </ul>
    
<div class = "sandbox">
<div class="block"></div>

<script>

/*
 There is a current distance, a new distance to move to, and the distance the user has chosen.
*/
var currentDistance;
var newDistance;
var inputDistance;

$("#writing").click(function(){
  inputDistance = 1;
});

$("#math").click(function(){
	inputDistance = 2;
});

$("#science").click(function(){
	inputDistance = 3;
});

$("#english").click(function(){
	inputDistance = 4;
});

$("#you").click(function(){
	inputDistance = 5;
});

newDistance = inputDistance - currentDistance;

$(".block").animate({marginTop: "+=newDistance"}, "slow");
currentDistance = newDistance;
</script>

</body>
</html>

I had this working previously, when the values were hard-coded in.. But now, I want it to work now if I pass a variable to the function - am I going about this the wrong way?
 

DHDdirect

New Member
Where does currentDistance initially get it's value from?

I haven't tested it but I'd say it would be null and that on your first click you would be subtracting inputDistance with the null value.

I could be way off, I'm new to jquery as well. Let me know.
 

leroy30

New Member
Can jquery evaluate strings as javascript? Specifically "+=newDistance".

Try it without the quotes... not 100% that's it without testing it as I've never done that specifically with jquery but give it a shot anyway and see how you go.
 

notarypublic

New Member
After some tinkering, I got it to work. I think it's hard for me dealing with web pages being processed in such a linear way, coming from a programming background. I rewrote it to use onClick() to call a function, and just used jQuery for animate. I'd like to get more comfortable with jQuery's functionality, but for now if it's simpler using basic js, I'll just do that.

HTML:
<!DOCTYPE html>
<html>
<head>
  <style>
  
.sentence {
	float: left;
}

.block {
	float: left;
	position:absolute;
	list-style-type: none;
	margin: 0;
}

.sandbox {
	width: 800px;
	height: 800px;
	background-color: #ccc;
}

.spacer {
	width: 100%;
	height: 400px;
	
}
  </style>
<script src="http://code.jquery.com/jquery-git.js"></script>
  <script>
  /*initial values */
var currentDistance = 0;
var newDistance = 0;
var inputDistance = 0;
</script>
</head>
<body>
<div class = "sandbox">
<div class = "spacer"></div>
<div class = "sentence">I like </div> 
<ul class = "block">
        <li id = "writing" onclick="slide_nav(0)">writing</li>
        <li id = "math" onclick="slide_nav(1)">math</li>
        <li id = "science" onclick="slide_nav(2)">science</li>
        <li id = "english" onclick="slide_nav(3)">english</li>
        <li id = "you" onclick="slide_nav(4)">you</li>
    </ul>

</div>

<script>

/*
 There is a current distance, a new distance to move to, and the distance the user has chosen.
*/
function slide_nav(clickedValue){
	newDistance = 20*(clickedValue - currentDistance);
	$(".block").animate({marginTop: '-=' +newDistance}, "slow");
	currentDistance = clickedValue;
}

</script>

</body>
</html>

edit: I'm using this as the future navigation bar on my portfolio site. I feel like there's a more elegant way to do this (Have it scroll based on which child was selected, instead of passing values and using a formula?), but the biggest concern is two-fold:

1, I want the animation to move at a constant pace, regardless of which link was clicked.
2, I want it to move the text up in terms of em's instead of pixels.

Small things, but the details make it what it is. Any ideas?
 
Last edited:

leroy30

New Member
You need to get some web hosting so we can see it!

I think you should stick at it RE JQuery. You will find all the browser compatability has been done for you and it's a very tidy framework for coding in.

What programming language do you come from?

btw did you ever get my PM response to your question the other week? My replys never went to the sent folder so I'm not sure if they actually sent ok!
 

notarypublic

New Member
You need to get some web hosting so we can see it!

I think you should stick at it RE JQuery. You will find all the browser compatability has been done for you and it's a very tidy framework for coding in.

What programming language do you come from?

btw did you ever get my PM response to your question the other week? My replys never went to the sent folder so I'm not sure if they actually sent ok!

I know.. I'm working on hosting right now. In the meantime though, if you copy and paste all of this into one empty html file, it should work as a standalone file.

I have fairly extensive knowledge with vbscript in Excel (horrible memories) and decent knowledge with java.. I also used to know qbasic and C++, but I'm happy to put most of that behind me :)
 

leroy30

New Member
woohoo qbasic that takes me back! Started qbasic when I was 12 with a game I called "Space blobs"! Good times lol.

My mainstream language these days is VB.net and C#.net both desktop and web. I prefer VB.net as I find it cleaner to look at but some clients require me to write C# so C# it is!

I never understood c++ more than printing "Hello world" on the screen lol.
 
Top