2011-11-16 53 views
2

所以我偶然发现了编写网站的这个小部分的方法,但它有点小错误。我知道jquery足够危险,但我希望有人能帮助我做到更好,更高效。关于mouseenter的jQuery动画问题

这是我觉得可以改善这一点,但我不知道如何完成这些:

  1. 滚过的第一个城市后,移动到下一个时, 不应该带回所有其他标记,然后重新隐藏它们。它 应该只是隐藏你所在的标记,然后显示当前已经结束的那个标记,但是鼠标移出并且延迟它将所有标记带回去。
  2. 目前,如果你从一个侧翻转到有点 快速的步伐在未来,动画似乎假发出
  3. 这是奖金,但我原本可视化的标记会 动画在一个递增的顺序,而不是所有在同一时间。 可能是随机的或者它可能被定义。我认为这将使 动画看起来更加流畅。我认为这将需要循环 ,但正如我前面提到的,我只知道危险。

这里是链接到页面:http://204.12.117.109/~sandbox/map/

下面是我使用jQuery代码:

$(function() { 
     $('.city').mouseenter(function(){ 

      var relid = $(this).attr('rel'); 

      $("#communities div[id!=" + relid + "].pin").animate({ 
       opacity: 0, 
       top: '-=40', 
       }, 500); 
     }); 
     $('.city').mouseleave(function(){ 

      var relid = $(this).attr('rel'); 

      $("#communities div[id!=" + relid + "].pin").animate({ 
       opacity: 1, 
       top: '+=40', 
       }, 500, 'easeOutBounce'); 
     }); 
    }); 

这里的我使用的CSS:

#communities { background: url(images/bgCommunities.gif) scroll bottom right no-repeat; position: relative; } 
    #communities .lists { width: 340px !important; } 
    #cities li { width: 160px !important; margin: 0px 2px !important; padding: 4px 0 4px 4px; } 
    #cities li:hover { background-color: rgb(240,240,240); } 
     #boyle, #long, #santa, #rich, #fres, #sac, #city, #sLA, #coach, #kern, #oak, #merc, #sal, #del { position: absolute; width: 8px; height: 12px; background: url(images/pin.png) 0 0 no-repeat; } 
     #boyle { top: 230px; right: 95px; } 
     #long { top: 241px; right: 92px; } 
     #santa { top: 236px; right: 84px; } 
     #rich { top: 139px; right: 165px; } 
     #fres { top: 173px; right: 121px; } 
     #sac { top: 133px; right: 147px; } 
     #city { top: 258px; right: 78px; } 
     #sLA { top: 235px; right: 90px; } 
     #coach { top: 239px; right: 62px; } 
     #kern { top: 206px; right: 105px; } 
     #oak { top: 148px; right: 162px; } 
     #merc { top: 161px; right: 136px; } 
     #sal { top: 171px; right: 155px; } 
     #del { top: 71px; right: 185px; } 

以下是我正在使用的HTML:

<div id="communities"> 
<div class="lists"> 
    <ul id="cities"> 
     <li class="city" rel="boyle"><a href="#">Boyle Heights</a></li> 
     <li class="city" rel="long"><a href="#">Long Beach</a></li> 
     <li class="city" rel="santa"><a href="#">Central Santa Ana</a></li> 
     <li class="city" rel="rich"><a href="#">Richmond</a></li> 
     <li class="city" rel="fres"><a href="#">Central/SE/SW Fresno</a></li> 
     <li class="city" rel="sac"><a href="#">Sacramento</a></li> 
     <li class="city" rel="city"><a href="#">City Heights</a></li> 
     <li class="city" rel="sLA"><a href="#">South Los Angeles</a></li> 
     <li class="city" rel="coach"><a href="#" >Coachella Valley</a></li> 
     <li class="city" rel="kern"><a href="#">South Kern</a></li> 
     <li class="city" rel="oak"><a href="#">East Oakland</a></li> 
     <li class="city" rel="merc"><a href="#">SW Merced/East Merced</a></li> 
     <li class="city" rel="sal"><a href="#">East Salinas (Alisal)</a></li> 
     <li class="city" rel="del"><a href="#">Del Norte County</a></li> 
    </ul> 
</div> 
<div class="pin" id="boyle"></div> 
<div class="pin" id="long"></div> 
<div class="pin" id="santa"></div> 
<div class="pin" id="rich"></div> 
<div class="pin" id="fres"></div> 
<div class="pin" id="sac"></div> 
<div class="pin" id="city"></div> 
<div class="pin" id="sLA"></div> 
<div class="pin" id="coach"></div> 
<div class="pin" id="kern"></div> 
<div class="pin" id="oak"></div> 
<div class="pin" id="merc"></div> 
<div class="pin" id="sal"></div> 
<div class="pin" id="del"></div> 

+1

请重命名你的标题“我该如何解决我的越野车代码?'所以他人更容易理解你的问题。你的头衔现在太笼统了。也可以尝试让你的CSS和HTML更简单,以便更容易浏览。 – ksindi

+1

