我有一个网格与一些元素。这些元素的类别为item
。当我点击其中的一个时,我会通过expand
来调整此元素的大小。JQuery CSS方法不改变高度元素
<body>
<div class="grid">
<a href="#">
<div class="item" style="background-image: url('https://i.giphy.com/l0NwI1oBNxYfoRRny.gif'); height: 270px; background-size: cover; background-repeat: no-repeat; position: absolute;">
</div>
</a>
<a href="#">
<div class="item" style="background-image: url('https://i.giphy.com/pYI1hSqUdcBiw.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;">
</div>
</a>
<a href="#">
<div class="item" style="background-image: url('https://i.giphy.com/l3vR58nYgtIUKua1G.gif'); height: 149px; background-size: cover; background-repeat: no-repeat; position: absolute;">
</div>
</a>
<a href="#">
<div class="item" style="background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;">
</div>
</a>
...
</div>
</body>
CSS
.item {
color: white;
display: table;
font-size: 1.4em;
text-align: center;
margin: 5px;
width: 270px;
}
.expand {
transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
-webkit-transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
height: 100% ;
width: 100%;
left: 0 !important;
top: 0 ;
z-index: 99;
text-indent: -9999px;
}
JS
$('.item').click(function() {
$(this).toggleClass('expand');
});
所以,width
调整大小以父大小(grid
)的100%,但height
没有变化。
这是前点击网格视图中的任何项目元素
上点击第三个元素
等信息之后,现在的问题是:在同一个JS我尝试添加的方法:
$('.item').click(function() {
var height = $(this).height();
var width = $(this).width();
var act = $(this).hasClass("expand");
if(act){
$('.expand').css('height',Math.floor($('.grid').width() * height/width) + 'px !important');
$('.expand').css('width','50% !important');
}
}
但height
和width
不改变。所以,由于某种原因,这个$('.expand').css()
方法没有添加新的值。
的问题是,每一个选项'item'元素必须定义内联样式的高度。我尝试做'$('。expand').css('height',Math.floor($('。grid').width()* height/width)+'px!important');'但是不起作用。如果我在'width'上试试'!important',它不会起作用$'('。expand')。css('width','50%!important');'。 –
@AlbertoCrespo'!重要的'工作得很好 - 更新了我的答案。 –