Moving balloons with jQuery
I really want to get into jQuery first of all.
I have a site im making, ove a balloon is clicked on it floats up and then goes to the targeted page. However they seem to bash into each other.
<开发者_如何学编程;script type="text/javascript">
$(document).ready(function(){
$("a.balloon-nav").click(function(event){
var target = $(this).attr("href");
var zIndex = $(this).css("z-index");
$(this).attr("href", "#").css("z-index", "100");
$(this).animate({ top: "-500px" }, 1000, function() {
$(this).css("z-index", zIndex);
window.location=target;
alert("foo");
});
});
});
</script>
I also would like to know the use of "event" and what "propagation" means.
Try using:
$(this).animate({ top: "-500px" }, 1000, '', function() { // ...
Definition:
animate( params, [duration], [easing], [callback] )
About event
, read here. Taken from there:
jQuery's event system normalizes the event object according to W3C standards
event.stopPropagation()
is usually used to avoid firing an event to parent elements.
For example, if you have
<p> UH
<p> Something </p>
</p>
and you do something like $('p').click(function() { //magic stuff } );
, if you happen to click the <p>
containing "Something", the magic stuff will fire twice!
So, if you do
//bind click event on all 'p's
$('p').click(
function(myEvent) {
//magic stuff
alert($(this).text());
myEvent.stopPropagation();
}
);
whenever you click a <p>
, it ensures it gets fired on that element only, and not on his parents.
UPDATE: it is advisable to always store jquery elements if used more than once. In your case, you use a lot of $(this).something()
, for either retrieving/setting values or calling functions.
Try to store it like:
var $this = $(this); //stored it in variable
var someData = $this.something();
$this.functionNeeded(...);
I also would like to know the use of "event" and what "propagation" means.
When you click on a text shown in an HTML page, the event (which could be interpreted as information about something happened in the page) is propagated (or sent) to the parent element of the element where the event happened, and then to the parent element until the windows object is reached. Each of those elements can react to the event, and have a handler that is executed. That is why the event handlers in jQuery have a target parameter, which says to the event handler which was the element that originated the event.
精彩评论