简体   繁体   English

CSS过渡不起作用(高度和边距)

[英]CSS Transition don't work (height and margin)

I really don't know why this doesn't work: fiddle 我真的不知道为什么这行不通: 提琴
When I click on the red box, I want it to move down 50px and change the height to 200px. 当我点击红色框时,我希望它向下移动50像素并将其高度更改为200像素。

Any Idea? 任何想法?

Well this is how CSS work sadly, IDs take higher priority as a selector than a class would read this article for more info. 好吧,这就是CSS令人遗憾的工作方式,ID作为选择器的优先级高于类阅读本文以获得更多信息的优先级。

On click i added a class with the size/marign changes you wanted. 在单击时,我添加了一个您想要的尺寸/ marign更改的类。

.box {
    background-color: red;
    height: 100px;
    margin-top: 0px;
    width: 100px;
    display: block;
    transition: all .5s ease-in-out;
}

.box-active {
    height: 200px;
    margin-top: 50px;
}

The JS JS

$('.box').click(function(){
    $(this).addClass("box-active");
});

Here's a fiddle with the working solution. 这是工作解决方案的摆弄。 Fiddle 小提琴

You could use this instead: 您可以改用以下方法:

jsFiddle example jsFiddle示例

$('#box').click(function(){
  $(this).css('margin-top','50px');
  $(this).css('height','200px');
});

I also added in the -webkit- / -moz- vendors/prefixes. 我还添加了-webkit- / -moz-供应商/前缀。

  function changeprop(id)
{
    $(id).css({'margin-top', '50px'});
    $(id).css({'height', '200px'});
}

SyntaxError: missing : after property id $(id).css({'margin-top', '50px'}); 语法错误:缺少:属性ID $ {id).css({'margin-top','50px'})之后;

use : between property name and value not , 使用:在属性名称和值之间,

  function changeprop(id)
{
    $(id).css({'margin-top': '50px'});
    $(id).css({'height': '200px'});
}

this will work fine 这会很好

maybe you can write your code in local html file and watch the console message throw firebug or other develop tool 也许您可以在本地html文件中编写代码,并观看控制台消息抛出Firebug或其他开发工具

thus can suffer less from error 因此可以减少错误

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

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