嘿Tuva,我很抱歉,我不知道如何在不破坏功能的情况下让CSS和HTML更简单。我不知道你是否看到我的链接,但如果你没有看到代码在行动将清除问题:[链接](http://204.12.117.109/~sandbox/map/) – Johnny5

回答

0

这个例子应该为你解决问题。

http://jsfiddle.net/wJnr5/3

你需要做的是绑定一个事件来保存城市的容器。当鼠标离开这个容器时,你想要重新显示所有的城市。我还使用了一个更好的jQuery选择器来获取除当前城市以外的所有内容。

$('.city').bind('mouseenter', 
    function() { 
     //fade out everyone but me 
     $('.city').not($(this)).each(function() {        
      $(this).stop().delay(Math.floor(Math.random()*1000)).animate({ opacity: 0 }); 
     }); 
     //fade me in 
     $(this).stop().animate({ opacity: 1 }); 
    } 
); 

$('#container').bind('mouseleave', function() { 
    $('.city').stop().animate({ opacity: 1 });  
}); 
+0

即使现在好多了。根据规范3,城市随机消失。 – mrtsherman

+0

感谢您的回复。我将确保将其存档以备将来使用,但不幸的是,这不是我制作动画的文字。文本链接是绝对定位在地图上的定位标记图标。在绑定元素之外定位多个元素时,绑定解决方案可以工作吗?看到我的链接的功能:[链接] http://204.12.117.109/~sandbox/map/ – Johnny5

+0

代码将最终以完全相同的方式工作。只需将选择器表达式更改为定位您的标记而不是您的城市。例如,将'.city'改为'.marker'。您将无法使用$(this),但只需像您在旧代码中那样选择您想要的那个。 – mrtsherman

1

明白了(02:31est)! (现在工作的随机版...我喜欢挑战!):

EDIT(见http://zequinha-bsb.int-domains.com/map/cities.html

明白了2 @上午3时29分EST(我被按小时计酬...或在所有?) 随机版本是在它的各自链接的底部。

<script type="text/javascript"> 

    var firstEntry = true; 
    var lastOn = ''; 

    function showAllPins() { 
     if ($('#communities').hasClass('theMouseIsOff')) { 
      $("#communities div[id!=''].pin").animate({ 
       opacity: 1, 
       top: '+=40', 
      }, 500, 'easeOutBounce'); 
      firstEntry = true; 
      $('#communities').removeClass('theMouseIsOff'); 
     } 
    } 
    function showPin(relid){ 
     lastOn = relid; 
     if ($('#communities').hasClass('theMouseIsOff')) $('#communities').removeClass('theMouseIsOff'); 
     if (firstEntry == true) { 
      $("#communities div[id!=" + relid + "].pin").animate({ 
       opacity: 0, 
       top: '-=40', 
      }, 500); 
      firstEntry = false; 
     } else { 
      $("#communities div[id=" + relid + "].pin").animate({ 
       opacity: 1, 
       top: '+=40', 
      }, 500, 'easeOutBounce'); 
     } 
    } 
    function removeLastPin() { 
     $('#communities').addClass('theMouseIsOff'); 
     $("#communities div[id=" + lastOn + "].pin").animate({ 
      opacity: 0, 
      top: '-=40', 
     }, 500); 
     setTimeout('showAllPins()',5000); 
    } 

    $(document).ready(function() { 
     $('.city').mouseenter(function() { 
      relid = $(this).attr('rel'); 
      showPin(relid); 
     }).mouseleave(function() { removeLastPin() }); 
    }); 

</script> 

随机版本:

<script type="text/javascript"> 

    var firstEntry = true; 
    var lastOn = ''; 

    function showAllPins() { 
     if ($('#communities').hasClass('theMouseIsOff')) { 
      var citiesArr = []; 
      $('.pin').each(function() { 
       citiesArr.push(this.id); 
       $('#'+this.id).hide(); 
      }); 
      var stillHidden = citiesArr.length; 
      while (stillHidden > 0) { 
       var a = Math.floor(Math.random()*citiesArr.length); 
       if ($('#'+citiesArr[a]).is(':hidden')) { 
        $('#'+citiesArr[a]).show().delay(Math.floor(Math.random()*900)).animate({ 
         opacity: 1, 
         top: '+=40', 
        }, Math.floor(Math.random()*900), 'easeOutBounce'); 
        stillHidden--; 
       } 
      } 
      firstEntry = true; 
      $('#communities').removeClass('theMouseIsOff'); 
     } 
    } 
    function showPin(relid){ 
     lastOn = relid; 
     if ($('#communities').hasClass('theMouseIsOff')) $('#communities').removeClass('theMouseIsOff'); 
     if (firstEntry == true) { 
      $("#communities div[id!=" + relid + "].pin").animate({ 
       opacity: 0, 
       top: '-=40', 
      }, 500); 
      firstEntry = false; 
     } else { 
      $("#communities div[id=" + relid + "].pin").animate({ 
       opacity: 1, 
       top: '+=40', 
      }, 500, 'easeOutBounce'); 
     } 
    } 
    function removeLastPin() { 
     $('#communities').addClass('theMouseIsOff'); 
     $("#communities div[id=" + lastOn + "].pin").animate({ 
      opacity: 0, 
      top: '-=40', 
     }, 500); 
     setTimeout('showAllPins()',2000); 
    } 

    $(document).ready(function() { 
     $('.city').mouseenter(function() { 
      relid = $(this).attr('rel'); 
      showPin(relid); 
     }).mouseleave(function() { removeLastPin() }); 
    }); 

</script> 

EDIT(在removeLastPin删除RELID作为参数)

您可以点击此处查看:http://zequinha-bsb.int-domains.com/map/randomCities.html

+0

感谢您的答复zeq,但它看起来像这只适用于第一次翻车。之后,它只是不断推动页面上的标记。 – Johnny5

+0

我试图复制您的网站,以便我可以制作最终版本。我无法下载精灵,因为../ images/...不能从沙箱内访问。我确实发现了一个错误:显示的“”全部应该是id =='';不是id ==“”(单引号而不是双引号)。 –

+0

这里我已经将你的代码加载到jsfiddle中:http://jsfiddle.net/luniablue/e4LWU/15/ – Johnny5