首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery .toggle .animate

Jquery .toggle .animate
EN

Stack Overflow用户
提问于 2014-07-14 13:36:07
回答 3查看 389关注 0票数 0

我想.toggle一个.animation。您能帮我找出为什么jquery代码不能工作吗?

小提琴在这里

代码语言:javascript
复制
$(document).ready(function() {
$('.adiv').toggle(
   function(){
    $(this).animate({
        width: "150",
        height: "150",
    }, 1000);
    },
    function(){
    $(this).animate({
        width: "77", 
        height: "77",
    }, 1000);        
});
});

谢谢你的帮忙!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-07-14 13:43:51

http://jsbin.com/sirodego/1/edit

代码语言:javascript
复制
$('#toggle').toggle(function(){
    $(this).animate({
       width: "150",
       height: "150",
    }, 1000);
  },function(){
 $(this).animate({
     width: "77", 
     height: "77",
 }, 1000);        
});

这很好,对于第一个条件,要么隐藏,要么显示哪个将是第一个动画,然后做相反的将是第二个动画。

如果你需要做更多的事,请告诉我。我的小提琴告诉你发生了什么。

票数 -1
EN

Stack Overflow用户

发布于 2014-07-14 13:44:30

注意:你的代码很好用!问题非常简单:在您的jsfiddle中,您忘记导入jquery!使用左边的侧边栏,然后再试一次!

顺便说一下,我的解决方案是使用mouseover和mouseout:

演示

代码语言:javascript
复制
$(document).ready(function() {
$('.adiv').mouseover(function()
     {$(this).animate({
            width: "150",
            height: "150"
        }, 1000);
    }).mouseout(
    function(){
        $(this).animate({
            width: "77", 
            height: "77"
        }, 1000);        
});
});
票数 0
EN

Stack Overflow用户

发布于 2014-07-14 13:48:02

试试这段代码

代码语言:javascript
复制
$(document).ready(function() {
    $('.adiv').click( function(){
    if($(this).hasClass('active')){
        $(this).animate({
            width: "30",
            height: "30",
        }, 1000);
    $(this).removeClass('active');
    }  
    else{
        $(this).animate({
            width: "150", 
            height: "150",
        }, 1000);
    $(this).addClass('active');        
     };
  });      
});

检查这个演示

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24737789

复制
相关文章

相似问题

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