首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将鼠标悬停在alink上并为div设置动画

将鼠标悬停在alink上并为div设置动画
EN

Stack Overflow用户
提问于 2011-05-13 21:55:38
回答 2查看 270关注 0票数 0

嗨,我在尝试得到我需要的东西时停了下来。

基于:

代码语言:javascript
复制
$(".test a").hover(function(){
        $(this).toggle({ height: "200px" });
    }, function() {
        var currentTab = $(this).attr('href');
        $(this).stop(true, false).animate({ height: "100px" });
    });
});

<div class="test">
  <a href="#one">1</a>
  <a href="#two">2</a>
  <a href="#three">3</a>
</div>

我需要它来动画一个div而不是自己的,我想出了下面的,它动画它下来,但不备份,等等:

代码语言:javascript
复制
$(".test a").hover(function(){
        var activeTab = $(this).attr("href"); 

        $(activeTab).toggle({ height: "200px" });   }, function() {
        $(activeTab).stop(true, false).animate({ height: "100px" });    }); });

<div class="test">
<a href="#one">1</a>
<a href="#two">2</a>
<a href="#three">3</a>

<div id="one" class="tab one">1</div>
<div id="two" class="tab two">2</div>
<div id="three" class="tab three">3</div> </div>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-13 21:59:36

您需要重新声明变量activeTab,因为它只存在于mouseenter范围内,而不存在于mouseout范围内

代码语言:javascript
复制
$(".test a").hover(function(){      
          var activeTab = $(this).attr("href"); 
          $(activeTab).toggle({ height: "200px" });   
     }, 
     function() {
          var activeTab = $(this).attr("href"); 
          $(activeTab).stop(true, false).animate({ height: "100px" });   
     }); 
});
票数 0
EN

Stack Overflow用户

发布于 2011-05-13 22:02:35

代码语言:javascript
复制
$(".test a").hover(function(){      var activeTab = $(this).attr("href"); 

        $(activeTab).toggle({ height: "200px" });   }, function() {
        $(activeTab).stop(true, false).animate({ height: "100px" });    }); });

这里有一些错误...一是,数一数你的括号……

代码语言:javascript
复制
$(".test a").hover(function(){
    var activeTab = $(this).attr("href"); 
    $(activeTab).toggle({ height: "200px" });
},
function() {
    $(activeTab).stop(true, false).animate({ height: "100px" });
});
}); // too many closing brackets

第二个问题是第二个函数中的var activeTab超出了作用域。因此您需要在第二个函数中重新定义var activeTab

这应该是可行的:

代码语言:javascript
复制
$(".test a").hover(function(){
    var activeTab = $(this).attr("href"); 
    $(activeTab).toggle({ height: "200px" });
},
function() {
    var activeTab = $(this).attr("href"); 
    $(activeTab).stop(true, false).animate({ height: "100px" });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5992959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档