简体   繁体   English

用jQuery移动气球

[英]Moving balloons with jQuery

I really want to get into jQuery first of all. 我真的很想先进入jQuery。

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] ) animate(params,[duration],[easing],[callback])


About event , read here . 关于event请在这里阅读 Taken from there: 从那里取:

jQuery's event system normalizes the event object according to W3C standards jQuery的事件系统根据W3C标准标准化事件对象


event.stopPropagation() is usually used to avoid firing an event to parent elements. event.stopPropagation()通常用于避免将事件触发给父元素。

For example, if you have 例如,如果您有

<p> UH
   <p> Something </p>
</p>

and you do something like $('p').click(function() { //magic stuff } ); 然后您执行类似$('p').click(function() { //magic stuff } ); , if you happen to click the <p> containing "Something", the magic stuff will fire twice! ,如果您碰巧点击包含“ Something”的<p> ,则魔法物品将触发两次!

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. 只要您单击<p> ,它可以确保仅在该元素而不是其父元素上触发它。


UPDATE : it is advisable to always store jquery elements if used more than once. 更新 :如果多次使用,建议始终存储jquery元素。 In your case, you use a lot of $(this).something() , for either retrieving/setting values or calling functions. 在您的情况下,您将使用大量$(this).something()来获取/设置值或调用函数。
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. 当您单击HTML页面中显示的文本时,该事件(可以解释为与该页面中发生的事情有关的信息)将传播(或发送)到事件发生的元素的父元素,然后传播到父元素,直到到达Windows对象。 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. 这就是为什么jQuery中的事件处理程序具有目标参数的原因,该参数向事件处理程序说这是引发事件的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM