简体   繁体   English

我无法让.animate()去工作

[英]I'm having trouble getting .animate() to work

Link: https://jsfiddle.net/nbonne/9hcoy9j8/ 链接: https//jsfiddle.net/nbonne/9hcoy9j8/

I aim is to have the medium blue box and everything in it move up when the search button is pressed. 我的目标是在按下搜索按钮时使用中蓝色框并且其中的所有内容都会向上移动。

I know my selector is for "form" and it's trying to change the background, I'm having trouble getting anything to animate. 我知道我的选择器是用于“形式”而它正在尝试改变背景,我无法获得任何动画效果。

I think this is the correct way to accomplish it but nothing happens: 我认为这是完成它的正确方法,但没有任何反应:

$("#search-btn").click(function{
    $("controls-main").animate({
        background: "red"
    }, 500);
})

jQuery on its own can't animate colors. jQuery本身不能为颜色设置动画。 You need another library like jQuery UI. 你需要另一个像jQuery UI这样的库。 See this SO answer for more information. 有关更多信息,请参阅此SO答案

In the fiddle: 在小提琴:

  1. Second click handler was missing parentheses (syntax error in console) 第二次单击处理程序缺少括号(控制台中的语法错误)
  2. s-form class was missing from your form html code (empty selector) 你的表单html代码(空选择器)中缺少s-form类
  3. $('search') is a wrong selector. $('search')是一个错误的选择器。 You probably wanted $('[name=search]'); 你可能想要$('[name = search]');
  4. You are animating background, which jQuery's animate doesn't animate. 你是动画背景,jQuery的动画没有动画。

Updated working fiddle: https://jsfiddle.net/9hcoy9j8/18/ 更新了工作小提琴: https//jsfiddle.net/9hcoy9j8/18/

$(document).ready(function() {
  $(".rand-wiki").click(function() {
    window.open("https://en.wikipedia.org/wiki/Special:Random");
  });
  $(".s-form").on('click', function () {
    $("[name=search]").animate({
      marginTop: '40px'
    }, 500);
  });
});

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

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