简体   繁体   English

使用动画更改div高度onclick

[英]Change div height onclick with animation

I'm trying to make a gallery using divs that change their height when you click on them. 我正在尝试使用div来创建一个库,当你点击它们时会改变它们的高度。 Ideally, this would include animation to smoothly expand the div's height. 理想情况下,这将包括平滑扩展div高度的动画。 There will be several of each div on each page, so it needs to just expand that section. 每个页面上会有几个div,因此只需要展开该部分。

It's actually supposed to turn out something like the news section on this page: http://runescape.com/ 它实际上应该是这个页面上的新闻部分: http//runescape.com/

I'd like to do it with JavaScript/jQuery if possible. 如果可能的话,我想用JavaScript / jQuery做。

$('div').click(function(){
    $(this).animate({height:'300'})
})

Check working example at http://jsfiddle.net/tJugd/ 查看http://jsfiddle.net/tJugd/上的工作示例

Here's the code I ended up using: 这是我最终使用的代码:

JS: JS:

document.getElementById("box").addEventListener("click", function() {
    this.classList.toggle("is-active");
});

CSS: CSS:

#box {
    background: red;
    height: 100px;
    transition: height 300ms;
    width: 100px;
}

#box.is-active {
    height: 300px;
}

HTML: HTML:

<div id="box"></div>

Fiddle: 小提琴:

https://jsfiddle.net/cp7uf8fg/ https://jsfiddle.net/cp7uf8fg/

try 尝试

$('div').toggle(function(){
    $(this).animate({'height': '100px'}, 100);
}, function(){
    $(this).animate({'height': '80px'}, 100);
});

DEMO DEMO

jQuery rules. jQuery规则。 Check this out. 看一下这个。

http://api.jquery.com/resize/ http://api.jquery.com/resize/

The complete solution: 完整的解决方案:

Both spacer DIV and Margin or Padding on content DIV works but best to still have a spacer DIV. 内容DIV上的间隔DIV和边距或填充都有效,但最好还是有间隔DIV。

Responsive design can be then applied to it in your CSS file. 然后可以在CSS文件中将响应式设计应用于它。 This is mutch better as with JAVA the screen would flicker! 随着JAVA屏幕闪烁,这变得更好! If you use a grid system there will be a media query part there you need to include your settings. 如果您使用网格系统,则需要在其中包含媒体查询部分以包含您的设置。

I use a little spacer on HD screen while its increasing till mobile screen! 我在高清屏幕上使用一个小间隔,而它增加到移动屏幕!

Still if you have breadcrumb in header multiple lines can be tricky, so best to have a java but deferred for speed resons. 仍然如果你在标题中有多个行的痕迹可能是棘手的,所以最好有一个java但延迟速度resons。 Note that animation is for getting rid of flickering of screen. 请注意,动画是为了摆脱屏幕的闪烁。

This java then would only fire if breadcrumb is very long otherwise single CSS applied via the grid and no flickering at all. 然后,只有当痕迹很长时才会触发此java,否则通过网格应用单个CSS并且根本不会闪烁。 Even if java fired its doing its work via an elegant animation 即使java通过优雅的动画解雇了它的工作

var header_height = $('#fixed_header_div').height();
var spacer_height = $('#header_spacer').height() + 5;    

if (header_height > spacer_height) {
    $('#header_spacer').animate({height:header_height});
};

Note that I have applied a 5px tolerance margin! 请注意,我已经应用了5px的容差范围!

Ho this helps :-) 这有助于:-)

I know this is old, but if anyone seems to find their way here. 我知道这是旧的,但如果有人似乎在这里找到他们的方式。 @JacobTheDev answer is great and has no jQuery! @JacobTheDev答案很棒,没有jQuery! I have added a little more for use cases where the event is not being assigned at the same point your toggling the css class. 对于在切换css类的同一点没有分配事件的用例,我添加了一些。

HTML HTML

<div id='item' onclick='handleToggle()'> </div>

JS JS

handleToggle(event){    
  document.getElementById(event.target.id).classList.toggle('active')
}

CSS CSS

#item {
  height: 20px;
  transition: 1s;
}

.active {
  height: 100px;
}

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

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