【发布时间】:2017-07-20 01:53:39
【问题描述】:
我有一个包含一些元素的网格。这些元素具有类 item 。当我点击其中一个时,我使用 expand 调整该元素的大小。
<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() 方法没有添加新值。
【问题讨论】:
标签: javascript jquery html css height