简体   繁体   English

jQuery动画表格单元格

[英]jQuery animate table-cell

I am trying to animate an a tag, it has a css property of table-cell to get the effect I am looking for. 我试图为a标签设置动画,它有一个table-cell的css属性来获得我正在寻找的效果。 I am looking to animate it when someone clicks on it using jQuery, but it isn't working, and I think it is because of the table-cell but removing table-cell breaks the layout. 当有人使用jQuery点击它时,我希望为它设置动画,但它不起作用,我认为这是因为table-cell但删除table-cell会破坏布局。 Here is the page I am working with . 这是我正在使用的页面

Here is a fiddle: http://jsfiddle.net/nEryb/ 这是一个小提琴: http//jsfiddle.net/nEryb/

.clip{
    background-color: #373938;
    min-height: 100%;
    height: 100%;
    width: 300px;
    background-position: center center;
    background-size: auto 100%;
    background-repeat: no-repeat;
    display: table-cell;
    box-shadow: -2px 0 5px rgba(0,0,0,.2), -2px -2px 5px rgba(0,0,0,.2), -2px 2px 5px rgba(0,0,0,.2);
    text-align: center;
    filter: grayscale(100%);
    filter: url(/media/images/new/filters.svg#grayscale);
    filter: gray;
    -webkit-filter: grayscale(1);
    position: relative;
}

Here is the jquery: 这是jquery:

$(document).on("click", "a.clip", function(e){
    e.preventDefault();
    window.history.pushState("Gallery", "Gallery", $(this).attr("href"));
    $("a.clip.hover").animate({
        height: "20px"
    }, "fast");
});

What can I do to get this to work? 我该怎么做才能让它发挥作用?

In order to properly use the CSS table-model , you have to understand how the browser renders such a model, and what each concept really means . 为了正确使用CSS表模型 ,您必须了解浏览器如何呈现这样的模型,以及每个概念的真正含义

In a nutshell, your code doesn't work because a table cell is supposed to take the entirety of the space so that it might cover all the relative coordinates of the grid. 简而言之,您的代码不起作用,因为表单元格应该占用整个空间,以便它可以覆盖网格的所有相对坐标。 If you think about how a table should work, it makes sense. 如果你考虑一个表应该如何工作,这是有道理的。 That is why it has historically been difficult to make tables behave, and you can see many examples of such questions here on SO. 这就是为什么历史上很难让表格表现出来的原因,你可以在这里看到很多关于这些问题的例子。

One solution could be to actually apply your animation and most of your CSS properties to an included block element. 一种解决方案可能是将您的动画和大多数CSS属性实际应用于包含的块元素。

In the following example I used your markup, including an anchor element, but making it display: block in the CSS. 在下面的示例中,我使用了您的标记,包括一个anchor元素,但在CSS中display: block

Example 1 例1

Another solution would be to forego the table model and use inline-block on your elements. 另一个解决方案是放弃表模型并在元素上使用inline-block Get rid of that display: table and change display: table-cell to display: inline-block ; 摆脱那个display: table和change display: table-cell to display: inline-block ; give it an explicit width: 20% and, also, your height is not going anywhere as long as you keep that min-height: 100% and don't change it. 给它一个明确的width: 20% ,而且,只要你保持min-height: 100% ,你的身高就不会去任何地方min-height: 100%并且不要改变它。

Example 2 例2

You can use display: inline-block because table-model somehow does not allow you to change the sizes of the relative cells. 您可以使用display: inline-block因为table-model以某种方式不允许您更改相对单元格的大小。 Anyway, on your jQuery code, you should use the $(this) selector for animating the specific box clicked 无论如何,在您的jQuery代码上,您应该使用$(this)选择器为单击的特定框设置动画

$(document).on("click", "a.clip", function(e) {
    e.preventDefault();
    window.history.pushState("Gallery", "Gallery", $(this).attr("href"));
    $("a.clip.hover").animate({
        height: "20px"
    }, "fast");
});

Here is an update: http://jsfiddle.net/nEryb/1/ 这是一个更新: http//jsfiddle.net/nEryb/1/

I also noticed your selector has a.clip.hover . 我还注意到你的选择器有一个a.clip.hover You should be using the hover event instead of click . 您应该使用hover事件而不是click

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

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